MacOSX:获得最重要的窗口标题

Alb*_*ert 11 macos applescript

我正在使用此代码获取窗口标题:

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
Run Code Online (Sandbox Code Playgroud)

但是,在某些情况下,这会失败.显然,当没有打开的窗口时它会失败,但那是好的.但是,在某些情况下,例如Texmaker,它会因错误而失败.它也不适用于预览.

什么是获得窗口标题的方法,即使像Texmaker这样的情况?

Alb*_*ert 19

这似乎始终有效:

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}
Run Code Online (Sandbox Code Playgroud)

这里得到了主意.


小智 5

建立在阿尔伯特的答案之上,我宁愿做

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
    set windowTitle to "no window"
    tell process frontAppName
        if exists (1st window whose value of attribute "AXMain" is true) then
            tell (1st window whose value of attribute "AXMain" is true)
                set windowTitle to value of attribute "AXTitle"
            end tell
        end if
    end tell
end tell

return {frontAppName, windowTitle}
Run Code Online (Sandbox Code Playgroud)

这是一个hack,我没有经验,但是优点是,如果没有窗口,它也不会崩溃。