沙箱化嵌入 AppleScript 的 Objective-C 应用程序
Posted
技术标签:
【中文标题】沙箱化嵌入 AppleScript 的 Objective-C 应用程序【英文标题】:Sandboxing an objective-c app embedding applescript 【发布时间】:2011-11-14 20:28:53 【问题描述】:我想对一个使用 applescript 联系 safari 以获取选项卡列表的应用程序进行沙箱处理。然后我通过 NSAppleEventDescriptor 在目标 c 中取回这些数据。 我已经将带有 com.apple.safari 的 com.apple.security.temporary-exception.apple-events 键添加到我的权利中。 当我的应用没有被沙盒化时,我没有问题,但是当我尝试沙盒化它时,我只会得到“null”作为数据。
applescript 列出选项卡并将其作为 xml 返回。
tell application "Safari"
set listeSite to "http://www.deezer.com", "deezer"
set resp to "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
repeat with aWindow in windows
repeat with aTab in tabs of aWindow
set URLTab to URL of aTab
repeat with aSite in listeSite
if URLTab starts with item 1 of aSite then
set oktabURL to URL of aTab
set oktabWindowID to id of aWindow
set resp to resp & "<site><url>" & oktabURL & "</url><windowID>" & oktabWindowID & "</windowID><type>" & item 2 of aSite & "</type></site>"
end if
end repeat
end repeat
end repeat
return resp
end tell
在目标 c 中取回数据
NSString *path = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"scpt"];
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURLfileURLWithPath:path] error:nil];
NSAppleEventDescriptor *result = [script executeAndReturnError:nil];
NSLog(@"result == %@", result);
DescType descriptorType = [result descriptorType];
NSLog(@"descriptorType == %@", NSFileTypeForHFSTypeCode(descriptorType));
NSString *responseValue = [result stringValue];
NSLog(@"value == %@", responseValue);
[label setStringValue:responseValue];
NSData *data = [result data];
NSXMLDocument *xmlResponse = [[NSXMLDocument alloc] initWithData:data options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:nil];
NSLog(@"xml == %@", xmlResponse);
没有沙箱时收到消息
[Switching to process 6362 thread 0x0]
2011-11-14 13:18:31.127 SongController[6362:407] result == <NSAppleEventDescriptor: 'utxt'("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><site><url>http://www.deezer.com/fr/index.php</url><windowID>4536</windowID><type>deezer</type></site>")>
2011-11-14 13:18:31.128 SongController[6362:407] descriptorType == 'utxt'
2011-11-14 13:18:31.129 SongController[6362:407] value == <?xml version="1.0" encoding="iso-8859-1"?><site><url>http://www.deezer.com/fr/index.php</url><windowID>4536</windowID><type>deezer</type></site>
2011-11-14 13:18:31.130 SongController[6362:407] xml == <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?><site><url>http://www.deezer.com/fr/index.php</url><windowID>4536</windowID><type>deezer</type></site>
沙盒时收到的消息
[Switching to process 6380 thread 0x0]
2011-11-14 13:20:21.233 SongController[6380:407] result == (null)
2011-11-14 13:20:21.234 SongController[6380:407] descriptorType == ''
2011-11-14 13:20:21.235 SongController[6380:407] value == (null)
2011-11-14 13:20:21.235 SongController[6380:407] *** Assertion failure in -[NSTextFieldCell _objectValue:forString:errorDescription:], /SourceCache/AppKit/AppKit-1138.23/AppKit.subproj/NSCell.m:1564
2011-11-14 13:20:21.236 SongController[6380:407] An uncaught exception was raised
2011-11-14 13:20:21.237 SongController[6380:407] Invalid parameter not satisfying: aString != nil
2011-11-14 13:20:21.241 SongController[6380:407] (
0 CoreFoundation 0x00007fff8a8bc286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff9125dd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8a8bc0ba +[NSException raise:format:arguments:] + 106
3 Foundation 0x00007fff8b3547d3 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 169
4 AppKit 0x00007fff886e1a09 -[NSCell _objectValue:forString:errorDescription:] + 160
5 AppKit 0x00007fff886e1963 -[NSCell _objectValue:forString:] + 19
6 AppKit 0x00007fff886e18c9 -[NSCell setStringValue:] + 41
7 AppKit 0x00007fff887e3084 -[NSControl setStringValue:] + 115
8 SongController 0x000000010000142f -[AppController sayHello:] + 495
9 CoreFoundation 0x00007fff8a8aba1d -[NSObject performSelector:withObject:] + 61
10 AppKit 0x00007fff887ca710 -[NSApplication sendAction:to:from:] + 139
11 AppKit 0x00007fff888b7bd7 -[NSMenuItem _corePerformAction] + 399
12 AppKit 0x00007fff888b790e -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 125
13 AppKit 0x00007fff88b54194 -[NSMenu _internalPerformActionForItemAtIndex:] + 38
14 AppKit 0x00007fff889e5ce5 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 138
15 AppKit 0x00007fff8883151f NSSLMMenuEventHandler + 339
16 HIToolbox 0x00007fff8f4b6308 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1263
17 HIToolbox 0x00007fff8f4b5914 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 446
18 HIToolbox 0x00007fff8f4cc6c7 SendEventToEventTarget + 76
19 HIToolbox 0x00007fff8f5129f5 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 398
20 HIToolbox 0x00007fff8f5f9645 SendMenuCommandWithContextAndModifiers + 56
21 HIToolbox 0x00007fff8f63fef9 SendMenuItemSelectedEvent + 253
22 HIToolbox 0x00007fff8f50bb07 _ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2_ + 101
23 HIToolbox 0x00007fff8f638909 _ZL19PopUpMenuSelectCoreP8MenuData5PointdS1_tjPK4RecttjS4_S4_PK10__CFStringPP13OpaqueMenuRefPt + 1660
24 HIToolbox 0x00007fff8f638bc8 _HandlePopUpMenuSelection7 + 621
25 AppKit 0x00007fff889e89b1 _NSSLMPopUpCarbonMenu3 + 3860
26 AppKit 0x00007fff88d911ba _NSPopUpCarbonMenu3 + 39
27 AppKit 0x00007fff889e6b16 -[NSCarbonMenuImpl popUpMenu:atLocation:width:forView:withSelectedItem:withFont:withFlags:withOptions:] + 322
28 AppKit 0x00007fff88c45b3e +[NSStatusBarButtonCell popupStatusBarMenu:inRect:ofView:withEvent:] + 673
29 AppKit 0x00007fff88c45de9 -[NSStatusBarButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 147
30 AppKit 0x00007fff887c863a -[NSControl mouseDown:] + 786
31 AppKit 0x00007fff887930e0 -[NSWindow sendEvent:] + 6306
32 AppKit 0x00007fff88c467fc -[NSStatusBarWindow sendEvent:] + 66
33 AppKit 0x00007fff8872b68f -[NSApplication sendEvent:] + 5593
34 AppKit 0x00007fff886c1682 -[NSApplication run] + 555
35 AppKit 0x00007fff8894080c NSApplicationMain + 867
36 SongController 0x0000000100001022 main + 34
37 SongController 0x0000000100000ff4 start + 52
)
权利文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.temporary-exception.apple-events
com.apple.security.temporary-exception.apple-events
</key>
<array>
<string>com.apple.systemevents</string>
<string>com.google.chrome</string>
<string>com.apple.safari</string>
</array>
</dict>
</plist>
我希望我已经给出了你需要帮助我的东西。 谢谢
【问题讨论】:
您的密钥 'com.apple.security.temporary-exception.apple-events' 似乎在您的 .entitlements 文件中重复。还是只是一个错字? 对于 com.apple.systemevents 有 Apple 事件例外的应用程序将被 Mac App Store 拒绝。 Chrome 和 Safari 通常都可以,但要根据具体情况而定。 【参考方案1】:据我所知,一旦我修复了您的权利 plist 中的双键,您的代码就可以正常工作。
【讨论】:
以上是关于沙箱化嵌入 AppleScript 的 Objective-C 应用程序的主要内容,如果未能解决你的问题,请参考以下文章