How to use applescript to click a button in a modal window

When I try to close a Safari page, sometimes a popup (a modal window?) shows up asking if I want to "Stay on Page" or "Leave Page". I'm trying to write an applescript to look for such a window and click "Leave Page". I have trouble referencing the popup. Is it treated as a window in Safari? I asked for a count of number of windows and applescript doesn't seem to include this modal window in the count. I tried something like:


tell application "Safari"

set theWindows to every window

repeat with theCurrentWindow in theWindows

if(modal of theCurrentWindow) then

-- appropriate action

end if

end repeat

end tell

end repeat


it looks like "modal of theCurrentWindow" always returns false. So I'm guessing "theWindows" doesn't include the popup. How do I reference the popup so that I can perform some action on it?

Posted on Jun 20, 2014 4:04 PM

Reply
6 replies

Jun 22, 2014 5:51 PM in response to sunnyabhi87

Thought I would send along some more code to see if it helps with your modal detection. It recognizes a JavaScript alert dialog launched within Safari.


User uploaded file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Modal Test</title>
</head>
<body>
<form name=myform>
<input type=button value=“Launch Modal"
onClick="alert(‘Me Modal!')">
</form>
</body>
</html>

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to use applescript to click a button in a modal window

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.