如何告诉Applescript停止执行

jef*_*pan 20 applescript

Applescript的"从列表中选择"用户交互有一个"取消"按钮 - 我希望这个"取消"告诉脚本立即停止执行.换一种说法:

 set wmColor to choose from list {"Black", "Black for all", "White", 
     "White for all"} with prompt "What color should the watermark be?" 
     default items "White for all" without multiple selections allowed and 
     empty selection allowed
 if wmColor is false
     *tell script to stop executing*
 end if
Run Code Online (Sandbox Code Playgroud)

似乎无法找到如何做到这一点 - 有谁知道如何?

小智 30

错误号-128是"用户已取消",并将停止脚本 - 例如:

if wmColor is false then
    error number -128
end if
Run Code Online (Sandbox Code Playgroud)

  • 这仅在未包含在捕获此类错误的try块中时才有效. (3认同)

小智 11

"return"告诉脚本停止执行:

display dialog "Do you want to exit this script?" with icon note buttons {"Exit", "Continue"}
if the button returned of the result is "Exit" then
  return
end if
Run Code Online (Sandbox Code Playgroud)

  • 好吧,不是真的 - 它告诉当前的处理程序返回(没有值).如果你处于脚本的最顶层,那很好,它将结束执行,但如果你在一个处理程序中,它将返回一个nil到任何调用它,脚本将愉快地继续执行(可能意外如果调用者不期望丢失值,则会得到结果). (3认同)