duannaoye0732 2011-10-21 04:57 采纳率: 100%
浏览 251
已采纳

正确将参数传递给Go Exec

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.

  • 写回答

3条回答 默认 最新

  • dongpao5261 2011-11-06 05:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有卷积神经网络识别害虫的项目吗
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件