macos 在 Mac OS X 中获取当前活动窗口/文档的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/480866/
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
Get the title of the current active Window/Document in Mac OS X
提问by sebastiangeiger
Refering to a previously asked question, I would like to know how to get the title of the current active document.
参考之前提出的问题,我想知道如何获取当前活动文档的标题。
I tried the script mention in the answers to the question above. This works, but only gives me the name of the application. For example, I am writing this question: When I fire up the script it gives me the name of the application, i.e. "Firefox". This is pretty neat, but does not really help. I would rather like to capture the title of my current active document. See the image.
我尝试了上述问题的答案中提到的脚本。这有效,但只给了我应用程序的名称。例如,我正在写这个问题:当我启动脚本时,它给了我应用程序的名称,即“Firefox”。这很整洁,但并没有真正的帮助。我宁愿捕获当前活动文档的标题。请参阅图像。
Firefox title http://img.skitch.com/20090126-nq2egknhjr928d1s74i9xixckf.jpg
Firefox 标题 http://img.skitch.com/20090126-nq2egknhjr928d1s74i9xixckf.jpg
I am using Leopard, so no backward compatibility needed. Also I am using Python's Appkit to gain access to the NSWorkspace class, but if you tell me the Objective-C code, I could figure out the translation to Python.
我正在使用 Leopard,因此不需要向后兼容。此外,我正在使用 Python 的 Appkit 来访问 NSWorkspace 类,但是如果您告诉我 Objective-C 代码,我可以找出对 Python 的转换。
好的,我有一个不太令人满意的解决方案,这就是为什么我不标记 Koen Bok 的答案。至少现在还没有。
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
Save as script and invoke it with osascript from the shell.
另存为脚本并从 shell 使用 osascript 调用它。
采纳答案by Koen Bok
As far as I know your best bet is wrapping an AppleScript. But AppleScript is magic to me so I leave it as an exercise for the questioner :-)
据我所知,您最好的选择是包装 AppleScript。但是 AppleScript 对我来说很神奇,所以我把它作为提问者的练习:-)
This might help a little: A script to resize frontmost two windows to fill screen - Mac OS X Hints
这可能会有所帮助:调整最前面的两个窗口大小以填充屏幕的脚本 - Mac OS X 提示
回答by sdt
In Objective-C, the short answer, using a little Cocoa and mostly the Carbon Accessibility APIis:
在 Objective-C 中,使用一点 Cocoa 和主要是Carbon Accessibility API的简短答案是:
// Get the process ID of the frontmost application.
NSRunningApplication* app = [[NSWorkspace sharedWorkspace]
frontmostApplication];
pid_t pid = [app processIdentifier];
// See if we have accessibility permissions, and if not, prompt the user to
// visit System Preferences.
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
Boolean appHasPermission = AXIsProcessTrustedWithOptions(
(__bridge CFDictionaryRef)options);
if (!appHasPermission) {
return; // we don't have accessibility permissions
// Get the accessibility element corresponding to the frontmost application.
AXUIElementRef appElem = AXUIElementCreateApplication(pid);
if (!appElem) {
return;
}
// Get the accessibility element corresponding to the frontmost window
// of the frontmost application.
AXUIElementRef window = NULL;
if (AXUIElementCopyAttributeValue(appElem,
kAXFocusedWindowAttribute, (CFTypeRef*)&window) != kAXErrorSuccess) {
CFRelease(appElem);
return;
}
// Finally, get the title of the frontmost window.
CFStringRef title = NULL;
AXError result = AXUIElementCopyAttributeValue(window, kAXTitleAttribute,
(CFTypeRef*)&title);
// At this point, we don't need window and appElem anymore.
CFRelease(window);
CFRelease(appElem);
if (result != kAXErrorSuccess) {
// Failed to get the window title.
return;
}
// Success! Now, do something with the title, e.g. copy it somewhere.
// Once we're done with the title, release it.
CFRelease(title);
Alternatively, it may be simpler to use the CGWindow API, as alluded to in this StackOverflow answer.
或者,使用 CGWindow API 可能更简单,正如StackOverflow answer 中所提到的。