1

I'm working on a project involving Apple Script and I can not manage to set it up correctly in the GitLab runner. No matter what I do it seems the gitlab-runner does not see any running applications, see following execution (Terminal is also running, I see it open in by screen sharing):

$ ps -axj | grep applee    # To check System Events is running
_appleevents       349     1   349      0    1 S      ??    0:05.01 /usr/sbin/distnoted agent
main         71925 71816 71810      0    1 S      ??    0:00.00 grep applee
_appleevents     77279     1 77279      0    0 Ss     ??    0:00.56 /System/Library/CoreServices/appleeventsd --server
$ osascript -e 'tell application "Terminal" to do script "echo hello"' || true
31:53: execution error: Terminal got an error: Application isn’t running. (-600)
$ osascript -e 'tell application "System Events" to get every process' || true
40:53: execution error: System Events got an error: Application isn’t running. (-600)

same thing happens if I try to access by application id as suggested here

$ ps -axj | grep applee     # To check System Events is running    
_appleevents       349     1   349      0    1 S      ??    0:05.03 /usr/sbin/distnoted agent
main         76150 76026 76020      0    1 S      ??    0:00.00 grep applee
_appleevents     77279     1 77279      0    0 Ss     ??    0:00.60 /System/Library/CoreServices/appleeventsd --server
$ osascript -e 'tell application id "com.apple.terminal" to do script "echo hello"' || true
44:66: execution error: Terminal got an error: Application isn’t running. (-600)
$ osascript -e 'tell application id "com.apple.systemevents" to get every process' || true
52:65: execution error: System Events got an error: Application isn’t running. (-600)

If I access the runner via ssh or Screen Sharing and I launch any of these commands in the ssh/Terminal, they work perfectly in both methodologies shown above.

I've also tried to kill the System Events process so it would be respawned and nothing changed. Tried also to run osascript with sudo in front, nothing changed either.

Moreover, it's not a matter of settings:

  • gitlab-runner, AEServer and Terminal have all Accessibility permissions enabled.

  • gitlab-runner and Terminal have Automation permissions enabled for the 'System Events' application.

I do not know what else to try or how to configure the gitlab-runner so osascript can see the running applications. Any help or idea would be appreciated.

Meru
  • 77
  • 13
  • 1
    What happens if you launch system events in a prior osascript command? e.g. `osascript -e 'tell application id "com.apple.systemevents" to launch' -e 'tell application id "com.apple.systemevents" to get every process' || true`. Otherwise, any chance that the incorrect system events is being called upon (as in local or remote)? Sorry… not familiar with gitlab-runner. – Mockman Mar 18 '23 at 05:58
  • Not working unfortunately but seems to be the "way to do it". According to this link,the issue could be a timing issue: https://www.reddit.com/r/applescript/comments/ll0zti/applescript_problem_since_updating_mac/ Will try to add a wait loop and see how it goes. Thank you for your comment @Mockman – Meru Mar 21 '23 at 11:23

1 Answers1

1

For anyone interested, at the end it was a matter of a race condition: it seems that osascript was faster to launch the commands for "System Events" than "system events" was ready after opening.

The solution is as simple as adding this section to any script I launch:

if application "System Events" is not running then
   log "System events wasn't running"
   launch application "System Events"
   delay 0.5
end if
    
if application "System Events" is not running then
    repeat 10 times
        if application "System Events" is not running then
            log "Not running yet"
            delay 1
        else
            log "Now is running"
            exit repeat
        end if
    end repeat
else
    log "Now is running"
end if

The logs are not necessary but help keep nice track. Also for some reason the logging in osascript is launched directly to stderr instead of stdout, just so you know folks :)

Meru
  • 77
  • 13