peter
peter

Reputation: 43

golang exec osascript not calling

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

Upvotes: 0

Views: 1087

Answers (1)

putu
putu

Reputation: 6444

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

Upvotes: 3

Related Questions