macos Applescript 是否有类似于 PHP 的“退出”或“死”命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8157814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:37:36  来源:igfitidea点击:

Does Applescript have an "exit" or "die" command similar to PHP?

macoserror-handlingapplescript

提问by cwd

How can I throw an error and exit in Applescript? I'd like to have something like PHP's dieor exitcommand so that the "completed" dialog does not fire.

如何在 Applescript 中抛出错误并退出?我想要一些类似 PHPdieexit命令的东西,这样“完成”对话框就不会触发。

function1()
display dialog "completed"

on function1()
    function2()
end function1

on function2()
    exit //what do i use here?
end function2

Here is what I've tried with the answer that was posted below:

这是我尝试使用下面发布的答案的方法:

function1()
display dialog "completed"

on function1()
    function2()
end function1

on function2()

    try
        display dialog "Do you want to catch an error?" buttons {"Continue without error", "Cause an error"} default button 2
        if button returned of result is "Cause an error" then
            error "I'm causing an error and thus it is caught in 'on error'"
        end if
        display dialog "completed without error"
    on error theError
        return theError -- this ends the applescript when an error occurs
    end try


end function2

采纳答案by regulus6633

Try this ;)

试试这个 ;)

-- errors are only handled inside of a "try" block of code
try
    display dialog "Do you want to catch an error?" buttons {"Continue without error", "Cause an error"} default button 2
    if button returned of result is "Cause an error" then
        error "I'm causing an error and thus it is caught in 'on error'"
    end if
    display dialog "completed without error"
on error theError
    return theError -- this ends the applescript when an error occurs
end try

EDIT: Based on your comment... just return values from your functions. Check that returned value in your main code where you call the functions and the return value will tell you if you should "quit" the application or not. As such here's one way to solve your example problem...

编辑:根据您的评论...只需从您的函数返回值。检查调用函数的主代码中的返回值,返回值将告诉您是否应该“退出”应用程序。因此,这是解决示例问题的一种方法......

set returnValue to function1()

-- we check the return value from the handler
if returnValue is not true then return -- this "quits" the script

display dialog "completed"

on function1()
    set returnValue to function2()
    return returnValue
end function1

-- note that when there is no error the the script returns true.
-- so we can check for that and actt appropriately in the main script
on function2()
    try
        display dialog "Do you want to catch an error?" buttons {"Continue without error", "Cause an error"} default button 2
        if button returned of result is "Cause an error" then
            error "I'm causing an error and thus it is caught in 'on error'"
        end if
        display dialog "completed without error"
        return true
    on error theError
        return theError -- this ends the applescript when an error occurs
    end try
end function2