2011-12-23 89 views
16

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 

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

回答

23

错误号-128是“用户取消”,并停止脚本 - 例如:

if wmColor is false then 
    error number -128 
end if 
+3

这只适用于未包含在捕获此类错误的try块中。 – spyle 2015-02-18 22:11:46

1

我发现这非常适用于在应用程序中运行脚本(例如,InDesign的CS5.5) :

repeat 100 times 
    tell application ("System Events") to keystroke "." using command down 
    delay (random number from 0.5 to 5) 
end repeat 

这是从这个answer修改。

7

“回归”讲述了一个脚本停止执行:

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 
+0

您能否详细说明您的答案,并添加关于您提供的解决方案的更多描述? – abarisone 2015-04-01 12:44:32

+0

在我看来,以正常方式停止脚本的执行,使用'return'的**更好**选项比抛出用户异常。 – halloleo 2016-05-14 00:46:09

+1

嗯,不是真的 - 它告诉当前处理程序返回(没有值)。如果您处于脚本的顶层,那很好,它会结束执行,但是如果您处在一个处理程序中,它将返回零,无论它调用什么,脚本都会高兴地继续执行(可能意外结果如果调用者不期望缺失值)。 – dsmudger 2016-05-16 09:21:50

-1

可以用“退出”退出当前的应用程序。如果将脚本另存为应用程序,则可以使用它。

if wmColor is false 
    quit 
end if 
+5

请注意,如果您在脚本编辑器内运行的脚本上使用quit命令,它将退出脚本编辑器。 – garetmckinley 2015-09-14 15:04:14

+0

如果您正在查看应用程序“Finder”块中,“quit”不起作用。 – 2017-04-07 18:23:05