Search code examples
macosapplescriptindex-error

How can I bypass an index error or have the script run again?


I am encountering an index error that appears when the app in use has an overlay or notification appear. To provide a better description, the app will occasionally show an alert if something needs to be acknowledged or dismissed. When that happens, the script is unable to return the value from the designated location in the GUI, and returns the following error message: "Can’t get group 4 of toolbar 1 of window 1 of process "App I'm Using". Invalid index.System Events got an error: Can’t get group 4 of toolbar 1 of window 1 of process "App I'm Using". Invalid index. (-1719)"

The behavior is expected, but I would like to adjust the script to where it will either delay trying again for 30 seconds or so, or just not display said error at all.

I've been toying around with using an 'on error' statement, but I can't get it to take with the 'tell' statement that it's referring to, for example:

    on error error_message number error_number
        if error_number = -1719 then
            wait 30
        end if

I'm unsure of how I can use the 'on error' function with the section of the script below, but if I can make it try again in 30 - 45 seconds without displaying an error, it would be perfect.

on idle
    -- Update the status item's text here.
    tell application "System Events"
        if not (exists process appName) then
            display alert "Application " & appName & " is not running" as warning giving up after 6
            quit me
        end if
        tell process appName
            tell first window's first toolbar's fourth group's first group's first menu button
                set activityState to first item of (value as list) as text
            end tell
        end tell
    end tell
end idle

I believe the error is encountered when the script reaches "tell window's first toolbar's fourth group's..." before it is supposed to "set activityState to first item...".

I have used the 'on error' function with 'try' statements successfully, but I'm having issues moving forward with this one.


Solution

  • Or you can try this approach which will remain in the repeat loop until first window's first toolbar's fourth group's first group's first menu button becomes available.

    on idle
        -- Update the status item's text here.
        tell application "System Events"
            if not (exists process appName) then
                display alert "Application " & appName & " is not running" as warning giving up after 6
                quit me
            end if
            tell process appName
                repeat until exists of first window's first toolbar's fourth group's first group's first menu button
                    delay 0.2
                end repeat
                tell first window's first toolbar's fourth group's first group's first menu button
                    set activityState to first item of (value as list) as text
                end tell
            end tell
        end tell
    end idle