NSAppleScript execution fails in sandboxed app

本文介绍了一个关于在沙盒应用中使用AppleScript向Safari添加阅读列表项的问题及解决方案。作者尝试通过Cocoa应用程序执行特定AppleScript,但遇到了错误。最终发现是由于在权限声明中使用了不正确的Safari包标识符。

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?

share improve 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

up vote 2 down vote accepted

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

share improve this answer
 

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

share improve 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值