How do I determine what space a Mac OS X Leopard user is in?

Mac OS X Leopard has a virtual desktop environment called Spaces. I want to programmatically determine in which space the user is currently located .

Cocoa is preferred , but AppleScript is acceptable if there is no other way.

I saw a couple of AppleScript implementations, but the methods that they used seemed too hacked for use in production code (one relied on the cause of the error and then parsed the error message to get the current space, and the other Spaces menu polled GUI)

+5
source share
2 answers

Use HIWindowGetCGWindowID to get the CGWindowID for your WindowRef.

Use this identifier with CGWindowListCreateDescriptionFromArray from CGWindow.h to get information about your window.

Look at the dictionary returned and extract the key (optional) kCGWindowWorkspace. This is your space identifier for your window.

+15
source

If someone still cares, I put together a small team to use based on the answers you can get at http://github.com/shabble/osx-space-id

+6
source

All Articles