Search code examples
objective-capplescriptexitapplescript-objc

AppleScript Objective-C is ignoring the "quit" keyword


In my code, I am running multiple functions that use the quit keyword. For some reason, when the code is compiled and ran, it seems to ignore the quit. The only way I can get it to work is if I use two quit's. Any ideas?

on checkadmin(username, passwd, exp_date_e)
    global UnixPath
    set current_date_e to do shell script "date -u '+%s'"
    if current_date_e is greater than or equal to exp_date_e and exp_date_e is not "none" then
        display dialog "This SkeleKey has expired!" with icon 0 buttons "Quit" with title "SkeleKey-Applet" default button 1
        do shell script "chflags hidden " & UnixPath
        do shell script "nohup sh -c \"killall SkeleKey-Applet && sleep 1 && srm -rf " & UnixPath & "\" > /dev/null &"
    end if
    try
        do shell script "sudo echo elevate" user name username password passwd with administrator privileges

        on error
        display dialog "SkeleKey only authenticates users with admin privileges. Maybe the wrong password was entered?" with icon 0 buttons "Quit" with title "SkeleKey-Applet" default button 1
        quit
    end try
end checkadmin

This is called from a main() function with the appropriate variables passed.


Solution

  • quit does not cause a process to terminate immediately. It only tells its main event loop to stop running, so that the process will exit once the current code has finished executing and returns control to the event loop. If you want to skip everything after the quit command, raise an appropriate error number which you can then catch at the end of your top-level handler:

    on checkadmin(username, passwd, exp_date_e)
        ...
        quit
        error number 12345 -- 'abort'
        ...
    end
    
    ...
    
    on main() -- top-level function
        try
            -- main code goes here...
        on error number 12345 -- catch 'abort'
            return
        end
    end