golang exec osascript没有调用。

huangapple go评论77阅读模式
英文:

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()

请注意,我只翻译了代码部分,其他内容不做翻译。

英文:

I wrote a command that sends a text, but it's not working, even though if I paste the command in it does. Is there some syntax error or thing i'm missing?

The printed command is:
/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'

my code is:

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()

答案1

得分: 3

Command文档中可以看到:

返回的Cmd的Args字段是由命令名称和arg的元素构成的,因此arg不应包含命令名称本身。例如,Command("echo", "hello").Args[0]始终是名称,而不是可能已解析的路径。

因此,你应该这样做:

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()
英文:

From the 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.

So, you should do something like:

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()

huangapple
  • 本文由 发表于 2017年7月22日 07:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/45248144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定