2

In a particularly 'pesky' website, a dialog box pops up to block (or cover) automated entry of the userid input box.

I have used the following code attempting to get a reference & to close this window, but have been unsuccessful.

tell application "System Events"
    key code 15 using {command down}
    key code 36        // return key

end tell

tell application "System Events" to tell process "Safari"
    set numSheets to (count of sheets) & " sheets" as text       // 0 sheets
end tell

set numDocuments to (count of documents) & " documents" as text  // 1 documents
set numWindows to (count of windows) & " windows" as text        // 1 sheets

What else is available to find and close that alert box?

Edit: The key code 36 will sometimes close the alert, and other times, when it's not present, will submit the page too early.

6
  • 1
    Is the "dialog box" being rendered by the OS or the browser? Stated another way, can you drag the "dialog box" out of the browser window? Commented Jul 18, 2014 at 21:15
  • Great question. It can be pulled outside the confines of the Safari window. I have found the alert message in code in an included .js file on the page.
    – David C
    Commented Jul 18, 2014 at 21:25
  • So you are visiting a website that has an input field for userid but the website is launching an alert window over the input field to obscure it? Commented Jul 18, 2014 at 21:29
  • Yes. Intermittently. The alert is saying that some part of a java applet did not load.
    – David C
    Commented Jul 18, 2014 at 21:32
  • 1
    So if you close the alert window you can then log in okay? Have you tried to contact the people who maintain the website to suggest they fix it? Commented Jul 18, 2014 at 21:36

1 Answer 1

4

If you had an alert dialog appearing with an OK button, then this AppleScript code would close it.

tell application "System Events" to tell process "Safari"
    if exists (button "OK" of window 1) then
        click (button "OK" of window 1)
    end if
end tell

You'll likely need to customise this to fit your exact situation. If the alert dialog appears with a title you can all specify that as below.

tell application "System Events" to tell process "Safari"
    if exists (button "OK" of window "Java applet missing") then
        click (button "OK" of window "Java applet missing")
    end if
end tell

Another option would be to identify the alert by using "front window" as in the following example (assuming the alert is the front window).

tell application "System Events" to tell process "Safari"
    if exists (button "OK" of front window) then
        click (button "OK" of front window)
    end if
end tell

Hope this helps.

5
  • Awesome. Thank you. I will play with that. What if the alert has NO title?
    – David C
    Commented Jul 18, 2014 at 21:53
  • Assuming you only have one Safari window open, then the first example where you just specify the index of the window should work. Commented Jul 18, 2014 at 21:54
  • The first example works. Could the number of windows always = 1 (previously) because the question above (count of windows) is inside tell application "Safari" ?
    – David C
    Commented Jul 18, 2014 at 22:02
  • The number of windows will change. It is simply what it seems, a count of the open windows. So if you have one window open, then "count of windows" will return 1. If an alert window opens, then "count of windows" will return 2. Note that the windows are indexed from zero. So the first window is 0. The second is 1. And so forth... Commented Jul 18, 2014 at 22:11
  • 1
    I've updated the answer with another option ("front window") for identifying the alert window. Commented Jul 18, 2014 at 22:56

You must log in to answer this question.

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