Best way to handle bugs with the AppleScript-ObjC built-in Cocoa app?

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.

+4
source share
1 answer

AppleScript uses C ++ exceptions to implement its own exceptions, but nothing more; you should not see them in code that succeeds. If the AppleScript exception slips to the point of return to Objective-C, the bridge actually swallows it - you should get a log message and return value nil / zero / NO from the bridge method. The result of all this is that you are already doing the right thing: the error trap in AppleScript and returning what your Objective-C code might detect.

As a side note, AppleScriptObjC is primarily intended for developers who write applications mostly or entirely in AppleScript. If you are writing an Objective-C application with mostly several bits that control other applications through scripts, use ScriptingBridge instead.

0
source

Source: https://habr.com/ru/post/1494792/


All Articles