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

发布于 2024-12-16 01:14:15 字数 885 浏览 1 评论 0原文

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

function1()
display dialog "completed"

on function1()
    function2()
end function1

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

这是我尝试过的答案,如下所示:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-12-23 01:14:16

试试这个;)

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

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文