golang exec osascript 不调用

golang exec osascript not calling

我写了一个发送文本的命令,但它不起作用,即使我将命令粘贴到其中也是如此。是否有语法错误或我遗漏了什么?

打印的命令是: /usr/bin/osascript -e 'tell application "Messages"' -e 'set mybuddy to a reference to text chat id "iMessage;+;chatXXXXXXXXXX"' -e 'send "test" to mybuddy' -e 'end tell'

我的代码是:

command := fmt.Sprintf("/usr/bin/osascript -e 'tell application \"Messages\"' -e 'set mybuddy to a reference to text chat id \"%s\"' -e 'send \"%s\" to mybuddy' -e 'end tell'", chatid, message)
fmt.Println(command)
exec.Command(command).Run()

来自Command documentation

The returned Cmd's Args field is constructed from the command name followed by the elements of arg, so arg should not include the command name itself. For example, Command("echo", "hello"). Args[0] is always name, not the possibly resolved Path.

所以,你应该这样做:

argChatID := fmt.Sprintf(`'set mybuddy to a reference to text chat id "%s"'`, chatid)
argMessage := fmt.Sprintf(`'send "%s" to mybuddy'`, message)

exec.Command("/usr/bin/osascript", "-e", `'tell application "Messages"'`, 
    "-e", argChatID, "-e", argMessage,  "-e", "'end tell'").Run()