英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论