2

I am running a golang app on Macos. It has some codes like the following:

for {
    time.Sleep(time.Second * 5)
    cmd := exec.Command("/usr/bin/osascript", "-e", `display dialog "hello" with title "hello"`)
    err := cmd.Run()
}

It works fine if I don't lock the screen (when the screen is always on). But the code err := cmd.Run() will hang forever if the screen is locked and off when that line executes. When I unlock the screen (turn it on), the for loop just hangs there forever and will never continue its execution.

I am not sure if this issue belongs to golang or how MacOS handles osascript. Can anyone please tell me how to workaround it? Thanks a lot.

PS: I use the same code in Linux and replace /usr/bin/osascript to /usr/bin/xmessage and this always works fine without any problems even if the screen is locked/off in Linux.

Edited:

My solution, use chrome instead:

cmd := exec.Command(`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`, "-new-window", "/path/hello.html")
sgon00
  • 4,879
  • 1
  • 43
  • 59

2 Answers2

4

It seems to be related to how MacOS puts processes in idle when the screen locks. It makes the osasscriptchild process never finish execution and block the for loop.

One thing you can do is run the command with a timeout context. I have tried and it works. Execution will resume when the screen is unlocked and timeout expires.

Example:

package main

import (
    "context"
    "fmt"
    "os/exec"
    "time"
)

func main() {
    for {
        time.Sleep(time.Second * 5)

        // run your command with a timeout
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        cmd := exec.CommandContext(
            ctx,
            "/usr/bin/osascript",
            "-e",
            `display dialog "hello" with title "hello"`,
        )

        err := cmd.Run()
        if err != nil {
            fmt.Println(err)
        }
        // don't forget to cancel your context to avoid context leak
        cancel()
    }
}

Alternatively, if you don't want a timeout you can check if the screen is locked before trying to call display dialog.

package main

import (
    "context"
    "fmt"
    "os"
    "os/exec"
    "strings"
    "time"
)

func main() {
    for {
        time.Sleep(time.Second * 5)

        ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
        cmd := exec.CommandContext(
            ctx,
            "python",
            "-c",
            "import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); print d",
        )

        var err error
        var b []byte
        if b, err = cmd.CombinedOutput(); err != nil {
            cancel()
            continue
        }
        cancel()

        // if screen is not locked
        if !strings.Contains(string(b), "CGSSessionScreenIsLocked = 1") {
            cmd = exec.Command(
                "/usr/bin/osascript",
                "-e",
                "display dialog \"Hello\"",
            )
            cmd.Stdout = os.Stdout
            cmd.Stderr = os.Stderr

            err = cmd.Run()
            if err != nil {
                fmt.Println("err: ", err)
            }
        }
    }
}
Francois
  • 3,050
  • 13
  • 21
  • Thanks a lot for the answer. It works, but there is a problem. I need the dialog to be present forever until I press the "OK" button in the dialog when the screen is NOT off. With the solution, the dialog will be gone after a timeout. Are there any better workaround? Otherwise, i have to give a very big timeout value which is not very ideal. Thanks. – sgon00 Mar 24 '19 at 07:58
  • Oh sure there's a solution. Give me a few minutes. – Francois Mar 24 '19 at 08:01
  • Thank you very much. Take your time. ^_^ – sgon00 Mar 24 '19 at 08:03
  • It seems the problem is specifically with `display dialog` I have updated the code with a second example which checks if the screen is locked before trying to call display dialog. – Francois Mar 24 '19 at 09:49
  • Thank you very much for your code and time. I didn't realize this problem only happens to osascript before. Now, I am using `cmd := exec.Command(\`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\`, "-new-window", "/path/hello.html")` and so far so good. I think I will use browser for now. :) – sgon00 Mar 24 '19 at 10:40
  • Btw, I have upvoted and marked your answer as the answer. Thanks. – sgon00 Mar 24 '19 at 10:41
  • Exactly what i needed! – danfromisrael Dec 21 '20 at 20:23
0

You can just use & at the end of your command which will cause the program to run in the background.

 for {
    time.Sleep(time.Second * 5)
    cmd := exec.Command("/usr/bin/osascript", "-e", `display dialog "hello" with title "hello"`, "&")
    err := cmd.Run()
}
Baruch
  • 1