对嵌入 applescript 的 Objective-C 应用程序进行沙箱处理

发布于 2024-12-15 11:43:03 字数 8496 浏览 2 评论 0原文

我想对一个应用程序进行沙箱处理,该应用程序使用 applescript 联系 safari 以获取选项卡列表。然后我通过 NSAppleEventDescriptor 在目标 c 中取回这些数据。 我已经将 com.apple.security.temporary-exception.apple-events 密钥和 com.apple.safari 添加到我的权利中。 当我的应用程序没有沙箱化时,我没有问题,但是当我尝试沙箱化它时,我只是得到“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>

我希望我已经提供了您需要的信息来帮助我。 谢谢

I would like to sandbox an app which uses applescript to contact safari to get the list of the tabs. Then I get back those datas in an objective c trough NSAppleEventDescriptor.
I've already added the com.apple.security.temporary-exception.apple-events key with com.apple.safari to my entitlements.
When my app is not sandboxed i have no problem but when i try to sandbox it i just get "null" as data.

applescript to list the tabs and return it as 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

get data back in objective 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);

Message received when no sandbox

[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>

Message received when sandboxed

[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
)

Entitlements file

<?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>

I hope i've given what you need to help me.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-12-22 11:43:03

据我所知,一旦我修复了您的权利 plist 中的双重密钥,您的代码就可以正常工作。

As far as I can tell, your code works just fine, once I fix the doubled key in your entitlements plist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文