macos OSX Lion AppleScript:如何从任务控制中获取当前空间#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6768684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OSX Lion AppleScript : How to get current space # from mission control?
提问by Mark Kahn
I'm trying to figure out how to get the current space # from mission control. Source would be helpful, but more helpful would be info on how to figure this out myself. I've written a few applescripts, but more often than not it seems like any time I need to do something new (that I can't find dictionary documentation for) it falls under the category of "tell this specific app (e.g. "System Events") this very specific thing" and I've no clue how I would actually figure that out.
我想弄清楚如何从任务控制中获取当前空间。来源会有所帮助,但更有用的是有关如何自己解决这个问题的信息。我写了一些苹果脚本,但似乎任何时候我需要做一些新的事情(我找不到字典文档)它属于“告诉这个特定的应用程序(例如“系统事件”)这个非常具体的事情”,我不知道我实际上会如何解决这个问题。
Specifically what I am trying to do:
特别是我正在尝试做的事情:
I hatethe new mission control in OSX 10.7. I want my spaces "grid" back since I used it all the time. I used to navigate between spaces using arrow keys (e.g. ALT+↑) every few seconds. Now I'm stuck with this clunky 1x9 array of spaces instead of an elegant 3x3 grid. I've re-mapped all my spaces to use the number pad, which partially takes care of the problem (since it is a 3x3 grid), but only when I have an external keyboard attached.
我讨厌OSX 10.7 中的新任务控制。我希望我的空间“网格”回来,因为我一直在使用它。我曾经每隔几秒钟使用箭头键(例如ALT+ ↑)在空格之间导航。现在我坚持使用这个笨重的 1x9 空间阵列,而不是优雅的 3x3 网格。我重新映射了所有空间以使用数字键盘,这部分解决了问题(因为它是一个 3x3 网格),但仅当我连接了外部键盘时。
Basically, I want to be able to use ALT+↑and ↓again, but to do so I need to detect the current space # so that I can switch from space 5-->2, for example.
基本上,我希望能够用ALT+↑和↓了,但这样做,我需要检测当前空间#,这样我可以从太空中5开关- > 2,例如。
Dave's answer below, although far more detailed than I expected, requires writing an app to do this (plus it still doesn't fully answer the question). If it's at all possible, I'd rather just bind a few keys to an applescript.
下面戴夫的回答虽然比我预期的要详细得多,但需要编写一个应用程序来执行此操作(而且它仍然没有完全回答问题)。如果可能的话,我宁愿将几个键绑定到一个applescript。
采纳答案by Dave Gallagher
I'm trying to figure this out myself. Not there yet, but in the right direction:
我正在尝试自己解决这个问题。还没有,但方向正确:
- Each Mission Control "space" gets a
uuid
assigned to it... - ...except for the very first one (AFAIK), and the Dashboard one.
- 每个任务控制“空间”都被
uuid
分配给它...... - ...除了第一个(AFAIK)和仪表板。
You can read them here:
你可以在这里阅读它们:
$ defaults read com.apple.spaces
$ defaults read com.apple.desktop
File locations:
文件位置:
~/Library/Preferences/com.apple.spaces.plist
~/Library/Preferences/com.apple.desktop.plist
Here's mine. I have four spaces enabled, and three entries show up:
这是我的。我启用了四个空格,并显示三个条目:
$ defaults read com.apple.spaces
{
spaces = (
{
type = 0;
uuid = "9F552977-3DB0-43E5-8753-E45AC4C61973";
},
{
type = 0;
uuid = "44C8072A-7DC9-4E83-94DD-BDEAF333C924";
},
{
type = 0;
uuid = "6FADBDFE-4CE8-4FC9-B535-40D7CC3C4C58";
}
);
}
If you delete a space, that entry will get removed from the file. If you add a space, an entry will be added. Again, there's never an entry for Desktop 1 or Dashboard.
如果您删除一个空格,该条目将从文件中删除。如果添加空格,则会添加一个条目。同样,Desktop 1 或 Dashboard 从来没有条目。
I'm not sure if there's a public API to figure out what space uuid
is being displayed on a display. I'd assume that no uuid
means Display 1, and the others' mean Display 1+n.
我不确定是否有公共 API 来确定uuid
显示器上显示的空间。我假设 nouuid
意味着 Display 1,而其他人的意思是 Display 1+n。
I took a quick glance through the AppleScript Editor Library (Window ---> Library) and didn't see any entries under System Events for spaces
. This is probably something that can be done with Cocoa, perhaps via private API, but I'm not sure about AppleScript.
我快速浏览了 AppleScript 编辑器库(窗口 ---> 库),在系统事件下没有看到任何条目spaces
。这可能是可以用 Cocoa 完成的,也许是通过私有 API 完成的,但我不确定 AppleScript。
UPDATE - July 23, 2011
更新 - 2011 年 7 月 23 日
It looks like Dock controls Mission Control. You can grab its header files like so:
它看起来像 Dock 控制任务控制。你可以像这样获取它的头文件:
- Go to:
/System/Library/CoreServices/Dock
- Right-Click and Show Package Contents
- Navigate:
/Contents/MacOS/
- Copy and paste the
Dock
binary to your desktop. - Run:
$class-dump ~/Desktop/Dock
- 去:
/System/Library/CoreServices/Dock
- 右键单击并显示包内容
- 导航:
/Contents/MacOS/
- 将
Dock
二进制文件复制并粘贴到桌面。 - 跑:
$class-dump ~/Desktop/Dock
That'll spit out all of its header files (it's long; almost 7,500 lines). You can see the spaceUUID
strings appearing in there. There's a class called WVSpace
which appears to represent a single Space in Mission Control, and a lot of other WV* classes.
这将吐出它的所有头文件(它很长;将近 7,500 行)。您可以看到spaceUUID
出现在那里的字符串。有一个名为的类WVSpace
,它似乎代表 Mission Control 中的单个 Space,以及许多其他 WV* 类。
I'll keep looking at it tomorrow; too tired now. :)
明天继续看;现在太累了。:)
UPDATE - July 24, 2011
更新 - 2011 年 7 月 24 日
Inside Dock there's a class called WVSpaces
. It has a number of attributes including:
在 Dock 中有一个名为WVSpaces
. 它有许多属性,包括:
WVSpace *currentSpace;
unsigned int currentWorkspace;
WVSpace *nextSpace; // Space on the right???
WVSpace *previousSpace; // Space on the left???
BOOL currentSpaceIsDashboard;
BOOL dashboardIsCurrent;
...lots more...
Each WVSpace
class has an NSString *_uuid;
attribute, which is likely its SpaceUUID. So theoretically you can get the current space number like so:
每个WVSpace
类都有一个NSString *_uuid;
属性,可能是它的 SpaceUUID。所以理论上你可以像这样获得当前的空间编号:
WVSpace *currentSpace = [[WVSpaces sharedInstance] currentSpace];
NSString *currentSpaceUUID = [currentSpace _uuid]; // Empty string if main space???
The trick is, how to get access to the private WVSpaces
class buried inside of Dock? I'm assuming it's Singleton as it has an NSMutableArray *_spaces;
attribute, probably with every space listed in it. Only one space gets displayed at a time (this holds true if you're using multiple monitors; the space spans across both of them), so it makes sense to only have one WVSpaces
instance.
诀窍是,如何访问WVSpaces
埋在 Dock 中的私有类?我假设它是 Singleton,因为它有一个NSMutableArray *_spaces;
属性,其中可能列出了每个空格。一次只显示一个空间(如果您使用多台显示器,这也是正确的;空间跨越它们),因此只有一个WVSpaces
实例是有意义的。
So it looks like it'll require some SIMBLhacking of Dock to gain access to WVSpaces
.
所以看起来它需要对 Dock进行一些SIMBL黑客攻击才能访问WVSpaces
.
回答by LaC
I've been poking around, and I came up with this: https://gist.github.com/1129406
我一直在摸索,我想出了这个:https: //gist.github.com/1129406
Spaces have a nonsequential ID and a sequential index (0-based). You can get the ID in two ways:
空间具有非顺序 ID 和顺序索引(从 0 开始)。您可以通过两种方式获取 ID:
- from public APIs (see
get_space_id
) - from the private CGS API
CGSGetWorkspace
- 来自公共 API(请参阅
get_space_id
) - 来自私有 CGS API
CGSGetWorkspace
You can set the current space by index using public APIs (though the notifications themselves are not publicly documented): see set_space_by_index
您可以使用公共 API 通过索引设置当前空间(尽管通知本身没有公开记录):请参阅 set_space_by_index
You can set the current space by ID using private the CGS API CGSSetWorkspace
.
您可以使用私有的 CGS API 通过 ID 设置当前空间CGSSetWorkspace
。
You cannotget the current space index directly. However, if you're always using the same set of nine spaces, you can rotate through them once using set_space_by_index
, collect their IDs, and build a mapping. Then you will be able to get the current index from the ID.
你不能直接获得当前的空间索引。但是,如果您始终使用相同的一组九个空格,则可以使用 轮换它们一次set_space_by_index
,收集它们的 ID 并构建映射。然后您将能够从 ID 获取当前索引。
回答by Janus
... also been working on this :)
...也一直在努力:)
You say that you "need to to detect the current space #". This is not strictly true: To move down one row, you just move 3 spaces right, so in principle you could just bind something like
你说你“需要检测当前的空间#”。这不是严格正确的:要向下移动一行,您只需向右移动 3 个空格,因此原则上您可以绑定类似
tell application "System Events" to tell process "WindowServer"
key code {124, 124, 124} using control down
end tell
to Alt-down (with FastScripts, Alfredor some other fast method that avoids the overhead of Automator). This approach will fail if you ever hit down in the bottom row, of course -- but if you are truly hard-wired, you never do :)
You have to "Enable access for assistive devices" in the Universal Access preference pane for the key code
approach to work.
到 Alt-down(使用FastScripts、Alfred或其他一些避免 Automator 开销的快速方法)。当然,如果您在最下面的行中按下,则此方法将失败——但如果您真的是硬连线,则永远不会这样做:)
您必须在 Universal Access 首选项窗格中“启用辅助设备的访问”key code
工作方法。
Caveat: This doesn't work. When I launch the script above, I nicely jump three spaces. The problem is that afterwards my keyboard goes unresponsive: It seems that only the window manager is receiving events: I can close windows and switch space, but I cannot interact with any applications.
My theory is that this happens when the jump causes the current application to change during the execution of the script -- but I have no idea how to fix this.
警告:这不起作用。当我启动上面的脚本时,我很好地跳了三个空格。问题是之后我的键盘没有响应:似乎只有窗口管理器正在接收事件:我可以关闭窗口和切换空间,但我无法与任何应用程序交互。
我的理论是,当跳转导致当前应用程序在脚本执行期间发生变化时会发生这种情况——但我不知道如何解决这个问题。
A related observation:The Mission Control
(i.e. /Applications/Mission Control.app/Contents/MacOS/Mission\ Control
) seems to react to some command line arguments:
一个相关的观察:在Mission Control
(即/Applications/Mission Control.app/Contents/MacOS/Mission\ Control
)似乎对某些命令行参数反应:
Mission\ Control
: show mission controlMission\ Control 1
: show desktopMission\ Control 2
: show current application windows
Mission\ Control
: 显示任务控制Mission\ Control 1
: 显示桌面Mission\ Control 2
: 显示当前应用程序窗口
I tried putting in some of the UUID's from defaults read com.apple.spaces
, but that didn't do much. So much for fumbling in the dark.
我尝试从 中放入一些 UUID defaults read com.apple.spaces
,但这并没有太大作用。在黑暗中摸索这么多。
回答by sdsykes
I wrote an app - does it work for you?
我写了一个应用程序 - 它对你有用吗?
The keys to make it work are control-shift and the arrow keys, although this may be fixable if you are stuck on ALT.
使其工作的关键是 control-shift 和箭头键,尽管如果您被困在 ALT 上,这可能是可以解决的。
Make sure you have 9 spaces (desktops) set up before you start, and you'll need to change the default ctrl-up and ctrl-down key bindings in System Preferences to something else (in Keyboard -> Keyboard Shortcuts -> Mission Control : Mission Control and Show Desktop).
确保在开始之前设置了 9 个空格(桌面),并且您需要将系统偏好设置中的默认 ctrl-up 和 ctrl-down 键绑定更改为其他内容(在键盘 -> 键盘快捷键 -> 任务控制中) :任务控制和显示桌面)。
On the first run it it will cycle through your desktops to enumerate them when you first change space.
在第一次运行时,它将在您的桌面上循环以在您第一次更改空间时枚举它们。
Then you should be able to change between desktops like in a 3x3 grid.
然后您应该能够像在 3x3 网格中一样在桌面之间切换。
There may be a few wrinkles, but it's basically functional, at least for me.
可能有一些皱纹,但它基本上是功能性的,至少对我来说是这样。
回答by David Lio
http://switchstep.com/ReSpaceApp
http://switchstep.com/ReSpaceApp
This works, is free (right now) and is awesome.
这有效,是免费的(现在)并且很棒。
Just be sure to manually create as many spaces as your layout (in preferences) is expecting.
只要确保手动创建与您的布局(在首选项中)期望的一样多的空间。
回答by patcon
I'm on Mountain Lion and this seems to work for me.
我在 Mountain Lion 上,这似乎对我有用。
defaults read com.apple.spaces
defaults read com.apple.spaces
Look for "Current Space". You'll notice that running this command with different active spaces doesn't change the current space BUT if you check and uncheck a checkbox button in "System Preferences" and run it again, you'll see it updated.
寻找“当前空间”。您会注意到,使用不同的活动空间运行此命令不会更改当前空间,但是如果您选中并取消选中“系统偏好设置”中的复选框按钮并再次运行它,您会看到它已更新。
Hopefully this helps someone else!
希望这对其他人有帮助!
EDIT: It's ugly, but I'm using this:
编辑:这很丑陋,但我正在使用它:
killall Dock && sleep 0.2 && defaults read com.apple.spaces | grep -A1 "Current Space" | tail -1 | awk '{print $NF }' | cut -f1 -d';'
killall Dock && sleep 0.2 && defaults read com.apple.spaces | grep -A1 "Current Space" | tail -1 | awk '{print $NF }' | cut -f1 -d';'
回答by Montaldo
on openNewSpace()
tell application "System Events"
—start mission control
do shell script "/Applications/Mission\ Control.app/Contents/MacOS/Mission\ Control"
tell process "Dock"
set countSpaces to count buttons of list 1 of group 1
--new space
click button 1 of group 1
--switch to new space
repeat until (count buttons of list 1 of group 1) = (countSpaces + 1)
end repeat
click button (countSpaces + 1) of list 1 of group 1
end tell
end tell
end openNewSpace