2

I considered posting this to Ask Different but I thought it belonged here due to being mainly focused on code.

I have created a macro on my mac for switching spaces, and since upgrading to Sierra the following AppleScript is no longer working. Does anyone know if something has changed?

tell application "System Events" to key code 124 using control down

and

tell application "System Events" to key code 123 using control down

This is the output when running in terminal (note the ^[[1;5D):

14:16 isaac@Isaac ~ $ osascript -e 'tell application "System Events" to key code 123 using control down'
^[[1;5D14:18 isaac@Isaac ~ $ ;5D

And when running via AppleScript Editor, nothing happens.

Isaac
  • 11,409
  • 5
  • 33
  • 45

2 Answers2

2

Make sure your key codes match the keyboard shortcuts in system preferences. Here are my keyboard shortcuts in system preferences and they do coincide correctly with my AppleScript commands.

enter image description here

tell application "System Events"
    key code 18 using (control down) -- Desktop 1
end tell

tell application "System Events"
    key code 19 using (control down) -- Desktop 2
end tell

tell application "System Events"
    key code 20 using (control down) -- Desktop 3
end tell

tell application "System Events"
    key code 21 using (control down) -- Desktop 4
end tell

These function correctly for me in the latest version of Sierra.

wch1zpink
  • 3,026
  • 1
  • 8
  • 19
2

Yes, it's a bug.

To simulate some global shortcut with the Control key, the command needs the fn key (a workaround for Sierra).

It's not possible to use the fn key with the AppleScript's key code command, but it's possible with the methods of the Core Graphics framework in a Python script.


Here's the script to simulate this shortcut --> (Right Arrow + Control), you can run the script in the Terminal (in a sh, bash or any similar shell)

/usr/bin/python -c 'import time; import Quartz.CoreGraphics as QCG; e = QCG.CGEventCreateKeyboardEvent(None, 124, True); QCG.CGEventSetFlags(e, (QCG.kCGEventFlagMaskControl | QCG.kCGEventFlagMaskSecondaryFn)); QCG.CGEventPost(QCG.kCGHIDEventTap, e); time.sleep(0.1); QCG.CGEventSetType(e, QCG.kCGEventKeyUp); QCG.CGEventPost(QCG.kCGHIDEventTap, e)'

Here's an AppleScript to test in the "Script Editor" application:

--  For switching spaces, 124 = the Right Arrow key, use 123 for the Left Arrow key
do shell script "/usr/bin/python -c 'import time; import Quartz.CoreGraphics as QCG; e = QCG.CGEventCreateKeyboardEvent(None, 124, True); QCG.CGEventSetFlags(e, (QCG.kCGEventFlagMaskControl | QCG.kCGEventFlagMaskSecondaryFn)); QCG.CGEventPost(QCG.kCGHIDEventTap, e); time.sleep(0.1); QCG.CGEventSetType(e, QCG.kCGEventKeyUp); QCG.CGEventPost(QCG.kCGHIDEventTap, e)'"
jackjr300
  • 7,111
  • 2
  • 15
  • 25
  • You legend... I thought something was up – Isaac Aug 21 '17 at 22:36
  • I tried your solution jackjr300 but it wouldn't work for me. Does this still work in the latest version of OS X? – user185813 May 04 '18 at 14:10
  • The bug may be resolved, I don't know because I have not installed high Sierra yet. Try to remove `| QCG.kCGEventFlagMaskSecondaryFn` in the script. – jackjr300 May 18 '18 at 00:30