如何在Mac(OS X)上的沙盒应用程序中运行AppleScript

11

我们正在使用Qt 5.2.0.Framework在MacOSX 10.9上为Mac应用商店开发应用程序。

这里有一段简单的AppleScript代码,可以创建一个Microsoft Excel工作簿并将其保存到任何位置。

tell application "Microsoft Excel"
    set myworkbook to make new workbook
    set fname to POSIX file "/Private/var/root/Download/ExcelFile" as text
    save workbook as myworkbook filename fname
end tell

上述脚本被保存为Untitled.scpt位于 /Library/ApplicationScript/

在应用程序中,我们使用Cocoa框架来执行AppleScript。

这个AppleScript在非沙盒化应用程序中可以工作,但在沙盒化应用程序中会失败。

我的问题是:如何在沙盒化应用程序中使用AppleScript?或者是否有替代方法?

请告诉我解决方案,因为这个问题正在拖延我的项目。

谢谢

输入图像描述

输入图像描述


1
我已经重新格式化了你的问题,以使其更易读;请查看源代码,以便将来自己进行此格式化;你首先说脚本文件名是 new.scpt,但你的 Objective-C 代码引用的是 Untitled.scpt。我对沙盒知之甚少,但我不会期望 /private/Library 等路径可以从沙盒应用程序中写入。.scpt 文件如何保存到目标位置? - mklement0
这个苹果脚本应该保存为Untitled.scpt(而不是new.scpt),路径为“/Library/ApplicationScript/Untitled.scpt”。当使用Cocoa执行脚本时,会创建一个Excel文件,并将其保存在/Private/var/root/Download/ExcelFile位置。 - A_kumar
在我的问题中将关键字“new.scpt”替换为“Untitled.scpt”。 - A_kumar
这是一篇不错的文章,可以阅读 https://www.objc.io/issues/14-mac/sandbox-scripting/ - onmyway133
3个回答

10

你的代码有两个问题:

  • Excel可能还不支持com.apple.security.scripting-targets,所以你需要com.apple.security.temporary-exception.apple-events(请参见这里如何找出是否支持它,以及在此如何通过添加要定位的捆绑标识符的数组来使用临时例外。您在旧屏幕截图中看到过这个。)

  • scripting-targets 和 com.apple.security.temporary-exception.apple-events 的授权均为捆绑标识符数组。您可以在Xcode中看到它的样子:com.apple.security.temporary-exception.apple-events

  • Mac App Store应用程序不得在共享位置(例如/Library/ApplicationScript)安装任何内容(请参见App Store审核指南第2.15节)。您需要将脚本存储在容器内并从其中运行。


我不理解 entitlement 文件中 com.apple.security.temporary-exception.apple-events 或者 com.apple.security.scripting-targets 的关键值。这个关键值在沙盒应用中执行 Apple 脚本时是正确的吗?请告诉我正确的关键值。 - A_kumar
感谢Mahal解决了我项目中的大问题。 - A_kumar
无法使用Cocoa执行do shell script命令。 Cocoa代码虚拟是为了看到我的问题上面。 NSAppleEventDescriptor的描述符变量返回NULL值。我的脚本文件包含此命令“do shell script“diskutil info / | grep "Volume Nmae:"”。 - A_kumar
这是一个新问题。要使用“do shell script”,您需要拥有另一个授权。而对于diskutil,最好使用Disk Arbitration进行操作 https://developer.apple.com/library/mac/documentation/DriversKernelHardware/Conceptual/DiskArbitrationProgGuide/ManipulatingDisks/ManipulatingDisks.html#//apple_ref/doc/uid/TP40009310-CH3-SW7 - mahal tertin
我的Apple脚本在10.7、10.8.5和10.9上成功运行,但在Mountain Loin(10.8到10.8.4)上失败了。我的Apple脚本存储在应用程序容器中,脚本路径为->“/Users/UserName/Library/Containers/com.comanyName.ProductName/Data/Library/AppleScripts/”。 - A_kumar
显示剩余2条评论

2

我是MAC上的新用户,在权限方面,我创建了一个com.apple.security.scripting-targets的新条目。但是如何创建application-group-id呢? - A_kumar
你只需将上面链接中的键/值(它有一个示例)添加到你的.entitlements文件中即可。 - indragie
请查看截图以获取附加到此问题的权限文件。在沙盒应用程序中添加 scripting-targets 键,但新创建的沙盒应用程序无法执行苹果脚本。 - A_kumar
在授权文件中添加新的键com.apple.security.scripting后,脚本未能运行(请参见授权文件的第二个截图)。如果键值不正确,请告诉我正确的值,并提供一个适当的示例来解决我的问题。 - A_kumar
我的Apple脚本在10.7、10.8.5和10.9上成功运行,但在Mountain Loin(10.8到10.8.4)上失败了。我的Apple脚本存储在应用程序容器中,脚本路径为->“/Users/UserName/Library/Containers/com.comanyName.ProductName/Data/Library/Appl‌​eScripts/”。 - A_kumar
显示剩余2条评论

1
要从您的应用程序使用Apple Script,您需要在授权文件中添加要发送或接收事件的应用程序的捆绑标识符。
以下是语法:
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>{bundle identifier of app 1}</string>
    <string>{bundle identifier of app 2}</string>
</array>

您可以在该数组中拥有一个或多个应用程序包标识符。现在,这些应用程序可以接收您的Apple事件命令。

例如:

<key>com.apple.security.temporary-exception.apple-events</key>
<array>
    <string>com.apple.finder</string>
    <string>com.apple.systemevents</string>
    <string>com.microsoft.excel</string>
</array>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接