【问题标题】:How to run an AppleScript from a sandboxed application on a Mac (OS X)如何在 Mac (OS X) 上从沙盒应用程序运行 AppleScript
【发布时间】:2014-03-22 09:46:01
【问题描述】:

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

这是一个简单的 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

以上脚本在/Library/ApplicationScript/中保存为Untitled.scpt

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

此 AppleScript 在非沙盒应用程序中工作。它在沙盒应用程序中失败。

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

请告诉我解决方案,因为我的项目因此而延迟。

谢谢

【问题讨论】:

  • 我已重新格式化您的问题,使其更具可读性;请查看源代码,以便您以后可以自己执行此格式化;你先说脚本文件名是new.scpt,但是你的Objective-C代码引用了Untitled.scpt。我对沙盒知之甚少,但我不希望像 /private/Library 这样的路径可以从沙盒应用程序中写入。 .scpt文件如何保存到目标位置?
  • 这个,苹果脚本在这个路径“/Library/ApplicationScript/Untitled.scpt”中保存为Untitled.scpt(不是new.scpt)。使用 cocoa 执行脚本时,会创建 excel 文件,保存为 /Private/var/root/Download/ExcelFile 这个位置
  • 在我的问题中将关键字“new.scpt”替换为“Untitled.scpt”
  • 这是一篇值得阅读的好帖子objc.io/issues/14-mac/sandbox-scripting

标签: cocoa applescript mac-app-store appstore-sandbox entitlements


【解决方案1】:

您需要 add the com.apple.security.scripting-targets sandboxing entitlement 从沙盒中编写其他应用程序的脚本。

【讨论】:

  • 我是 MAC 上的新用户,在权利中,我正在为 com.apple.security.scripting-targets 创建一个新条目。但是如何创建 application-group-id。
  • 您只需将上面链接中的键/值(它有一个示例)添加到您的 .entitlements 文件中。
  • 请参阅此问题附加的权利文件的屏幕截图。在沙盒应用中添加 scripting-targets 键,但新创建的沙盒应用不执行苹果脚本。
  • com.apple.security.scripting-targets 不是布尔值,它是一个字符串数组。请阅读我链接到的文章。
  • 在授权文件中添加新密钥 com.apple.security.scripting 后脚本未运行。 (参见权利文件的第二个屏幕截图)。如果键值错误,请告诉我正确的值并提供一个合适的例子来解决我的问题。
【解决方案2】:

您的代码有两个问题:

  • Excel 可能还不支持com.apple.security.scripting-targets,所以您需要com.apple.security.temporary-exception.apple-events(请参阅here 如何确定它是否支持它,这里how to work with the temporary exception 通过添加捆绑标识符数组你想定位。你在这个问题的旧屏幕截图中有这个。)

  • 脚本目标和com.apple.security.temporary-exception.apple-events 的权利是一个捆绑标识符数组。你会在 Xcode 中看到它是这样的:

  • Mac App Store 应用不得在/Library/ApplicationScript 等共享位置安装任何内容(请参阅App Store Review Guidelines 第2.15 节)。您需要将脚本存储在您的容器中并从那里运行它。

【讨论】:

  • 我不明白权利文件中 com.apple.security.temporary-exception.apple-events 或 com.apple.security.scripting-targets 的键值。这个键的正确值是多少。在沙盒应用程序上执行苹果脚本。
  • 感谢 mahal 解决了我项目的大问题。
  • 无法使用 cocoa 执行 do shell script 命令。 cocoa code dummy 是看上面我的问题。 NSAppleEventDescriptor 的描述符变量返回 NULL 值。我的脚本文件包含此命令“do shell script”diskutil info / | grep \"卷名称:\"" "
  • 这是一个新问题。要“执行 shell 脚本”,您需要拥有另一项权利。对于 diskutil,您最好使用 Disk Arbitration developer.apple.com/library/mac/documentation/…
  • 我的 Apple 脚本在 10.7,10.8.5,10.9 上成功运行。但它在 Mountain Loin (10.8 to 10.8.4) 中失败。我的苹果脚本存储在应用容器中。脚本路径是 -> "/Users/UserName/Library/Containers/com.comanyName.ProductName/Data/Library/AppleScripts/"
【解决方案3】:

要在您的应用程序中使用 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>

您可以在该数组中拥有一个或多个 app bundle 标识符。现在,这些应用可以接收您的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多