Skip to content

Instantly share code, notes, and snippets.

@djsegal
Created August 27, 2024 14:52
Show Gist options
  • Save djsegal/fef7237eedc24e109e1af83014138ad5 to your computer and use it in GitHub Desktop.
Save djsegal/fef7237eedc24e109e1af83014138ad5 to your computer and use it in GitHub Desktop.
Swap two identical dimension displays on MacOS using displayplacer and cliclick brew packages
#!/bin/bash
# Path to displayplacer and cliclick
DISPLAYPLACER="/opt/homebrew/bin/displayplacer"
CLICLICK="/opt/homebrew/bin/cliclick"
# Get current mouse position using cliclick
current_position=$($CLICLICK p | awk -F, '{print $1, $2}')
current_x=$(echo $current_position | awk '{print $1}')
current_y=$(echo $current_position | awk '{print $2}')
# Define monitor width
monitor_width=1920
# Perform the display swap
$DISPLAYPLACER list | tail -n1 | sed 's/displayplacer //;s/id:\(.\{36\}\) res:\([0-9x]\{7,9\}\) \(.*\)id:\(.\{36\}\) res:\2/id:\4 res:\2 \3id:\1 res:\2/g' | xargs $DISPLAYPLACER
# Calculate new mouse position with phase shift
new_x=$(( (current_x + monitor_width) % (2 * monitor_width) ))
new_y=$current_y
# Move the mouse to the new position
$CLICLICK m:$new_x,$new_y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment