Подтвердить что ты не робот

MacOSX: получить главное название окна

Я использую этот код, чтобы получить заголовок окна:

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?

4b9b3361

Ответ 1

Кажется, что это работает всегда:

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}

Получил идею здесь.

Ответ 2

Построение ответа Альберта, вместо этого я сделаю

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}

Это взломать, и у меня нет опыта, но преимущество в том, что он не падает, если нет окна.

Ответ 3

Дайте следующую script попытку:

tell application "System Events"
    set window_name to name of first window of (first application process whose frontmost is true)
end tell

Я не проверял, что он работает для TextMaker.