2017-07-21 2 views
0

J'ai écrit une commande qui envoie un texte, mais cela ne fonctionne pas, même si je colle la commande à l'intérieur. Y a-t-il une erreur de syntaxe ou une chose qui me manque?golang exec osascript ne pas appeler

La commande imprimée est: /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'

mon code est:

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

Répondre

3

De l'Command documentation:

Le retourné champ args de Cmd est construit à partir du nom de la commande suivi de la elements de arg, donc arg ne doit pas inclure le nom de la commande elle-même. Par exemple, Commande ("echo", "hello"). Args [0] est toujours le nom, pas le chemin possiblement résolu.

Donc, vous devriez faire quelque chose comme:

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