3

I'm trying to learn go and as a start I wanted to try to throw together a super simple web server for controlling my iTunes. I've used osascript -e 'Tell Application "iTunes" to playpause' to this purpose many times in the past and thought I could simply sluff the call off to osascript here.

The commented out "say 5" command does work.

package main

import "exec"
//import "os"

func main() {

    var command = "Tell Application 'iTunes' to playpause"
    //var command = "say 5"

    c := exec.Command("/usr/bin/osascript", "-e", command)
//  c.Stdin = os.Stdin
    _, err := c.CombinedOutput()
    println(err.String());


}

The response I am receiving from this is as follows -

jessed@JesseDonat-MBP ~/Desktop/goproj » ./8.out
exit status 1
[55/1536]0x1087f000

I'm not exactly sure where to go from here and any direction would be greatly appreciated.

donatJ
  • 3,105
  • 3
  • 32
  • 51

3 Answers3

8

I got it working with this

package main

import (
    "fmt"
    "exec"
)

func main() {
    command := "Tell Application \"iTunes\" to playpause"

    c := exec.Command("/usr/bin/osascript", "-e", command)
    if err := c.Run(); err != nil {
        fmt.Println(err.String())
    }
}

I think exec.Command(...) adds double quotes to the parameters if there is spaces in them, so you only need to escape \" where you need them.

Squeeseio
  • 96
  • 3
  • Escaping quotes is only needed when you type things into a command shell. Go is explicitly starting a program and passing each parameter as an individual value. Each argument (parameter) is it's own entry in an array of arguments. An individual argument may contain spaces or quotes without any escaping. – Gabe Oct 08 '15 at 02:02
1

Your are probably just missing quotes. Try:

var command = "\"Tell Application 'iTunes' to playpause\""

Also, instead of println, idiomatic go usually looks like:

if err != nil {
    fmt.Println(err.String());
}
amattn
  • 10,045
  • 1
  • 36
  • 33
  • Well I tried that and got a new one - panic: runtime error: invalid memory address or nil pointer dereference [signal 0xa code=0x2 addr=0x14 pc=0x20a8] runtime.panic+0x92 /Users/jessed/go/src/pkg/runtime/proc.c:1254 runtime.panic(0x33788, 0x1048a3d8) – donatJ Oct 22 '11 at 02:07
  • did you chat the line with err? if err is nil, you would get a panic on the line with println – amattn Oct 22 '11 at 02:53
0

Try

c := exec.Command("/usr/bin/osascript", "-e", "say 5")
output, err := c.CombinedOutput()

or try

c := exec.Command("/usr/bin/osascript", "-e", "say 5")
c.Stdin = os.Stdin
output, err := c.CombinedOutput()

To print the error (if any) and the combined output:

if err != nil { fmt.Println(err) }
fmt.Print(string(output))
  • Thank you for the response but I'm afraid it still doesn't work and returns exit status 1 [55/1536]0x1088e000 – donatJ Oct 21 '11 at 14:24
  • I updated my answer. If it doesn't work, I don't know how to fix the problem. –  Oct 21 '11 at 16:06
  • Your answer *did* work - but now I'm trying to pass "Tell Application 'iTunes' to playpause" and its returning `exit status 1` once again - I've updated my old post with the code example I am using at current. Any help would be greatly appreciated. – donatJ Oct 21 '11 at 20:27