【发布时间】:2019-04-12 13:11:55
【问题描述】:
我是 swift 新手,并尝试按照互联网上的几个不同教程来让我的应用程序运行终端。
我有一个复选框,应该启用(通过终端命令)充电铃声(当然在未选中时将其关闭)。
编辑: 更新了代码,还是不行:
@discardableResult func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/usr/bin/"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
@IBAction func SoundBox(_ sender: NSButton) {
if(SoundBox.state == NSControl.StateValue.on){
self.helloLabel.stringValue = "It's On!"
shell("say hello")
//shell("defaults write com.apple.PowerChime ChimeOnAllHardware -bool true; open /System/Library/CoreServices/PowerChime.app &")
}
else if(SoundBox.state == NSControl.StateValue.off){
self.helloLabel.stringValue = "It's off!"
shell("say hello")
//shell("defaults write com.apple.PowerChime ChimeOnAllHardware -bool false; killall PowerChime")
}
新的控制台输出:
2019-04-12 16:44:54.792282+0200 TerminApp[2985:45592] [General] Couldn't posix_spawn: error 13
2019-04-12 16:44:54.795254+0200 TerminApp[2985:45592] [General] (
0 CoreFoundation 0x00007fff42444e45 __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff6d07c3c6 objc_exception_throw + 48
2 CoreFoundation 0x00007fff42444c77 +[NSException raise:format:] + 193
3 Foundation 0x00007fff446495e9 -[NSConcreteTask launchWithDictionary:error:] + 4437
4 TerminApp 0x000000010000299b $s9TerminApp14ViewControllerC5shellyS2SF + 635
5 TerminApp 0x000000010000349e $s9TerminApp14ViewControllerC8SoundBoxyySo8NSButtonCF + 1438
6 TerminApp 0x000000010000360c $s9TerminApp14ViewControllerC8SoundBoxyySo8NSButtonCFTo + 60
7 AppKit 0x00007fff3fcf8e80 -[NSApplication(NSResponder) sendAction:to:from:] + 312
8 AppKit 0x00007fff3fd63196 -[NSControl sendAction:to:] + 86
9 AppKit 0x00007fff3fd630c8 __26-[NSCell _sendActionFrom:]_block_invoke + 136
10 AppKit 0x00007fff3fd62fca -[NSCell _sendActionFrom:] + 178
11 AppKit 0x00007fff3fd8fd4f -[NSButtonCell _sendActionFrom:] + 96
12 AppKit 0x00007fff3fd618e5 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2375
13 AppKit 0x00007fff3fd8faa0 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 698
14 AppKit 0x00007fff3fd60322 -[NSControl mouseDown:] + 791
15 AppKit 0x00007fff3fc3c16f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5724
16 AppKit 0x00007fff3fb729de -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2295
17 AppKit 0x00007fff3fb71e9f -[NSWindow(NSEventRouting) sendEvent:] + 478
18 AppKit 0x00007fff3fa116c3 -[NSApplication(NSEvent) sendEvent:] + 331
19 AppKit 0x00007fff3f9ffee8 -[NSApplication run] + 755
20 AppKit 0x00007fff3f9ef3f0 NSApplicationMain + 777
21 TerminApp 0x000000010000475d main + 13
22 libdyld.dylib 0x00007fff6e8a13d5 start + 1
)
【问题讨论】:
-
我的第一个猜测是因为您将参数作为数组的 1 个元素传递,因此 swift 将其视为单个参数。不过,我不是 swift 方面的专家。尝试分离你的命令,看看它是否有效
-
您是否尝试过将
open /System/Library/CoreServices/PowerChime.app &作为单独的命令运行,而不是作为defaults的参数的一部分? -
如果你想直接调用 bash 命令而不像以前那样使用参数分隔,创建一个像 this answer 这样的自定义函数可能是一个解决方案。
-
我已经很接近了,但还是失败了
-
如果您在终端中输入
say hello,它可以工作吗?你是说shell("echosay hello")吗?另外,可能想做["-l", "-c", command]?
标签: swift bash shell process nstask