Skip to content

Instantly share code, notes, and snippets.

@seocam
Last active November 3, 2023 13:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seocam/3a0c82349c6e4df610615550bb1741ae to your computer and use it in GitHub Desktop.
Save seocam/3a0c82349c6e4df610615550bb1741ae to your computer and use it in GitHub Desktop.
Script to swap display positions on OS X
#!/bin/bash
# Usage: ./swap-displays.sh 0 1
# Swaps display 0 (laptop display) position with the display 1 position.
# Also works for more displays, for example:
# ./swap-displays.sh 1 2
# Must install displayplacer before using:
# https://github.com/jakehilborn/displayplacer
# WARNING: If the displays don't have the exact same config this command might fail
DISPLAYPLACER_OUT=`displayplacer list`
declare -a DISPLAYS=( $(echo "$DISPLAYPLACER_OUT" | grep Persistent | cut -d: -f2 | xargs) )
CURRENT_STATE=$( echo "$DISPLAYPLACER_OUT" | tail -1)
SWAP_COMMAND=$(
echo $CURRENT_STATE \
| sed s/${DISPLAYS[$1]}/FIRST_SCREEN/g \
| sed s/${DISPLAYS[$2]}/SECOND_SCREEN/g \
| sed s/FIRST_SCREEN/${DISPLAYS[$2]}/g \
| sed s/SECOND_SCREEN/${DISPLAYS[$1]}/g
)
eval $SWAP_COMMAND
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment