有没有办法在Mac OS X上使用Python在给定时间找到当前活动窗口的应用程序名称?
参考之前提出的问题,我想知道如何获取当前活动文档的标题.
我在上面问题的答案中尝试了脚本.这有效,但只给出了应用程序的名称.例如,我正在写这个问题:当我启动脚本时它会给我应用程序的名称,即"Firefox".这很整洁,但并没有真正帮助.我想要捕获我当前活动文档的标题.看图像.
Firefox标题http://img.skitch.com/20090126-nq2egknhjr928d1s74i9xixckf.jpg
我正在使用Leopard,因此不需要向后兼容性.我也使用Python的Appkit来访问NSWorkspace类,但如果你告诉我Objective-C代码,我可以找到Python的翻译.
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
Run Code Online (Sandbox Code Playgroud)
保存为脚本并使用shell中的osascript调用它.