Can I use a Terminal command to switch to a specific space in OS X 10.6?

7

1

Is there any terminal command I can use to switch directly to a space in OS X 10.6 ?

Something like:

spaces switch 2

cwd

Posted 2011-12-14T18:53:00.363

Reputation: 13 508

Answers

10

If you have assigned keyboard shortcuts for switching between Spaces in System Preferences, you can trigger them using AppleScript:

osascript -e 'tell application "System Events" to key code 124 using control down'

This will effectively press Control-Right. Left is 123, Up is 126, and Down is 125.

These are the key codes for the number keys:

1 18
2 19
3 20
4 21
5 23
6 22
7 26
8 28
9 25
0 29


Just assign an alias to that osascript call or wrap it in a shell function.

Daniel Beck

Posted 2011-12-14T18:53:00.363

Reputation: 98 421

Thanks Daniel. I guess you don't know of a way to do it other than the old "have applescript press the keys for me" method then? – cwd – 2011-12-14T20:07:06.280

@cwd I didn't find anything. Given the UI requirement and the default assignable shortcuts it's likely not supported because it's of limited usefulness. But wait a few days, maybe Lri or slhck or someone else has an idea. – Daniel Beck – 2011-12-14T20:30:19.037

Thanks, I gave you an upvote. Take a look at this one too when you have a chance: http://superuser.com/questions/368154/

– cwd – 2011-12-15T02:08:07.497

0

From @Daniel's answer a quick zsh command to switch to a numbered desktop accessible through ctrl + desktopnumber.

switchdesktop() {
    typeset -A desktophash
    desktophash[0]=29
    desktophash[1]=18
    desktophash[2]=19
    desktophash[3]=20
    desktophash[4]=21
    desktophash[5]=23
    desktophash[6]=22
    desktophash[7]=26
    desktophash[8]=28
    desktophash[9]=25
    desktopkey=${desktophash[$1]}
    osascript -e "tell application \"System Events\" to key code $desktopkey using control down"
}
alias switchdesktop=switchdesktop

usage: switchdesktop 5

AsTeR

Posted 2011-12-14T18:53:00.363

Reputation: 159