使用applescript关闭系统对话框

时间:2015-04-09 21:19:01

标签: applescript

有没有办法从applescript上点击系统对话框上的OK按钮?目前我已经完成了我的所有工作流程,只关闭了该对话框部分。 对话框出现在Safari应用程序上(因为我让我的脚本在Safari上工作)按钮"停止提醒"点击通过Javascript函数。它是破坏性操作的确认对话框(停止提醒)。

enter image description here

clickId("stop reminders button id") --clicking on button I need
delay 2 -- making sure that dialog has enough time to appear
pressEnterButton() --just trying to close it

to clickId(theId)
    tell application "Safari"
        do JavaScript "document.getElementById('" & theId & "').click();" in document 1
    end tell
end clickId

to pressEnterButton()
    tell application "System Events"
        keystroke return
    end tell
end pressEnterButton

这就是我现在尝试这样做的方式,但它并不是那样有效的(很难过,因为当我按下"在键盘上输入"它可以正常工作并驳回对话)。

2 个答案:

答案 0 :(得分:3)

试试这个:

to pressEnterButton()
   tell application "Safari" to activate
   tell application "System Events"
      tell application process "Safari"
          keystroke return
      end tell
  end tell
end pressEnterButton

答案 1 :(得分:0)

@ShooTeKo关于将代码的对话框部分包装在

中有一个很好的观点

ignoring application responses阻止

这适用于您现有的代码。

我使用www.w3schools

中经过调整的html-javascript代码段进行了测试

(我在按钮上添加了一个id。)

要测试的html页面是:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button id="myBtn" type="button"" onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var person = prompt("Please enter your name", "Harry Potter");

    if (person != null) {
        document.getElementById("demo").innerHTML =
        "Hello " + person + "! How are you today?";
    }
}
</script>

</body>
</html> 

你调整了代码。

clickId("myBtn") --clicking on button I need

delay 2 -- making sure that dialog has enough time to appear
pressEnterButton() --just trying to close it

to clickId(theId)
    tell application "Safari"
        ignoring application responses
            activate

            do JavaScript "document.getElementById('" & theId & "').click()" in document 1
        end ignoring
    end tell
end clickId

to pressEnterButton()
    tell application "System Events"
        keystroke return
    end tell
end pressEnterButton