Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript get title of a window

I try to get the title of an app (and copy it as a var) Here is my code so far but this is failing

tell application "app"
    activate
end tell

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

Result :

error "app got an error: every window doesn’t understand the “count” message." number -1708 from every window

like image 909
Kevin Avatar asked Sep 26 '22 08:09

Kevin


1 Answers

The error message suggests that the frontmost application is not scriptable.

The only way to target a non-scriptable application's windows is via the properties of [application] process objects from the System Events context - not via its application object (only scriptable application objects have windows elements):

tell application "app"
    activate
end tell

tell application "System Events"
    # Get the frontmost app's *process* object.
    set frontAppProcess to first application process whose frontmost is true
end tell

# Tell the *process* to count its windows and return its front window's name.
tell frontAppProcess
    if count of windows > 0 then
       set window_name to name of front window
    end if
end tell
like image 112
mklement0 Avatar answered Oct 20 '22 13:10

mklement0