thetall0ne
thetall0ne

Reputation: 29

How to fix 'syntax error: A unknown token can’t go here' when running `exec.Command` in Go script

I am trying to run this Apple Script command with osascript in my Go script and I'm getting the error 0:1: syntax error: A unknown token can’t go here. (-2740).

This is the command that, when run in Terminal, works great!

/usr/bin/osascript -e 'on run {f, c}' -e 'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end "/Users/computerman/Desktop/testfile.png" "Hello, World"

My Go script below actually outputs the above string, and I can literally cut and paste it in Terminal and it works. However, running the Go script itself I get the aforementioned error.

Please help!

This is on MacOS 10.14.4 (18E2034). I tried replacing the fmt.Sprintf with simpler string with \"Finder"\ and have the same exact issue.


import (
    "fmt"
    "log"
    "os"
    "os/exec"
)

func main() {
    filepath := "/Users/computerman/Desktop/testfile.png"
    comment := "Hello, World"
    onrun := "'on run {f, c}'"
    command := fmt.Sprintf(`'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end "%s" "%s"`, filepath, comment)
    log.Println("/usr/bin/osascript", "-e", onrun, "-e", command)
    cmd := exec.Command("/usr/bin/osascript", "-e", onrun, "-e", command)
    cmd.Stderr = os.Stderr
    cmd.Stdout = os.Stdout
    cmd.Stdin = os.Stdin

    err := cmd.Run()
    if err != nil {
        log.Println(err)
    }
}

When I do go run main.go I get this in Terminal

2019/04/17 13:02:28 exit status 1

I expect the output of

Hello, World

And no errors. Plus, the comment field in the file to be updated with Hello, World.

Upvotes: 0

Views: 3968

Answers (2)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54089

You need to break out the -e on the end of command too and the comment, something like this. This makes the same number of parameters as in the original command hopefully.

func main() {
    filepath := "/Users/computerman/Desktop/testfile.png"
    comment := "Hello, World"
    onrun := "'on run {f, c}'"
    command := fmt.Sprintf(`'tell app "Finder" to set comment of (POSIX file f as alias) to c'`)
    end := fmt.Sprintf(`end "%s")`, filepath)
    log.Println("/usr/bin/osascript", "-e", onrun, "-e", command, "-e", end, comment)
    cmd := exec.Command("/usr/bin/osascript", "-e", onrun, "-e", command, "-e", end, comment)
    cmd.Stderr = os.Stderr
    cmd.Stdout = os.Stdout
    cmd.Stdin = os.Stdin

    err := cmd.Run()
    if err != nil {
        log.Println(err)
    }
}

Upvotes: 0

thetall0ne
thetall0ne

Reputation: 29

Thanks to the previous comments, I discovered the answer. Here is the correct code that works. Thanks to Adrian for pointing out that you don't need the single quotes, so I removed them, and thanks to Nick for pointing out that the end flag needed to be separate from that string.

func main() {
    filepath := "/Users/computerman/Desktop/testfile.png"
    comment := "Hello, World"
    onrun := "on run {f, c}"
    command := fmt.Sprintf(`tell app "Finder" to set comment of (POSIX file f as alias) to c`)
    //log.Println("/usr/bin/osascript", "-e", onrun, "-e", command)
    cmd := exec.Command("/usr/bin/osascript", "-e", onrun, "-e", command, "-e", "end", filepath, comment)
    cmd.Stderr = os.Stderr
    cmd.Stdout = os.Stdout
    cmd.Stdin = os.Stdin

    err := cmd.Run()
    if err != nil {
        log.Println(err)
    }
}

It works!

Upvotes: 1

Related Questions