【发布时间】:2019-08-24 16:46:15
【问题描述】:
我正在尝试从我的 macOS 应用程序(Mojave、Xcode 10.3)运行 AppleScript。但是,当从应用程序运行 AppleScript 时,我收到此错误:
/Users/my_username/Desktop/test_apple_script.scpt: execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
根据其他帖子,我确保我的应用程序可以控制我的计算机(系统偏好设置 > 安全和隐私 > 隐私 > 可访问性),并且它还可以控制系统事件(系统偏好设置 > 安全和隐私 > 隐私 >自动化)。
这是我的 AppleScript:
tell application "System Events"
key code 48 using {command down}
end tell
脚本在脚本编辑器中运行时有效(我只需要在系统偏好设置 > 安全和隐私 > 隐私 > 可访问性中授予脚本编辑器访问权限)。
这是我用来从我的应用程序运行 Apple 脚本的代码(通过 osascript):
let proc = Process()
proc.launchPath = "/usr/bin/env"
proc.arguments = ["/usr/bin/osascript","/Users/my_username/Desktop/test_apple_script.scpt"]
proc.launch()
当我运行一个只显示通知的简单 AppleScript 时,这段代码可以完美运行:
tell application "System Events"
display dialog "Test"
end tell
我怎样才能让我的 AppleScript 从我的 macOS 应用程序中运行,就像我在脚本编辑器中运行它时一样?
编辑: 对于更多上下文,这里的命令选项卡大小写只是一个示例。我试图通过单击我的应用程序中的按钮来找到一种可通用的方法来模拟键盘快捷键(例如,用户单击“剪切”按钮并且应用程序的行为就像用户刚刚按下了 command + x)。可能有比使用 AppleScript 更好的方法,但我知道使用 AppleScripts 可以做我想做的事情。
编辑:如果我最初的问题不清楚,显示通知的简单 AppleScript 已经在我的应用程序中运行。当我尝试运行模拟用户从键盘输入的 AppleScript 时,就会出现问题。
编辑:这里的代码暂时对我有用,但它不再有效。我根本没有更改代码。我只是再次运行我的应用程序。
let PlayPauseScript = "tell application \"Spotify\"\n playpause\n end tell"\"test\"\n end tell"
if let scriptObject = NSAppleScript(source: PlayPauseScript) {
scriptObject.executeAndReturnError(nil)
}
编辑:
这是当前问题所在。根据建议,我将其添加到我的 info.plist,现在 Spotify AppleScript 始终有效(通过 NSAppleScript 调用时):
<key>NSAppleEventsUsageDescription</key>
<string>...this is why I need permission...</string>
看来我的问题(现在)只是使用系统事件的 AppleScripts。尽管确保我的应用程序和osascript 都被允许控制我的计算机(通过安全和隐私> 隐私> 可访问性),但我现在在使用NEAppleScript 和osascript 时得到相同的错误(1002)。我的应用程序也被允许控制系统事件(安全和隐私>隐私>自动化)。 osascript 错误在 Xcode 中打印到控制台,我通过使用此代码(在按钮的 @IBAction 中)发现了 NEAppleScript 的错误代码:
let script =
"""
try
tell application "System Events"
keystroke "c" using {command down}
end tell
on error errMsg number errorNumber
display dialog "An unknown error occurred: " & errorNumber as text
end try
"""
if let scriptObject = NSAppleScript(source: script) {
scriptObject.executeAndReturnError(nil)
}
上面的代码导致这个出现在对话框中:
An unknown error occurred: 1002
当我的应用程序运行时,它会提示我让它控制系统事件,我总是这样做。我的应用程序没有沙盒化。我相信NSAppleScript 和oascript 确实 都为我工作过一次,但是当我再次运行该应用程序时停止工作(尽管我没有对我的代码进行任何更改,但我几乎是肯定的)。
我不知道这是否提供信息,但我对可能发生的事情的唯一其他线索是,我必须不断检查和取消选中 security & privacy > privacy > accessibility 中的脚本编辑器的权限才能让它运行这些 AppleScript因为它告诉我不允许脚本编辑器发送击键。
【问题讨论】:
-
如果应用被沙盒化,您将无法使用
Process运行脚本。 -
好点。但是,我的应用没有沙盒化。
-
您是否在 Info.plist 中添加了
NSAppleEventsUsageDescription?顺便说一句,为什么不使用NSAppleScript运行脚本而不是执行shell 绕行。 -
请您提供更多上下文。为什么需要模拟command-tab?在我看来,有比通过 osascript 运行 AppleScript 更简单、更有效的方法来实现这一点。 (无论如何,还有更好的方法来运行苹果脚本,比如 NSAppleScript)。但在我知道上下文之前,我无法真正说出。
-
@vadian,我刚刚把它扔进了 Info.plist:
<key>NSAppleEventsUsageDescription</key> <string>NSApplication</string>。这是添加它的正确方法吗? (我对此还是很陌生。)我正在研究使用 NSAppleScript。
标签: swift macos permissions applescript keystroke