I'm writing a simple applescript that should focus an app and click "cmd+1".
This is what I wrote:
tell application "System Events"
tell application process "appname"
--Lobby focus
activate
keystroke "1" using command down
end tell
end tell
But instead of working, there's one beep and the application doesn't even take focus.
How can I solve this?
You can't tell processes to activate. Change it to set frontmost to true:
tell application "System Events"
set frontmost of process "Finder" to true
keystroke "1" using command down
end tell
Or tell the application to activate:
activate application "Finder"
tell application "System Events"
keystroke "1" using command down
end tell
If the application has no open windows, reopen opens a new default window:
tell application "Finder"
reopen
activate
end tell
tell application "System Events"
keystroke "1" using command down
end tell
Related
Why does the following Applescript hide Script Editor?
tell application "Last.fm" to launch
tell application "System Events" to tell process "Last.fm" to keystroke "h" using command down
This code I found will hide "Last.Fm", but also hides Script Editor. Ideally, I want to replace the keystroke "h" with a keystroke "w" but then I get an error:
The document can’t be closed while the script is running.
Why does the script I wrote effect Script Editor?
I don't have Last.fm, so I tried this:
tell application "TextEdit" to launch
delay 2
tell application "System Events" to tell process "TextEdit" to keystroke "h" using command down
And sure enough, it's true, TextEdit was hidden but so was Script Editor.
Then I tried this:
tell application "TextEdit" to launch
tell application "TextEdit" to activate
delay 2
tell application "System Events" to tell process "TextEdit" to keystroke "h" using command down
TextEdit was hidden, but Script Editor was not. So I would guess that this will help in your code too. Having the target app frontmost appears to be crucial (which makes a certain amount of sense, after all).
What is the functionality you're trying to achieve here? Is this the whole script? You want last.fm to launch, and then, ideally, to close the window?
tell application "System Events" to tell process
Doesn't work because it is highly misleading - it will not actually direct a keystroke to a particular application, the keystroke goes wherever it would go when it's hit regardless of the "tell process" statement, which is annoying.
I don't use last.fm, but either of these works for me:
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
keystroke "w" using command down
end tell
or
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
click menu item "Close" of menu 1 of menu bar item "File" of menu bar 1
end tell
Note that if you plan to run the script from a key command or the script menu, and don't want it to steal focus to last.fm, you can have it launch, close the window (or hide it), and then return you to the front app when the script was run:
set appPath to the path to the frontmost application
tell application "Finder"
set appName to the name of file appPath
set appName to text 1 thru ((offset of "." in appName) - 1) of appName
end tell
tell application "TextEdit"
launch
activate
end tell
delay 0.1
tell application "System Events"
keystroke "w" using command down
end tell
tell application appName to activate
This is my current code:
tell application "Finder"
tell application "System Events" to keystroke "k" using {command down}
end tell
But it won't work.
Any tips?
You do not tell the Finder to tell system events to do stuff.
You need to just make sure the Application that you want to intercept the keystrokes is active. Then tell System events to do the keystroke.
tell application "Finder" to activate
tell application "System Events"
keystroke "k" using {command down}
end tell
I got error; Can’t get button "save" of process "TextEdit".
activate application "TextEdit"
tell application "System Events" to tell process "TextEdit"
keystroke "s" using {command down}
click button "save"
end tell
I was also tring to include like "of window 1" still I can't get this working. Any help will be much appreciated. thanks
why not just talk to the app directly ?
tell application "TextEdit"
tell document 1 to save
end tell
if you must use the GUI you need the correct hierarchy ( but it is best to talk to the app directly)
activate application "TextEdit"
tell application "System Events"
tell process "TextEdit"
keystroke "s" using {command down}
delay 1
click button "Save" of sheet 1 of window 1
end tell
end tell
I created a code that toggles the keyboard viewer in Automator.
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
activate application "KeyboardViewer"
end if
return input
end run
However, the Keyboard Viewer becomes the current running window and I cannot instantaneously start typing (I have to switch back to the previous window). Is there a specific code that I can add so that the previous window is highlighted again ?
You could use launch instead of activate:
tell application "KeyboardViewer"
if running then
quit
else
launch
end if
end tell
If the application wasn't open, launch usually opens a new window above other applications but below the frontmost application. Otherwise it just keeps the application on the background. You can use AXRaise to raise windows in the second case, but it also makes them look like active windows.
launch application "Terminal"
tell application "System Events" to tell process "Terminal"
perform action "AXRaise" of windows
end tell
You could also save the previous application in a variable:
set a to path to frontmost application as text
activate application "Terminal"
activate application a
If you're moving focus to a background application, you can just activate the frontmost application later:
try
tell application "SystemUIServer"
display dialog "" default answer ""
end tell
end try
activate application (path to frontmost application as text)
Try this just after the activate application "KeyboardViewer" line...
tell application "System Events" to keystroke tab using command down
EDIT: Since the original post above didn't do it for you then try this. It uses a subroutine I use to get the frontmost running application in cases just like this. Just put this code into the applescript section of your automator action...
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
set frontAppPath to my getFrontAppPath()
activate application "KeyboardViewer"
delay 0.2
tell application frontAppPath to activate
end if
return input
end run
on getFrontAppPath()
set frontAppPath to (path to frontmost application) as text
set myPath to (path to me) as text
if frontAppPath is myPath then
try
tell application "Finder" to set bundleID to id of file myPath
tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false
-- we need to delay because it takes time for the process to hide
-- I noticed this when running the code as an application from the applescript menu bar item
set inTime to current date
repeat
set frontAppPath to (path to frontmost application) as text
if frontAppPath is not myPath then exit repeat
if (current date) - inTime is greater than 1 then exit repeat
end repeat
end try
end if
return frontAppPath
end getFrontAppPath
I'm trying to send a URL to chrome for viewing flash, quit Safari in the meanwhile so it's not using up memory, and then as soon as I quit Chrome, go back to Safari. It's not predictably going back to Safari after I quit Chrome, so I need help with the repeat loop. I want to run this as a service. Thanks!
property theURL : ""
on run {input, parameters}
tell application "Google Chrome"
activate
end tell
tell application "Safari"
activate
end tell
tell application "System Events"
keystroke "j" using {command down} -- Highlight the URL field.
keystroke "c" using {command down}
keystroke "w" using {command down}
end tell
delay 0.1
tell application "Safari"
quit
end tell
tell application "Google Chrome"
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of front window to the clipboard
activate
end tell
repeat
if application "Google Chrome" is not running then exit repeat
delay 5
end repeat
tell application "Safari"
activate
end tell
return input
end run
Update! Here's the working script:
property theURL : ""
on run
tell application "Safari"
activate
set theURL to URL of document 1
quit
end tell
tell application "Google Chrome"
activate
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of window 1 to theURL
activate tab 1
end tell
repeat
tell application "System Events"
if "Google Chrome" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
tell application "Safari"
activate
end tell
end run
You could try this...
repeat
tell application "System Events"
if "Google Chrome" is not in (name of application processes) then exit repeat
end tell
delay 5
end repeat
Also I always avoid using keystrokes and other gui scripting stuff if I can avoid it. They're not 100% reliable. As such I suggest you transfer the url like this...
tell application "Safari" to set theURL to URL of document 1
and...
tell application "Google Chrome" to set URL of active tab of window 1 to theURL