MacOSX: get foremost window title

前端 未结 3 1126
忘掉有多难
忘掉有多难 2020-12-24 03:57

I am using this code to get the window title:

tell application \"System Events\"
    set frontApp to name of first application process whose frontmost is tru         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 04:17

    Building off of Albert's answer, I'd instead do

    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}
    

    This is a hack and I have no experience, but the advantage is that it doesn't crash if there's no window.

提交回复
热议问题