2

I am using AppleScript with Google Chrome to automate a tedious process on a website. In certain conditions, the website has an alert popup to notify the user of an issue. This prevents the rest of the automator script from completing. I need to detect this popup, log it to a file (I already have this figured out), and click the ok button to continue on. The code that I currently have looks like this:

on run args
repeat with catNum in args
    tell application "Google Chrome"
        set myurl to "http://database.com/whatever"
        open location myurl
        delay 1
        tell active tab of window 1
            -- Click the button that needs to be clicked
            execute javascript "document.getElementById('verbatimCoordinatesDiv').getElementsByTagName('div')[0].getElementsByTagName('a')[0].click()"

            -- What I need to do:
            -- If (popup alert) then
                -- do shell script "echo Issue with " & catNum & " > templog.txt"
                -- Click ok button on popup
            -- else
                -- This clicks the save button
                execute javascript "document.getElementById('editButtonDiv').getElementsByTagName('input')[0].click()"
            -- end if

            delay 1
        end tell
        delay 1
        -- Close the tab
        delete tab (active tab index of window 1) of window 1
    end tell
end repeat
end run

So far what I have works fine when there is no popup message, however it requires manual intervention when a popup is triggered. Below is a screenshot of the popup window and Accessibility Inspector window.

Accessibility Inspector Window and Popup Window

EDIT: Here is a stripped down version of what I am trying to do, but on a public site. It appears as though the javascript alert is preventing anything from happening in the browser until OK is manually clicked.

tell application "Google Chrome"
    -- Go to the website with the javascript button
    set myurl to "http://t4t5.github.io/sweetalert/"
    open location myurl
    delay 1
    tell active tab of window 1
        -- Click the normal javascript button on the page
        execute javascript "document.getElementsByTagName('button')[1].click()"
    end tell
    -- Delete the tab when done
    delete tab (active tab index of window 1) of window 1
end tell
1

You can use a function on window.alert to intercept any alert and to cancel this alert, use a variable to see if there was an alert attempt.

Like this :

tell application "Google Chrome"
    -- Go to the website with the javascript button
    set myurl to "http://t4t5.github.io/sweetalert/"
    open location myurl
    delay 1
    tell active tab of window 1
        -- Click the normal javascript button on the page
        set popupAlert to execute javascript "var isAlert=false; (function() {window.alert = function() {isAlert=true; return;};})(); document.getElementsByTagName('button')[1].click(); isAlert"
        if popupAlert = "true" then -- This page has attempted to display an alert.
            do shell script "echo 'Issue with github.io' > templog.txt"
        else -- no alert, continue
            -- This clicks the save button
        end if
    end tell
    -- Delete the tab when done
    delete (get active tab of window 1)
end tell
0

You can use either the AppleScript Accessibility Inspector or some variant of this script to get the name of the dialog window, or the text in it or some other identifying characteristic. If your script finds that info, it means the dialog has popped up, and you can take action.

-- Entire Contents Demo - mini
-- BP ages ago or so

-- This'll get all the controls and structures associated with an App's window and menus
-- In a form which is easily pasteable into your own scripts
-- and show them in the result pane below.
--
-- Copy that into a text editor and change commas to returns to get an easily readable list.
--
-- The script can take a long time if there are LOTS of window items, such as
-- in the "music" pane of iTunes. It may even time out if you have a huge iTunes library
-- The script'll process most App's UI structures in under a minute

set appname to "System Preferences" -------------------------- Set this to the App you want to look at

set winstuff to "defaultval"
set menustuff to "defaultval"

tell application appname
activate
end tell

tell application "System Events"
tell process appname
set winstuff to entire contents of front window
set menustuff to entire contents of menu bar 1
end tell
end tell
--return winstuff & "rrrr" & menustuff -- comment this out to get just winstuff
return winstuff -- comment this out too to get just menustuff
--return menustuff 
  • So I have used Accessibility Inspector to try to find information on the window, however it has a blank window name, and I cant figure out how to focus on it. I have edited my original question with a screenshot of the popup window and Accessibility Inspector window. – brody painter Sep 4 '15 at 14:47
  • You should be able to test for existence of static text "The page at...", or even an "OK" button. Either of those will let you know that the dialog is up. Pseudocode: "If exists "AXButton: "OK" then.... – Wayfaring Stranger Sep 4 '15 at 15:12
  • I have tried a couple of things now to both look for the window, and to try to select it and click the button, however it seems that the window is blocking the AppleScript from running? It doesnt make sense to me but when I run my script, it hangs until i manually click the OK button, regardless of what I am trying to do. Is this something that is common? – brody painter Sep 4 '15 at 15:37
  • Try going in through the AX interface: Tell app Sys events Tell process appname click value of attribute "AXDefaultButton" See here for an example: discussions.apple.com/thread/3921091?tstart=0 Apple has made some of these structures opaque in the name of security, but I don't think they've gotten down to OK buttons in Google products yet. If you're running your script as an App be sure it has access permission through Security prefs. – Wayfaring Stranger Sep 4 '15 at 16:06
  • I'm starting to think this is not possible. This discussion link mentions that javascript is UI blocking. I'll edit my post with an AppleScript that does essentially what I am doing, but on a public site that everyone can use. – brody painter Sep 4 '15 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .