I'm trying to figure out how to get the current space # from mission control. Source would be helpful, but more helpful would be info on how to figure this out myself. I've written a few applescripts, but more often than not it seems like any time I need to do something new (that I can't find dictionary documentation for) it falls under the category of "tell this specific app (e.g. "System Events") this very specific thing" and I've no clue how I would actually figure that out.
Specifically what I am trying to do:
I hate the new mission control in OSX 10.7. I want my spaces "grid" back since I used it all the time. I used to navigate between spaces using arrow keys (e.g. ALT+↑) every few seconds. Now I'm stuck with this clunky 1x9 array of spaces instead of an elegant 3x3 grid. I've re-mapped all my spaces to use the number pad, which partially takes care of the problem (since it is a 3x3 grid), but only when I have an external keyboard attached.
Basically, I want to be able to use ALT+↑ and ↓ again, but to do so I need to detect the current space # so that I can switch from space 5-->2, for example.
Dave's answer below, although far more detailed than I expected, requires writing an app to do this (plus it still doesn't fully answer the question). If it's at all possible, I'd rather just bind a few keys to an applescript.
http://switchstep.com/ReSpaceApp
This works, is free (right now) and is awesome.
Just be sure to manually create as many spaces as your layout (in preferences) is expecting.
... also been working on this :)
You say that you "need to to detect the current space #". This is not strictly true: To move down one row, you just move 3 spaces right, so in principle you could just bind something like
to Alt-down (with FastScripts, Alfred or some other fast method that avoids the overhead of Automator). This approach will fail if you ever hit down in the bottom row, of course -- but if you are truly hard-wired, you never do :)
You have to "Enable access for assistive devices" in the Universal Access preference pane for the
key code
approach to work.Caveat: This doesn't work. When I launch the script above, I nicely jump three spaces. The problem is that afterwards my keyboard goes unresponsive: It seems that only the window manager is receiving events: I can close windows and switch space, but I cannot interact with any applications.
My theory is that this happens when the jump causes the current application to change during the execution of the script -- but I have no idea how to fix this.
A related observation: The
Mission Control
(i.e./Applications/Mission Control.app/Contents/MacOS/Mission\ Control
) seems to react to some command line arguments:Mission\ Control
: show mission controlMission\ Control 1
: show desktopMission\ Control 2
: show current application windowsI tried putting in some of the UUID's from
defaults read com.apple.spaces
, but that didn't do much. So much for fumbling in the dark.I'm trying to figure this out myself. Not there yet, but in the right direction:
uuid
assigned to it...You can read them here:
File locations:
Here's mine. I have four spaces enabled, and three entries show up:
If you delete a space, that entry will get removed from the file. If you add a space, an entry will be added. Again, there's never an entry for Desktop 1 or Dashboard.
I'm not sure if there's a public API to figure out what space
uuid
is being displayed on a display. I'd assume that nouuid
means Display 1, and the others' mean Display 1+n.I took a quick glance through the AppleScript Editor Library (Window ---> Library) and didn't see any entries under System Events for
spaces
. This is probably something that can be done with Cocoa, perhaps via private API, but I'm not sure about AppleScript.UPDATE - July 23, 2011
It looks like Dock controls Mission Control. You can grab its header files like so:
/System/Library/CoreServices/Dock
/Contents/MacOS/
Dock
binary to your desktop.$class-dump ~/Desktop/Dock
That'll spit out all of its header files (it's long; almost 7,500 lines). You can see the
spaceUUID
strings appearing in there. There's a class calledWVSpace
which appears to represent a single Space in Mission Control, and a lot of other WV* classes.I'll keep looking at it tomorrow; too tired now. :)
UPDATE - July 24, 2011
Inside Dock there's a class called
WVSpaces
. It has a number of attributes including:Each
WVSpace
class has anNSString *_uuid;
attribute, which is likely its SpaceUUID. So theoretically you can get the current space number like so:The trick is, how to get access to the private
WVSpaces
class buried inside of Dock? I'm assuming it's Singleton as it has anNSMutableArray *_spaces;
attribute, probably with every space listed in it. Only one space gets displayed at a time (this holds true if you're using multiple monitors; the space spans across both of them), so it makes sense to only have oneWVSpaces
instance.So it looks like it'll require some SIMBL hacking of Dock to gain access to
WVSpaces
.I've been poking around, and I came up with this: https://gist.github.com/1129406
Spaces have a nonsequential ID and a sequential index (0-based). You can get the ID in two ways:
get_space_id
)CGSGetWorkspace
You can set the current space by index using public APIs (though the notifications themselves are not publicly documented): see
set_space_by_index
You can set the current space by ID using private the CGS API
CGSSetWorkspace
.You cannot get the current space index directly. However, if you're always using the same set of nine spaces, you can rotate through them once using
set_space_by_index
, collect their IDs, and build a mapping. Then you will be able to get the current index from the ID.I'm on Mountain Lion and this seems to work for me.
defaults read com.apple.spaces
Look for "Current Space". You'll notice that running this command with different active spaces doesn't change the current space BUT if you check and uncheck a checkbox button in "System Preferences" and run it again, you'll see it updated.
Hopefully this helps someone else!
EDIT: It's ugly, but I'm using this:
killall Dock && sleep 0.2 && defaults read com.apple.spaces | grep -A1 "Current Space" | tail -1 | awk '{print $NF }' | cut -f1 -d';'