使用 applescript 关闭系统对话框

Close system dialog with applescript

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

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

这就是我现在尝试这样做的方式,但它不是那样工作的(很遗憾,因为当我在键盘上按 "enter" 时,它会正常工作并关闭对话框)。

试试这个:

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

@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