Skip to content

Instantly share code, notes, and snippets.

@hunterjsb
Created April 22, 2024 20:37
Show Gist options
  • Select an option

  • Save hunterjsb/671497551c08fb4b1fe2ac9a810e18b7 to your computer and use it in GitHub Desktop.

Select an option

Save hunterjsb/671497551c08fb4b1fe2ac9a810e18b7 to your computer and use it in GitHub Desktop.
Toggle Monitors One/All
#!/bin/bash
echo "Debug: Starting monitor toggle script"
# Get the current state of the monitors
monitors=$(xrandr --query | grep " connected" | awk '{print $1}')
echo "Debug: Connected monitors: $monitors"
# Check if all monitors are in use
all_on=0
for monitor in $monitors; do
status=$(xrandr --query | grep "^$monitor" | grep -c "+")
all_on=$((all_on + status))
done
echo "Debug: Number of monitors in use: $all_on"
if [ "$all_on" -eq "4" ]; then
echo "Debug: All monitors are in use"
# Turn off all monitors except the primary one
primary_monitor=$(xrandr --query | grep " connected primary" | awk '{print $1}')
echo "Debug: Primary monitor: $primary_monitor"
for monitor in $monitors; do
echo "Debug: Processing monitor: $monitor"
if [ "$monitor" != "$primary_monitor" ]; then
echo "Debug: Turning off monitor: $monitor"
xrandr --output "$monitor" --off
else
echo "Debug: Skipping primary monitor: $monitor"
fi
done
echo "Monitors turned off except the primary one."
else
echo "Debug: Not all monitors are in use"
# Turn on all monitors
for monitor in $monitors; do
echo "Debug: Turning on monitor: $monitor"
xrandr --output "$monitor" --auto
done
echo "All monitors turned on."
fi
echo "Debug: Monitor toggle script completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment