Applescript keystroke not behaving as expected

3.4k Views Asked by At

I'm experimenting with Applescript for the first time, and am trying to build a script to setup my default layout of applications for developing at work. This involves placing applications across multiple Mission Control spaces. My problem at hand is simply moving about the spaces. I found in many posts similar to this that such action could be achieved with

tell application "System Events"
    tell process "Finder"
        keystroke "1" using control down
    end tell
end tell

if the appropriate key binding was in place. I made the Preferences change so I could use control+1 to move to the first MC space. However, running the script doesn't do anything. This is the event log output:

tell application "System Events"
    keystroke "1" using control down
end tell

No errors that I can see, but again: new to Applescript. I've tried many variations of this command including wrapping control down in curly braces and wrapping the call to "System Events" inside a call to "Finder" like this

tell application "Finder"
    tell application "System Events"
        keystroke "1" using control down
    end tell
end tell

but the output is exactly the same with no shift in view. I think I'm missing something here...

According to this question's responses, I tried adding in a delay to make sure I wasn't stepping on my own feet with running the script with CMD-r but nothing happens. I hear the sound effect when you try to click out of an important focus window (if that makes any sense), it's a short beep. Am I talking to the applications improperly?

EDIT

Ok I got something working, but I'm a little confused why this is the case.

tell application "Finder"
    activate
    delay 0.2
    tell application "System Events" to keystroke "a" using control down
end tell

This accomplishes what I need, but I have to change the key binding to a letter. I can replicate the error tone by pressing control+1 when Applescript Editor is active. I guess there's a shortcut for AE that uses the key combo. But why is that running when Finder is supposed to be active?

4

There are 4 best solutions below

0
On

To answer your question, in your working code the difference is that you activate the Finder before you issue the keystroke command. Keystroke commands are always sent to the frontmost application so you must always make sure to activate an application first as you have done.

If control-1 didn't work then I suspect either some Finder command uses that combo or some other application uses that in a global context meaning it intercepts that command no matter which application is frontmost. Otherwise it should work for you.

Finally, I would remove your system events line of code from the Finder tell block of code. There's no reason the tell the Finder to tell system events to perform a command. Just put that line on its own after the "end tell" line.

Good luck.

1
On

I've just been working with this - I don't want to activate finder before triggering what should be a global shortcut. Keystroke was not working, however I tried key code and that indeed works:

on run {}
    tell application "System Events"
        key code {18} using {command down}
    end tell
    return "success"
end run

There is a list of key codes at this question: https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript

0
On

I found this tool to be a good alternative that does not have that issue.

Installation is as simple as:

brew install socsieng/tap/sendkeys

Usage:

sendkeys send --initial-delay 0 --delay 0.001 --characters 'Hello'

https://github.com/socsieng/sendkeys

0
On

This should work:

tell application "Finder"
activate
end tell
delay 0.2
tell application "System Events"
key code 18 using {control down}
end tell