I call a small AppleScript handler from a larger Cocoa / Objective-C application (using the AppleScript-ObjC framework to call AppleScript methods directly from Objective-C code). I initially tried using Scripting Bridge, but this did not work for me due to compatibility issues with an external application. The sole purpose of AppleScript is to essentially send a small string to an external application. This is the first time I have tried to do something like this to control an external application, so please bear with me if I make an obvious mistake.
Sometimes AppleScript may encounter errors depending on the state of the external application, and I would like to handle these errors accordingly in my Objective-C code.
The way I'm doing it now is that I have a try block in AppleScript:
try -- Do stuff return the number 0 on error the error_message number the error_number return the error_number end try
I return zero if the script ends normally and returns an error number if it is not. Then, in Objective-C code, I check the return value and throw a "normal" exception in cases where there is an error, so I can deal with this in my usual exception handling code.
This works, but is there a way to catch an AppleScript exception directly from Objective-C code? When I run the code without the AppleScript try-error code in Xcode, it seems to hit my exception breakpoint before it returns from AppleScript, but it never throws an @catch block for NSException . Is there any way to catch this exception that gets caught somehow? I suppose the answer is no, but I wanted to check that there is no better way to do this than what I do. Apple's documentation on AppleScript-ObjC is pretty scarce, and basically just talking about error handling in AppleScript.
Update: So, I just played around a little bit with setting a breakpoint, and it turns out that a C ++ exception is thrown from AppleScript code, not Objective-C. This is why it was not captured by the @catch block that I used. It looks like the AppleScript-ObjC mechanism uses exceptions for control / recovery thread errors, and I found that my breakpoint will break when they are thrown. Therefore, I think itβs best to just catch the errors in the AppleScript application and return the error code as a string or integer in Objective-C code.
source share