duan205571 2017-07-21 23:34
浏览 562
已采纳

golang exec osascript没有调用

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条回答 默认 最新

  • dongxiao1591 2017-07-22 03:23
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改