Есть ли в Applescript команда «exit» или «die», похожая на PHP? - PullRequest
10 голосов
/ 16 ноября 2011

Как мне сгенерировать ошибку и выйти в Applescript?Я хотел бы иметь что-то вроде команды PHP * die или exit, чтобы диалоговое окно «завершено» не запускалось.ниже:

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

1 Ответ

5 голосов
/ 17 ноября 2011

Попробуйте это;)

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