我正在使用此代码来获取窗口标题:
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
set window_name to name of front window
end tell
但是,这在某些情况下失败。显然,当没有打开的窗口时,它会失败,但是可以。但是,在某些情况下,例如Texmaker,它会失败并显示错误。它也不适用于预览。
无论如何,即使对于像Texmaker这样的情况,也将获得窗口标题的方法是什么?
最佳答案
这似乎总是可以工作:
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
return {frontAppName, windowTitle}
从here得到了这个主意。
关于MacOSX : get foremost window title,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5292204/