Skip to content

Instantly share code, notes, and snippets.

@fosron
Created May 5, 2023 09:19
Show Gist options
  • Save fosron/f3ef44a2f0be564e1f4e42b62c0528de to your computer and use it in GitHub Desktop.
Save fosron/f3ef44a2f0be564e1f4e42b62c0528de to your computer and use it in GitHub Desktop.
Stream Deck MK2 Monitor

To create an auto-run macOS service that monitors the connection of your Stream Deck MK.2 and runs the Stream Deck app when the device is connected, you can use a launchd user agent. Here's how to set it up:

  1. Create a new script monitor_stream_deck.sh in your home directory:
nano ~/monitor_stream_deck.sh
  1. Paste the following script into the file:
#!/bin/bash

# Replace this with the correct path to the Stream Deck app
STREAMDECK_APP_PATH="/Applications/Elgato Stream Deck.app"

DEVICE_NAME="Stream Deck MK.2"

while true; do
    DEVICE_FOUND=$(system_profiler SPUSBDataType -xml | xmllint --xpath "//string[text()='$DEVICE_NAME']" - 2>/dev/null)

    if [ -n "$DEVICE_FOUND" ]; then
        open -a "$STREAMDECK_APP_PATH"
        while [ -n "$DEVICE_FOUND" ]; do
            sleep 5
            DEVICE_FOUND=$(system_profiler SPUSBDataType -xml | xmllint --xpath "//string[text()='$DEVICE_NAME']" - 2>/dev/null)
        done
        osascript -e 'quit app "Stream Deck"' >/dev/null 2>&1
    fi

    sleep 5
done
  1. Make the script executable:
chmod +x ~/monitor_stream_deck.sh
  1. Create a new launchd user agent plist file:
nano ~/Library/LaunchAgents/com.user.monitor_stream_deck.plist
  1. Paste the following plist configuration into the file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.user.monitor_stream_deck</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/Users/your_username/monitor_stream_deck.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
</dict>
</plist>

Replace your_username with your actual username.

  1. Load the user agent:
launchctl load ~/Library/LaunchAgents/com.user.monitor_stream_deck.plist

The script will now run automatically when you log in to your macOS user account. It will keep checking for the Stream Deck MK.2 connection every 5 seconds. Once connected, it will launch the Stream Deck app and stop checking.

To unload the user agent, use the following command:

launchctl unload ~/Library/LaunchAgents/com.user.monitor_stream_deck.plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment