在AppleScript中,如何在按下一个按钮时运行代码,而在按下另一个按钮时运行其他代码?

时间:2018-11-28 12:50:09

标签: applescript

在Apple Script中,我正在制造一个假病毒作为恶作剧,我想知道如何制作它,因此,如果按下一个按钮,则运行某些代码,如果按下另一个按钮,则运行其他代码。 这是我到目前为止的内容:

display alert "Warning: Non-Standard hardware detected in 
core files. Please close all applications to allow 
maximum processing power."
delay 5
display alert "Warning: Non-standard hardware detected in 
core files. Core files may become corrupted."
delay 5
display alert "Non-standard hardware reaching dangerous 
level. If you would like to continue with purge process 
and risk your core files, click OK. If you would like to 
abort the process and leave the hardware, exit now." 
buttons {"Ok", "Cancel"} default button 1

我想要这样,如果用户单击“确定”按钮,则发生一件事,如果用户单击“取消”,则发生另一件事。我没有使用Apple Script的经验,这是我编写的第一个脚本。

编辑:当您双击它时,我还想知道如何使其自动运行,因为现在当我双击它时,Apple Script Editor将打开,我只希望它立即运行。

1 个答案:

答案 0 :(得分:1)

我重新格式化了您的代码,使其更易于使用。根据需要对其进行编辑后,只需将其另存为脚本编辑器中的应用程序即可。然后双击该新应用程序将启动您的脚本……而不是脚本编辑器。

property displayAlert1 : "Warning: Non-Standard hardware detected in 
core files. Please close all applications to allow 
maximum processing power."

property displayAlert2 : "Warning: Non-standard hardware detected in 
core files. Core files may become corrupted."

property displayAlert3 : "Non-standard hardware reaching dangerous 
level. If you would like to continue with purge process 
and risk your core files, click OK. If you would like to 
abort the process and leave the hardware, exit now."

display alert displayAlert1
delay 5
display alert displayAlert2
delay 5
set buttonReturned to button returned of ¬
    (display alert displayAlert3 buttons {"Ok", "Cancel"} default button 1)

if buttonReturned is "Ok" then
    display dialog "Your Computer Will Explode In 1 Minute" buttons {"Cancel", "OK"} ¬
        default button "OK"
else if buttonReturned is "Cancel" then
    display dialog "Your Computer Will Explode In 2 Minutes" buttons {"Cancel", "OK"} ¬
        default button "OK"
end if