Skip to content

Instantly share code, notes, and snippets.

@thewellington
Created January 30, 2015 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewellington/1680851c910ed793a4ce to your computer and use it in GitHub Desktop.
Save thewellington/1680851c910ed793a4ce to your computer and use it in GitHub Desktop.
Programmatically switch between your first 16 desktops in OS X Mavericks and Yosemite
#!/bin/bash
#
# switch.sh
# Allows for scripting the switching of the first 16 "Desktops" in OS X Mavericks
#
# Requires Mission Control Keyboard Shortcuts to be activated:
# Go to System Preferences -> Keyboard -> Shortcuts -> Mission Control.
# Click the expand Triangle next to Mission Control in the right hand pane.
# Activate "Switch to Desktop { 1-16 }
#
# Script assumes that the keyboard shortcuts are set to the default of ^1-9
# for the first nine desktops, ^0 for desktop 10 and finally ^opt1-6 for
# the remaining six desktops up to 16.
#
# v1.0 3014-05-27 by bill@wellingtonnet.net
# Keycode Mappings (for reference)
# -----------------
# | Key | Keycode |
# | 1 | 18 |
# | 2 | 19 |
# | 3 | 20 |
# | 4 | 21 |
# | 5 | 23 |
# | 6 | 22 |
# | 7 | 26 |
# | 8 | 28 |
# | 9 | 25 |
# | 0 | 29 |
switch()
{
DESKTOP=$1
# Build the array of desktop keycodes
DESKTOPS=(18 19 20 21 23 22 26 28 25 29)
if [[ $DESKTOP -le 10 ]]; then
INDEX=$(($DESKTOP - 1))
KEYCODE=${DESKTOPS[$INDEX]}
osascript <<EOF
tell application "System Events"
key code "$KEYCODE" using control down
end tell
EOF
elif [[ $DESKTOP -gt 10 ]]; then
INDEX=$(($DESKTOP - 10))
KEYCODE=${DESKTOPS[$INDEX]}
osascript <<EOF
tell application "System Events"
key code "$KEYCODE" using {control down, option down}
end tell
EOF
else
echo 'your display is not supported'
fi
}
# Allow script to be called directly
if [[ $0 == *switch.sh ]]; then
switch $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment