2011-12-23 104 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