I'm trying to find out what the frontmost application x is, run a script, then set x to the frontmost application using applescript.
tell application "System Events"
set frontApp to displayed name of first process whose frontmost is true
set apptemp to frontApp
end tell
[code here]
tell application "System Events"
set frontmost of process whose name is apptemp to true
end tell
Although this code does not return any bugs it does not work. I've also tried this code
tell application "System Events"
set apptemp to application "Google Chrome"
set frontmost of process "Google Chrome" to true
end tell
But again, although there are no bugs it will not work.
Also, someone please tell the admin to make it easier to display code. I have the most difficult time displaying code on this website. I have to indent four spaces for each line of code, that's insane.
Even better than both other approaches is using the bundle identifier which is a unique key.
tell application "System Events"
set frontAppID to bundle identifier of first process whose frontmost is true
end tell
-- Sample code... can be anything else
activate application "Finder"
delay 3
-- End of sample code
activate application id frontAppID
A robust way to handle this is to use path to frontmost application as follows:
# Save which application is frontmost (active),
# as an absolute, HFS-style path string pointing to the application bundle.
set frontmostAppPath to (path to frontmost application) as text
# Activate and work with another application.
activate application "Reminders"
delay 2
# Make the previously frontmost (active) application frontmost again.
activate application frontmostAppPath
Using the application's specific path ensures that the very same application is reactivated, irrespective of duplicates and applications whose process names differ.
Note: It's tempting to want to save and restore the object reference frontmost application directly, but that doesn't actually work: inexplicably, such a saved reference is treated the same as the current application (the one running the code).
I have a folder of images that gets updated from a camera taking pictures periodically throughout the day. I'm trying to write some applescript that will create a slideshow from a folder of images but also update as more are added without having to rerun the script. I started out trying to do quick look but couldn't get that working. Any ideas on how best to tackle this?
UPDATE
this is what I have hacked together so far:
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
I don't believe this works if I add photos behind the scenes though.
This is what I came up with and it works perfectly for what I need.
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
repeat while true
delay 60
tell application "System Events"
tell process "Finder"
keystroke "a" using command down
end tell
end tell
end repeat
A few caveats... since it is using quick look, you can't really do anything on the computer while this is running since it can't have any other app activate while it is running (quick look closes when this happens). Also the repeat section is required to get quick look to pickup the new additions to the directory without losing focus. Pretty nasty stuff, but I couldn't really find another easy way to do it!
I want to use AppleScript to auto login for a web by Safari.
When I open a URL and give username and password, if it is the first time or hasn't saved the password yet, it will show message: "Would you like to save this password?".
I want to use AppleScript to choose "Not now". How can I do that?
Here is my current code:
tell application "Safari"
open location "http://www.amazon.com"
activate
end tell
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
set loginURL to do JavaScript "document.getElementById('nav-your-account').getAttribute('href')" in document 1
open location loginURL
end tell
else
say "Page Failed to load or Safari didn't open"
end if
if page_loaded(60) then
say "Page Loaded"
tell application "Safari"
do JavaScript "document.getElementById('ap_email').value = 'mritjp'; document.getElementById('ap_password').value = 'mritjp'; document.forms['signIn'].submit();" in document 1
---HOW TO CONTROL TO CHOOSE "Not Now" in message "Would you like to save this password?" of safari
activate
delay 2
end tell
else
say "Page Failed to load or Safari didn't open"
end if
on page_loaded(timeout_value) -- in seconds
delay 1
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
Fixed by comment of Lauri Ranta:
If access for Assistive Devices has been enabled from System Preferences, you can use System Events:
on enabledGUIScripting()
do shell script "sudo touch /private/var/db/.AccessibilityAPIEnabled" password "1" with administrator privileges
end enabledGUIScripting()
Then:
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell
If access for assistive devices has been enabled from System Preferences, you can use System Events:
tell application "Safari"
do JavaScript "document.getElementById('login_button_id').click()" in document 1
end tell
delay 1
tell application "System Events" to tell process "Safari"
click button 3 of sheet 1 of window 1
end tell
How do I make a new document in TextMate using rb-appscript or AppleScript?
Here is my rb-appscript:
te = app("TextMate")
te.launch
doc = te.make(:new => :document)
But it doesn't work.
Here is the error message I get:
OSERROR: -10000
MESSAGE: Apple event handler failed.
COMMAND: app("/Applications/TextMate.app").make({:new=>:document})
If someone gives me an AppleScript code I can convert it to rb-appscript.
Technically, it's supposed to be just this:
tell application "TextMate"
set theResult to make new document
end tell
But I get the same error in Script Debugger. Creating a new document manually and getting the document via a script works fine. I'm going to say you found a bug in TextMate's Applescript implementation. You could go the GUI scripting route here (shamelessly copied from the Mac OS Automation site):
return do_menu("TextMate", "File", "New")
--> result: true and a window appeared in TextMate
on do_menu(app_name, menu_name, menu_item)
try
-- bring the target application to the front
tell application app_name
activate
end tell
tell application "System Events"
tell process app_name
tell menu bar 1
tell menu bar item menu_name
tell menu menu_name
click menu item menu_item
end tell
end tell
end tell
end tell
end tell
return true
on error error_message
return false
end try
end do_menu
Is it possible to use AppleScript to open a link in a new tab in Safari?
This will work:
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
I think this also does what you asked for, but it is much shorter and is less browser-specific:
do shell script "open http://www.webpagehere.com"
This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:
do shell script "open -a Safari 'http://www.webpagehere.com'"
This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):
tell application "Safari"
open location "http://stackoverflow.com"
activate
end tell
It opens a new window if "Open pages in tabs instead of windows" is set to never though.
tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:
set u to "http://ja.wikipedia.org/wiki/漢字"
tell application "System Events" to open location u
--tell application "Safari" to open location u
--do shell script "open " & quoted form of u
This opens a new tab even when new pages are set to open in windows:
tell application "Safari"
activate
reopen
tell (window 1 where (its document is not missing value))
if name of its document is not "Untitled" then set current tab to (make new tab)
set index to 1
end tell
set URL of document 1 to "http://stackoverflow.com"
end tell
tell application "System Events" to tell process "Safari"
perform action "AXRaise" of window 1
end tell
set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.
I've been using the following script to open hundreds of docs into tabs in a single window.
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://foo.com/bar"}
make new tab with properties {URL:"http://foo.com/baz"}
end tell
end tell
But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:
tell application "Safari"
tell window 1
set URL of (make new tab) to "http://foo.com/bar"
set make new tab to "http://foo.com/baz"
end tell
end tell
Code:
tell application "System Events"
tell application "Safari" to activate
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
tell application "Safari"
set URL of document 1 to "http://www.stackoverflow.com/"
end tell
One problem is that this only works if the system's language is set to English.
It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:theURL}
on error
open location theURL
end try
end tell
I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
Arrgh -- the one line is overflowing. Here it is without the code tags:
tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
I ended up using automator to do this which was much easier and it works.
You can try following approach:
//make Safari window active and topmost
tell application "Safari" to activate
//start communication with Safari
tell application "Safari"
tell window 1
//create new tab and open specified URL
tab with properties {URL:"https://url.com"})
//make tab active
set visible to true
end tell
end tell
Also u can combine usage of Apple script within FastScript (free for 10 shortcut)
To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.
If you want to open new Window than new tab u can play within next:
tell application "System Events"
tell process "Safari"
click menu item "New window" of menu "File" of menu bar 1
end tell
end tell
Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.
Not the shortest solution but also works, and not only in English ...
tell application "Safari"
activate
end tell
tell application "System Events"
set frontmost of process "Safari" to true
keystroke "t" using {command down}
end tell
set myURL to "anyurl.html"
delay 2
tell application "Safari" to set the URL of the front document to myURL
Worked for me in Safari v.11
tell application "Safari"
tell window 1
make new tab with properties {URL:"https://twitter.com"}
end tell
end tell
I found a way to open a new tab in the background with Safari.
tell application "Safari"
set the URL of (make new tab in window 1) to "your.url.net"
end tell
During the time I wrote this answer I made this
tell application "Safari"
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
end try
end tell
New version
tell application "Safari"
repeat
try
display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
set theURL to text returned of result
if theURL is "" then exit repeat
set netProto to "https://"
if theURL contains netProto then
set the URL of (make new tab in window 1) to theURL
else
set the URL of (make new tab in window 1) to netProto & theURL
end if
display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
if button returned of result is "No" then exit repeat
end try
end repeat
end tell
Any suggestions will be appreciate
Best regards