Mac OS X Leopard has a virtual desktop implementation called Spaces. I want to programatically detect which space the user is currently on.
Mac OS X Leopard有一个名为Spaces的虚拟桌面实现。我想以编程方式检测用户当前所在的空间。
Cocoa is preferable but AppleScript is acceptable if there's no other way.
可可是最好的,但如果没有别的办法,AppleScript是可以接受的。
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)
我已经看过几个AppleScript实现,但是他们使用的技术在生产代码中看起来有点过于繁琐(一个依赖于导致错误,然后解析错误消息以获取当前空间,另一个则查询Spaces菜单GUI)
2 个解决方案
#1
Use HIWindowGetCGWindowID to get the CGWindowID for your WindowRef.
使用HIWindowGetCGWindowID获取WindowRef的CGWindowID。
Use that ID with CGWindowListCreateDescriptionFromArray from CGWindow.h to get information on your window.
将该ID与CGWindow.h中的CGWindowListCreateDescriptionFromArray一起使用,以获取有关窗口的信息。
Look at the returned dictionary and extract the (optional) kCGWindowWorkspace key. That is your space ID for your window.
查看返回的字典并提取(可选)kCGWindowWorkspace密钥。这是您窗口的空间ID。
#2
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
如果有人还在乎,我已经根据这里的答案整理了一个命令行工具,你可以在这里获得http://github.com/shabble/osx-space-id
#1
Use HIWindowGetCGWindowID to get the CGWindowID for your WindowRef.
使用HIWindowGetCGWindowID获取WindowRef的CGWindowID。
Use that ID with CGWindowListCreateDescriptionFromArray from CGWindow.h to get information on your window.
将该ID与CGWindow.h中的CGWindowListCreateDescriptionFromArray一起使用,以获取有关窗口的信息。
Look at the returned dictionary and extract the (optional) kCGWindowWorkspace key. That is your space ID for your window.
查看返回的字典并提取(可选)kCGWindowWorkspace密钥。这是您窗口的空间ID。
#2
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
如果有人还在乎,我已经根据这里的答案整理了一个命令行工具,你可以在这里获得http://github.com/shabble/osx-space-id