Skip to content

Instantly share code, notes, and snippets.

@seanlinmt
Last active February 5, 2026 11:52
Show Gist options
  • Select an option

  • Save seanlinmt/4f51a86c136cd2808159d637ab5e8a55 to your computer and use it in GitHub Desktop.

Select an option

Save seanlinmt/4f51a86c136cd2808159d637ab5e8a55 to your computer and use it in GitHub Desktop.
DDC via lunar on macOS to toggle monitors between Mac and Windows.
#!/bin/bash
# lunar ddc <monitorID> 0x60 read to identify value of the monitor. Update serial number of monitor accordingly
lunar_switch() {
local LUNAR="/Users/sean/.local/bin/lunar"
_toggle_monitor() {
local ID=$1
# We check the display list including disconnected ones
# We use 'lunar displays' to see if the ID exists in any stateĀ
local STATE=$($LUNAR ddc $ID 0x60 read)
if [ "$STATE" -eq 4 ]; then
echo "Monitor $ID: Returning to Mac..."
$LUNAR connect $ID
sleep 2
$LUNAR ddc $ID 0x60 3
else
echo "Monitor $ID: Switching to Windows..."
$LUNAR disconnect $ID
sleep 2
$LUNAR ddc $ID 0x60 4
fi
}
case "$1" in
1) _toggle_monitor 3DCC4033-D862-480F-9F40-DE36F70A3C61;;
2) _toggle_monitor 50500ABB-E757-4C77-80A1-1969AB721D00;;
all)
echo "Toggling all monitors..."
_toggle_monitor 3DCC4033-D862-480F-9F40-DE36F70A3C61 &
_toggle_monitor 50500ABB-E757-4C77-80A1-1969AB721D00 &
wait
;;
*)
echo "Usage: lunar_switch {1|2|all}"
return 1
;;
esac
}
# Only execute if run directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]] || [[ "$ZSH_EVAL_CONTEXT" == "toplevel" ]]; then
lunar_switch "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment