georgebrock
georgebrock

Reputation: 30053

How to detect which Space the user is on in Mac OS X Leopard?

Mac OS X Leopard has a virtual desktop implementation called Spaces. I want to programatically detect which space the user is currently on.

Cocoa is preferable but AppleScript is acceptable if there's no other way.

I've seen a couple of AppleScript implementations, but the techniques they used seemed a bit too hacky to use in production code (one relied on causing an error and then parsing the error message to get the current space, the other interrogated the Spaces menu GUI)

Upvotes: 5

Views: 3206

Answers (2)

Shabble
Shabble

Reputation: 592

If anyone still cares, I've put together a little commandline util based on the answers here which you can get at http://github.com/shabble/osx-space-id

Upvotes: 9

Andrew Grant
Andrew Grant

Reputation: 58796

Use HIWindowGetCGWindowID to get the CGWindowID for your WindowRef.

Use that ID with CGWindowListCreateDescriptionFromArray from CGWindow.h to get information on your window.

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

Upvotes: 15

Related Questions