Launch App with Automator when system is idle My idea is to dim the display when the system is idle for 10 seconds, and with a keystroke or trackpad activity revert to original (I want to preserve battery automatically and not using a screen saver - I need to monitor some activity but with low display light). I could not find such application so I decided to make solution with Automator.

I found application Shady which can dim display when it start and if it is active (in front) with keystroke Q will quit.

So, how can I make a script in Automator to start Shady when the system is idle for 10 seconds and then wait for any keystroke or trackpad activity, then send keystroke q to quit Shady? The script should be active all the time, so when another 10 second idle time happens, start Shady again.
If you want to actually save energy, use brightness.c instead of Shady. Shady doesn't reduce the backlight levels of displays, and LCDs usually require a similar amount of energy to display dark and bright pixels.

You can see the time since the last human input device (keyboard or mouse) action with ioreg:

ioreg -c IOHIDSystem | awk '/HIDIdleTime/{print $NF/1000000000;exit}'


If you are for example wathing a video with VLC, pmset -g assertions will contain a line like this:

   PreventUserIdleSystemSleep     1


So you might use a script like this:

#!/bin/bash

while sleep 0.5; do
  idle=$(ioreg -c IOHIDSystem | awk '/HIDIdleTime/{printf "%i",$NF/1000000000;exit}')
  current=$(brightness -l | awk '/display 0: brightness/{print $NF}')
  if [[ $idle -ge 10 ]] &amp;&amp; pmset -g assertions | grep -q 'PreventUserIdleDisplaySleep *0'; then
    [[ $current != 0.000000 ]] &amp;&amp; old=$current
    brightness 0
  else
    [[ $old ]] &amp;&amp; brightness $old
  fi
done


It doesn't make any sense to execute over ten new processes every second though.