NSAppleScript execution fails in sandboxed app

来源:互联网 发布:网络运维系统 编辑:程序博客网 时间:2024/06/10 09:50


2
down votefavorite

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?

shareimprove this question
 
 
Maybe your entitlements aren't as appropriate as you thought ... ? What are they? – uliwitness Sep 20 '12 at 10:20
1 
Entitlements are com.apple.security.temporary-exception.apple-events is an array with com.apple.Safari. Note that the activate script does work in the sandboxed app, so I think the entitlements must be OK, at least partially. – Drew McCormack Sep 20 '12 at 10:24 
 
Oh, your script is a string. Have you tried running a precompiled/tokenized script (.scpt)? That shouldn't need the dictionary. – uliwitness Sep 20 '12 at 11:09
 
Found the problem. The bundle id has to be com.apple.safari in entitlements, not com.apple.Safari. :( – Drew McCormack Sep 20 '12 at 11:11
 
Can you require 10.8? If so, you may be able to use the NSUserScriptTask class(es) instead of NSAppleScript: developer.apple.com/library/mac/documentation/Foundation/… – Peter Hosey Sep 20 '12 at 14:10 

2 Answers

activeoldestvotes
up vote2down voteaccepted

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

shareimprove this answer
 

up vote1down vote

Sandboxed apps cannot send AppleEvents to other apps, hence they cannot use AppleScript to communicate with other applications.

shareimprove this answer
 
1 
It should work if you request the temporary exception for apple events. Note that I can run an 'activate' script in the sandboxed app fine. It's just the reading list script that fails. – Drew McCormack Sep 20 '12 at 10:27
 
The activate event might get handled by the system internally, so it's not an event send to the app itself, therefore it might not be constrained as other AppleEvents directly targeting an app. – iljawascoding Sep 20 '12 at 10:38

0 0