【发布时间】:2019-12-15 07:08:08
【问题描述】:
我的桌面上有一个 AppleScript,有两个输入,我需要快速运行它。 当我在终端中运行这个命令时,我得到了我想要的:
osascript /Users/faranegar/Desktop/sms.scpt +12345678901 "my text message"
所以我想在我的 swift 代码中完全运行上面的命令,所以我写了这段代码:
// Create a Task instance
let task = Process()
// Set the task parameters
task.launchPath = "/usr/bin/env"
task.arguments = ["osascript","/Users/faranegar/Desktop/sms.scpt","+12345678901" ,"\"my text message\""]
// Create a Pipe and make the task
// put all the output there
let pipe = Pipe()
task.standardOutput = pipe
// Launch the task
task.launch()
// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
print(output!)
但不幸的是,我收到了这个错误:
[Error] /Users/faranegar/Desktop/sms.scpt: script error -54. Couldn't get error text because of error -1700.
有谁知道问题出在哪里?
顺便说一下,这是我的 AppleScript:
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run
【问题讨论】:
-
看这个,好像你已经指定了两次
osascript:一次是在启动路径中,另一次是在参数中(实际上是“/usr/bin/osascript osascript /Users/faranegar.. 。”)。从参数列表中删除它,看看是否可以解决问题。你也可以使用 NSAppleScript 而不是 shell 任务来运行它;不确定这是否值得重构。 -
@TedWrigley 是的,你是对的。但是将我的代码更改为 task.launchPath = "/usr/bin/env" 后,我仍然收到错误
-
为什么要调用 usr/bin/env 来运行 AppleScript?内置支持。将脚本构造为字符串并运行它。或者使用 NSAppleScript。或者使用 Bridge。
标签: ios swift xcode macos applescript