My goal is to switch spaces from within an apple script. My shortcut to switch to the 4th space is ctrl+4, but when I tried this in an apple script nothing happens:

tell application "System Events"
    keystroke "4" using {control down}
end tell

What am I doing wrong?


I do not know why, but using key code instead works

tell application "System Events"
    tell application "System Events" to key code 21 using control down
end tell
  • If this works for you, you can mark it as accepted by clicking the check mark. This shows the community that the problem has been solved. – Arc676 Feb 15 '16 at 1:31
  • Possibly the reason this works is that the key code for 4 'above qwerty' is 21 but the 4 on the 10-key is 86 & the key commands for switching Spaces are sensitive to that. – Tetsujin Feb 15 '16 at 7:37
  • 1
    @Arc676 I will, in 2 days when the system will allow it. – samcarter Feb 15 '16 at 9:22
  • @Tetsujin That makes sense, thanks for your explanation! – samcarter Feb 15 '16 at 17:09
  • Additional key codes can be found here: eastmanreference.com/complete-list-of-applescript-key-codes – StevieD Apr 1 at 19:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .