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

### 构建应用时签名任务失败的解决方案 当遇到 `Execution failed for task ':app:signingConfigWriterRelease'` 错误时,通常是因为文件权限问题或配置不正确引起的。以下是详细的解决方法: #### 文件权限问题 如果错误提示涉及访问被拒绝,则可能是由于目标路径上的文件夹或文件权限不足造成的。应确保 Android Studio 对指定目录具有读写权限。 对于提到的具体路径 `D:\Development\Android\workProject\Git-XingTongBao\XingTongBao\app\build\intermediates\signing_config\release\out\signing-config.json` 的访问权限需要特别注意[^1]。 可以通过以下方式调整权限设置: - 右键点击项目所在的磁盘分区(如 D 盘),选择属性。 - 转到安全选项卡,确认当前用户的权限是否允许修改操作。 - 如果有必要,授予完全控制权给用户账户,并重新启动 IDE 和构建过程。 #### 清理缓存与重建工程 有时旧版本的数据残留也会引发此类异常情况的发生。尝试清理 Gradle 缓存并同步项目结构来排除潜在干扰因素。 具体做法是在菜单栏依次选取 File -> Invalidate Caches / Restart... 来清除本地索引数据;接着通过 Build -> Clean Project 命令彻底清洗整个工作区内的临时产物后再做一次完整的编译流程。 #### 修改 gradle 配置 考虑到某些情况下 Lint 工具可能会阻止 APK 打包完成,在 app 模块下的 `build.gradle` 中适当放宽检查策略有助于绕过不必要的阻碍。例如可以在该文件内加入如下片段以禁用发布版构建中的 Lint 自动检测机制[^4]: ```groovy android { ... lintOptions { checkReleaseBuilds false // 不再针对发行版本执行Lint分析 abortOnError false // 即使发现严重警告也不终止进程 } } ``` 以上措施能够有效缓解由严格的质量审查所导致的任务中断现象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值