Search code examples
cocoaapplescriptsandbox

NSAppleScript execution fails in sandboxed app


I have a sandboxed app targeting Mac OS X 10.7, and want to execute this AppleScript:

tell application "Safari" to add reading list item "http://www.apple.com"

I have tested this script in the AppleScript Editor, and it executes correctly.

In the Cocoa app, I have setup the appropriate temporary entitlements, and tested it with the following script, which executes properly:

tell application "Safari" to activate

But when I insert the first script in my Cocoa app, I get an error. Here is the code I am using

    NSString *url = [post.url absoluteString];
    NSString *source = [NSString stringWithFormat:@"tell application \"Safari\" to add reading list item \"%@\"", url];

    NSDictionary *errorDictionary;
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];

    if ( ![script executeAndReturnError:&errorDictionary] ) {
        NSLog(@"Error while saving to Safari Reading List: %@", errorDictionary);
    }

The error is

2012-09-20 10:30:29.370 Cream[2752:303] Error while saving to Safari Reading List: {
NSAppleScriptErrorBriefMessage = "A identifier can\U2019t go after this identifier.";
NSAppleScriptErrorMessage = "A identifier can\U2019t go after this identifier.";
NSAppleScriptErrorNumber = "-2740";
NSAppleScriptErrorRange = "NSRange: {29, 11}";
}

The error seems to refer to the term 'reading'. It's as though it hasn't loaded the Safari scripting dictionary, and doesn't understand what 'reading list item' means.

If I run the app with sandboxing disabled, it works perfectly with exactly the same script.

Anyone know what could be going on? Do I need to punch another hole in the sandbox somewhere?


Solution

  • Turns out the problem was using the bundle id com.apple.Safari in the entitlements, instead of com.apple.safari.