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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With