1

I'm very new to Applescript and I was looking to create an application that automatically switches between the desktops or spaces (as Apple calls them). Here is the code I have so far:

tell application "System Events"
key code 39 using {control down}
end tell

If I replace the "key code 39 using {control down}" section with keystroke "n" or any other key letter, it appears to work, but with my current code nothing occurs. Why would it not be executing?

Steve Hold
  • 25
  • 4

1 Answers1

2

Code 39 is not the arrow you want for space changes. I found a list of numbers in another answer and key code 124 was the equivalent of using the right arrow to switch to the next space in my setup. I plugged that number into your code:

tell application "System Events"
    key code 124 using {control down}
end tell

And this successfully switched to my next space.

Note that with your code, something did happen when running it inside of Script Editor: an apostrophe was added to the end of the code.

Community
  • 1
  • 1
Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
  • Funny, it works when I save the script as application but not when executed in Script-Editor. –  Mar 28 '15 at 01:38
  • Definitely works in Script Editor for me; perhaps its a version issue? I am using OS X 10.10.2. – Jerry Stratton Mar 28 '15 at 01:46
  • My bad, I forgot that I had a little hack running - had to relaunch Script-Editor to reset that and now it works as expected. –  Mar 28 '15 at 01:55
  • And this answer ( http://stackoverflow.com/questions/27454072/apple-script-simulate-key-events/27535024#27535024 ) showed a link to an exhaustive list of key codes. On it you can see that key code 39 is the single quote '. (Link is http://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-keys.html ) – jweaks Mar 28 '15 at 04:32