gpt4 book ai didi

ios - swift 中出现此错误的原因是什么? : script error -54. 由于错误 -1700 无法获取错误文本

转载 作者:行者123 更新时间:2023-11-28 07:23:12 25 4
gpt4 key购买 nike

我的桌面上有一个 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

最佳答案

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

我试过了,它对我有用。

这是苹果脚本:

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。

关于ios - swift 中出现此错误的原因是什么? : script error -54. 由于错误 -1700 无法获取错误文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57397898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com