doudouchan5830 2019-07-17 15:12
浏览 2268


I am able to get the following command to open a new terminal and execute the command when I directly input it inside a terminal, but I can not get it to work when I use the exec.Commmand function in go.

osascript -e 'tell application "Terminal" to do script "echo hello"'

I think the issue lies within the double and single quotes, but I am not exactly sure what is causing the error.

c := exec.Command("osascript", "-e", "'tell", "application", `"Terminal"`, "to", "do", "script", `"echo`, `hello"'`)
if err := c.Run(); err != nil {
     fmt.Println("Error: ", err)

As of now the code is returning Error: exit status 1, but I would like the code to open a terminal window and execute the command.

  • 写回答

1条回答 默认 最新

  • douya8978 2019-07-17 16:18

    After playing some time found this:

    cmd := exec.Command("osascript", "-s", "h", "-e",`tell application "Terminal" to do script "echo test"`)

    Apparently you should give the automator script in 1 argument and also use ticks (`) as the string literals for go.

    "-s", "h" is for automator program to tell you human readable errors.

    My complete test code is as follows:

    package main
    import (
    // this is a comment
    func main() {
        // osascript -e 'tell application "Terminal" to do script "echo hello"'
        cmd := exec.Command(`osascript`, "-s", "h", "-e",`tell application "Terminal" to do script "echo test"`)
        // cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
        stderr, err := cmd.StderrPipe()
        if err != nil {
        if err := cmd.Start(); err != nil {
        slurp, _ := ioutil.ReadAll(stderr)
    ", slurp)
        if err := cmd.Wait(); err != nil {

    PS: osascript is the command line interface for Automator app in macOS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
    • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料
    • ¥15 51单片机的外部中断,按下按键后不能切换到另一个模式
    • ¥15 java连接sqlserver有问题
    • ¥15 yolov8 如何调cfg参数
    • ¥15 这个四人抢答器代码哪儿有问题?仿真程序怎么写?
    • ¥15 burpsuite密码爆破
    • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
    • ¥15 scikit安装之后import不了
    • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊