OSX ScreenSaver detection and event

Hi

I would like to trgger some automations based on my MacBook Pro turning on its Screen saver. Is anyone aware of a way you can fire a script when this event happens? The best I have come up with so far is this.

#!/bin/bash

idle_time=$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}')

# if the screensaver is running then we don't need to check the
# idle_time because we already know we don't want to display anything
if pgrep -q ScreenSaverEngine; then
    echo "ScreenSaverEngine is running."
elif [[ "${idle_time}" > 299 ]]; then
    echo "idle_time is 5 minutes."
else
    echo "The display is unlocked."
fi

so with that I could turn it into a launchctrl cron/server/daemon on my mac and get it to fire curls. Just wondering if there is a better way?

check out

then let scenario execute a script that curls the HA API or send an mqtt command via comannd line to set a binary sensor in HA.

hhmmm, $8… now is it worth it? :slight_smile:

I use sleepwatcher to trigger some scripts when my Mac goes/returns to sleep. It also can get used to check idleness.

there’s also

that can monitor events like screen saver start and stop, but requires a little more elaborate scripting and skills.

scenario was pretty easy to setup and works fine, so for me the $8 were worth it. sleepwatcher also works fine, though i suspect that it’s hammering to many resources when running in daemon mode.