MacOS – How to iterate items of the window with dynamic title

applescriptmacosscript

I am trying to get all rows from a table of the application.

set ProcName to "AppName"
tell application ProcName to activate
tell application "System Events"
    tell process ProcName
        tell tab group 1 of window 1
            set items to every row of table 1 of scroll area 1
            set itemValues to {}
            repeat with aRow in items
                set itemValue to (value of text field 1 of aRow)
                log itemValue
                set the end of itemValues to itemValue
            end repeat
        end tell

    end tell
end tell

However when I run the script it prints out only several items because the window title is constantly changing and I get the following error.

 "System Events got an error: Can’t get window \"App name window title"

Is it possible to tell the script to use other identifiers other than title?
Or please suggest how to solve the issue.

Thank you.

Best Answer

The simplest way is using the Application's bundle identifier. This can be found from reading the Info.plist file within the application package, or with some simple AppleScript such as:

get id of application "TextEdit"

This returns com.apple.TextEdit.

Then, one could run the following AppleScript:

tell application id "com.apple.TextEdit"
    activate
end tell

So, your revised could would look like this (note use of process id since you're using System Events).

-- set bundleId to id of application "AppName"
set bundleId to "com.apple.TextEdit"

tell application id bundleId to activate
tell application "System Events"
    repeat with theProcessId in (processes whose bundle identifier is bundleId)
        tell process id (id of theProcessId)
            tell tab group 1 of window 1
                set items to every row of table 1 of scroll area 1
                set itemValues to {}
                repeat with aRow in items
                    set itemValue to (value of text field 1 of aRow)
                    log itemValue
                    set the end of itemValues to itemValue
                end repeat
            end tell
        end tell
    end repeat
end tell