Referencing modal pop-up windows while GUI scripting

how-to

(Tim Burton) #1

While running a script, an un-named modal pop-up dialog box appears with two buttons “Cancel” and “Remove Now”. I would like to programmatically choose the latter but cannot find a way to reference the pop-up box. Issuing this command doesn’t work:

click button “Remove Now” of front window*


(Ray Robertson) #2

Tim,

If doing GUI scripting, it is extremely worthwhile to invest in UIBrowser:

http://pfiddlesoft.com/uibrowser/

It is US $55 and has a trial version. It will show you live if that dialog supports UI scripting, and if so it will even generate the syntax you need to reference the “Remove Now” button.

It’s well worth the time to try it out. Otherwise, we are guessing in the dark because of the way UI elements can vary.

The other easier route—if you only need to make this script work on your own system—would be a macro program which supports AppleScript. I haven’t used one in a long time, but I believe Keyboard Maestro is the preferred one now.

FWIW, I will have a short section on UI scripting, including using UI browser, at this upcoming training event: https://www.cmddconf.com

Nope—Safari scripting will not be covered. Script Debugger? Oh, yeah…I’ll do my best.

Ray


(Tim Burton) #3

Ray: thanks for the info. The conference sounds interesting but it’s 3000 miles away! Anyway, I did download UIbrowser and found no useful information from deploying it. Lots of attribute information about the modal box but no way to reference it from the script being written. The modal dialog box’s title is “” (empty).

Still baffled.


(Tim Burton) #4

Hah, figured it out. The dialog box is apparently considered a part of the parent window, so the correct command is simply:

click button “Remove Now”


(Ray Robertson) #5

Glad you figured it out. I’d be surprised if that didn’t show up in UI Browser, but sometimes it is a matter of looking through the hierarchy it presents. Be sure to check the “Highlight” button as you explore, because it does show the path. “Follow Focus” might help as well.

As others here have said, GUI scripting is a last resort, but often persistence pays off. However, timing issues are a challenge. Avoid adding an arbitrary delay to pause a script, hoping that the expected element will show up. Instead, use a repeat and wait until an item “exists” when possible.

Ray


(Tim Burton) #6

Thanks Ray. Very helpful. I started programming in 1962 and since then have written in about 15 languages, but this GUI scripting is the most vexing I’ve ever encountered.

Right now, I have short script working flawlessly in Script Debugger (and Applescript Editor), but failing miserably when attempting to run as an app, presumably because of timing issues. I’ll attempt the repeat/wait technique. Tim


(Ray Robertson) #7

Tim,

Don’t forget to activate (bring app to the front/focus). That can behave differently from an app. You can observe it as the applet runs.

You can also add few display dialogs as a debugging technique to see if things are happening when and where you expect them.

Ray


(Tim Burton) #8

Ray: Heh heh. The script runs fine in Script Debugger, but when I double-click the app, a spinning ball begins and nothing happens for 122 seconds, then the app terminates. A *.hang file is generated which I cannot make sense of. This failure occurs right at the beginning, as a dialog box I inserted there doesn’t even show up; so the problem isn’t timing, but something more fundamental.

Here’s the entire script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Safari" to activate
set deCookie to {"nytimes.com", "wired.com", "statesmanjournal.com"}

tell application "System Events"
    tell process "Safari"
        -- go to Preferences… 
        click menu item 4 of menu 1 of menu bar item 2 of menu bar 1
        delay 1
        tell window 1
            click button "Privacy" of toolbar 1
            delay 3
            -- go to Manage Website Data:
            click button 1 of group 1 of group 1
            
            repeat with d in deCookie
                set value of text field 1 of sheet 1 to d
                delay 2
                if row 1 of table 1 of scroll area 1 of sheet 1 exists then
                    select row 1 of table 1 of scroll area 1 of sheet 1
                    click button "Remove All" of sheet 1
                    click button "Remove Now"
                end if
             end repeat

        end tell

(Tim Burton) #9

Oh oh; that copy-and-paste didn’t work well; I don’t know how to upload the whole script; this website won’t allow it.


(Shane Stanley) #10

You should precede the code with three tickmarks (`) on a line of their own, and end with the same thing. I’ll edit your post accordingly.


(Tim Burton) #11

OK: here’s the script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Safari" to activate
set deCookie to {"nytimes.com", "wired.com", "statesmanjournal.com"}

tell application "System Events"
    tell process "Safari"
        -- go to Preferences… 
        click menu item 4 of menu 1 of menu bar item 2 of menu bar 1
        delay 1
        tell window 1
            click button "Privacy" of toolbar 1
            delay 3
            -- go to Manage Website Data:
            click button 1 of group 1 of group 1
            
            repeat with d in deCookie
                set value of text field 1 of sheet 1 to d
                delay 2
                if row 1 of table 1 of scroll area 1 of sheet 1 exists then
                    select row 1 of table 1 of scroll area 1 of sheet 1
                    click button "Remove All" of sheet 1
                    click button "Remove Now"
                end if
             end repeat

        end tell

        click button "Done" of sheet 1 of window 1
        delay 2
        tell window 1
            click button 1
        end tel
    end tell
end tell

(Tim Burton) #12

Hmmm. Still didn’t copy and paste correctly.


(Shane Stanley) #14

Tick marks (the key that is also ~), not single quote marks. I have fixed it now – no need to re-post.


(Ray Robertson) #15

Tim,

Your script works for me as an app—once I added the script app to the list of apps allowed to control my computer under System Prefs>Security & Privacy>Privacy>Accessibility.

It sounds like you are getting standard 2-minute AppleScript time-out. Any chance you are missing a dialog about a “not allowed assistive access” or “grant access”? There are actually at least two types of dialogs which can appear because of macOS security, and they might be layered behind something.

Short story: compiled scripts you run from an editor are trusted. Save it as an app and suddenly it is not trustworthy, even on your own Mac.

Ray


(Tim Burton) #16

I’ve always granted access via System Prefs->Security->Privacy->Privacy->Accessibility so that isn’t the problem. And, to be sure, after each fresh compilation into an app, I do the access permissions anew.


(Tim Burton) #17

The mystery deepens. I copied the app over to an older MacBook, gave access perms and tried to run it; result was an error dialog saying OSStatus -192, which I looked up at OSStatus.com and found it to be a CarbonCore resNotFound.

As a test of whether ALL apps fail, I wrote an app that trivially opens a new TextEdit document and found it runs just fine on both machines.


(Ed Stockly) #18

What version of Mac OS are you running?

–> Script Debugger 7.0.3 (7A55)

–> Mac OS 10.13.6 (17G65)


(Tim Burton) #19

OS X 10.13.6 on a MacBook Pro


(Ray Robertson) #20

Tim,

I have no idea, so I can only start guessing. Two things to try:

  • Remove the first two lines since your script doesn’t use AppleScriptObjC.

  • Change both tell statements to the more modern ID form:

tell application id "com.apple.Safari" to activate

tell application id "com.apple.systemevents"
   --code here
end tell

Ray


(Tim Burton) #21

Many thanks, Ray. I’ll try both suggestions but meanwhile, I’ve discovered that although several applescript apps have been successfully given permissions in the system prefs/accessibility dialog, it won’t accept any new apps! Thus, your suggestions will need to wait while this new problem is solved. This isn’t a problem with my older mac running 10.11.6. Clearly, there’s something wrong with the accessibility feature of High Sierra on my 10.13.6