Take dektop screenshot from macOS launch agent

85 views Asked by At

I have the following script to take a screenshot programmatically

$ cat /Users/bamboo/capture/capture 
#!/bin/bash

screencapture -o "$(dirname "$0")/screenshot.png"

When I run the script from a Terminal window for the first time, macOS asks me to give permission to the Terminal app for recording the screen. Afterwards the screenshot is stored in screenshot.png as expected.

When I try to run the same script from a launch agent, however, I only get a screenshot of the empty desktop with none of the open windows displayed.

This is the launch-agent plist

$ cat /Users/bamboo/Library/LaunchAgents/com.capture.screen.plist 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.capture.screen</string>
        <key>KeepAlive</key>
        <true/>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/bamboo/capture/capture</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardOutPath</key>
        <string>/Users/bamboo/capture/stdout.log</string>
        <key>StandardErrorPath</key>
        <string>/Users/bamboo/capture/stderr.log</string>
    </dict>
</plist>

I suspect this is a permission problem because before having granted permission for the Terminal app to record the screen, I was getting the same empty screenshot when running the script manually. However, no new entry shows up under Preferences > Security & Privacy > Privacy > Screen Recording after running the launch agent.

How can I give permission to the launch agent for recording the screen?

0

There are 0 answers