【问题标题】:what is the cause of this error in swift? : script error -54. Couldn't get error text because of error -1700swift中这个错误的原因是什么? : 脚本错误 -54。由于错误 -1700 无法获取错误文本
【发布时间】: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


【解决方案1】:

我只需将命令设置为运行env,因此您只需调用在用户路径中找到的osascript

我试过了,它对我有用。

这是 Apple 脚本:

on run {targetBuddyPhone, targetMessage}
   "Target Buddy Phone:" & targetBuddyPhone & " - Target Message:" & targetMessage
end run

这是完整的 swift 脚本:

#!/usr/bin/env swift
import Foundation

let myCommand = Process()
let pipe = Pipe()
if #available(OSX 13, *) {
  myCommand.executableURL = URL(fileURLWithPath: "/usr/bin/env")
} else {
  myCommand.launchPath = "/usr/bin/env"
}
myCommand.arguments = ["osascript", "./script.scpt","+12345678901" ,"\"my text message\""]
myCommand.standardOutput = pipe
do {
  if #available(OSX 13, *) {
    try myCommand.run()
  } else {
    myCommand.launch()
  }
  myCommand.waitUntilExit()
  let data = pipe.fileHandleForReading.readDataToEndOfFile()
  if let output = String(data: data, encoding: String.Encoding.utf8) {
    print(output)
  }
}

当我运行脚本时,我得到了:

Target Buddy Phone:+12345678901 - Target Message:"my text message"

如果可行,请告诉我,我写了一篇 Swift 脚本介绍 (https://rderik.com/blog/using-swift-for-scripting/),您可能会觉得有用。

更新 创建了完整的 swift 脚本和一个测试 AppleScript。

【讨论】:

  • 感谢您的回答。但是在 Xcode 10.3 中粘贴这段代码后,我遇到了这些错误: >Use of unresolved identifier 'myCommand';你是说'doCommand'吗? >使用未解析的标识符“pmset”>“(选择器)类型的值 -> Void”没有成员“executableURL”
  • 我试过这个:让 ls = Process() ls.executableURL = URL(fileURLWithPath: "/usr/bin/env") ls.arguments = ["osascript","/Users/faranegar/ Desktop/sms.scpt" ,"+989194774880" ,"\"hi\""] do{ try ls.run() } catch { } 但仍然收到脚本错误 -54。
  • 我忘了声明:myCommand。开头应该是let myCommand = Process()
  • @HamidrezaShokouhi 我更新了答案,因此您可以轻松复制和粘贴两个脚本并进行测试。他们应该工作
  • 谢谢。我试过你的代码,代码没问题,但我一直收到这个错误:/Users/faranegar/Desktop/sms.scpt: script error -54。由于错误 -1700,无法获取错误文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多