如何告诉Applescript停止执行

如何告诉Applescript停止执行,applescript,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 mu

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
似乎找不到如何执行此操作-有人知道如何执行吗?

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

if wmColor is false then
    error number -128
end if

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

这是在此基础上修改的。

“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
您可以使用“退出”一词退出当前应用程序。如果将脚本保存为应用程序,则可以使用此选项

if wmColor is false
    quit
end if

对于通过osascript调用的AppleScript的特定情况,可以通过执行以下操作终止它,并将状态0返回给shell:

tell me to "exit"
tell me to error "{your message}"
终止,发出消息并返回状态1,方法是:

tell me to "exit"
tell me to error "{your message}"
上面将这样一行写入标准错误:

{scriptname}: execution error: {your message} (-2700)
{scriptname}: execution error: Terminated by user (0)
下面是一个替换了神秘的“-2700”的示例,它很可能符合日本的要求:

tell me to error "Terminated by user" number 0
将此写入标准错误:

{scriptname}: execution error: {your message} (-2700)
{scriptname}: execution error: Terminated by user (0)
并返回状态为1

在阅读之后,我通过实验学到了这一点:

这仅在未包含在捕获此类错误的try块中时有效。JXA变体:
const e=new Error(“用户已取消”);e、 errorNumber=-128;投掷e。魔术:JS
Error
类没有定义
errorNumber
属性,但是,您必须将
errorNumber
设置为
-128
,以避免向用户显示错误对话框。您能否详细说明您的答案,并添加有关您提供的解决方案的更多描述?我认为,要以正常方式停止脚本的执行,使用
返回
比抛出错误对话框要好得多用户异常。嗯,不是真的-它告诉当前处理程序返回(没有值)。如果您在脚本的顶层,这很好,它将结束执行,但是如果您在处理程序中,它将对调用它的任何对象返回一个nil,脚本将愉快地继续执行(如果调用方不希望缺少值,则可能会出现意外的结果)。这只会在我使用它时结束一个
If
语句,而不是退出脚本。
error
方法更好。我只需将此代码更改为一行:
如果wmColor为false,则返回
请注意,如果在脚本编辑器中运行的脚本上使用quit命令,它将退出脚本编辑器。如果您使用的是tell应用程序“Finder”,则“quit”不起作用阻塞。它正在退出自动程序运行并显示错误
无法与帮助程序应用程序通信。
在使用此功能时在终端中。