#!/bin/bash
# secure-input-watch.sh: prints a line when macOS Secure Input turns on or off,
# and when the app in front changes while it is on. Reads only. Ctrl+C to stop.
#
# Look at the time, not at a name: in our tests macOS named the app in front,
# not a background app that held it. What did you open just before "ON"?
# From useyappr.com/blog/dictation-hotkey-not-working-mac

front() { lsappinfo info -only name "$(lsappinfo front)" | cut -d'"' -f4; }

echo "Terminal's Secure Keyboard Entry: $( [ "$(defaults read com.apple.Terminal SecureKeyboardEntry 2>/dev/null)" = 1 ] && echo ON || echo off ) (iTerm2: see its iTerm2 menu)"
echo "Watching. Leave this open and use your Mac."
was=start
while :; do
  if ioreg -l -d 1 -w 0 | grep -q -a '"kCGSSessionSecureInputPID"'; then now="ON   in front: $(front)"; else now="off"; fi
  if [ "$now" != "$was" ]; then echo "$(date +%T)  $now"; was=$now; fi
  sleep 0.5
done
