gpt4 book ai didi

macos - Applescript 是否有类似于 PHP 的 "exit"或 "die"命令?

转载 作者:行者123 更新时间:2023-12-01 19:40:13 26 4
gpt4 key购买 nike

如何在 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

最佳答案

试试这个;)

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

关于macos - Applescript 是否有类似于 PHP 的 "exit"或 "die"命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8157814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com