Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 832
georgebrock Avatar asked Feb 16 '09 19:02

georgebrock


2 Answers

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.

like image 173
Andrew Grant Avatar answered Oct 21 '22 06:10

Andrew Grant


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

like image 30
Shabble Avatar answered Oct 21 '22 06:10

Shabble