Skip to content

Instantly share code, notes, and snippets.

@markdon
Created September 28, 2020 23:34
Show Gist options
  • Save markdon/6d13190d4518f16e61c104a1fbe1606d to your computer and use it in GitHub Desktop.
Save markdon/6d13190d4518f16e61c104a1fbe1606d to your computer and use it in GitHub Desktop.
Automate swapping position of two external displays with displayplacer. You will need to customise your builtin ID and displayplacer commands.
#!/bin/bash
BASEDIR=$(dirname "$0");
CONFIG_FILE=$BASEDIR/SWAP_DISPLAY_CONFIG;
DISPLAY_CONFIG=`cat $CONFIG_FILE`;
if [ -z "$DISPLAY_CONFIG" ]
then DISPLAY_CONFIG=0;
fi
if [ "$DISPLAY_CONFIG" == "0" ]
then DISPLAY_CONFIG="1";
else DISPLAY_CONFIG="0";
fi
echo config:$DISPLAY_CONFIG
# record the current config to file
touch $CONFIG_FILE;
echo $DISPLAY_CONFIG > $CONFIG_FILE;
# Find screen ids
dplist=`displayplacer list | tail -n 1`
ids=`displayplacer list | tail -n 1 | egrep -o 'id:[A-F0-9-]+' | egrep -o '[0-9A-F-]{36}'`
# Sort the ids so they are in consistent order
# https://stackoverflow.com/questions/7442417/how-to-sort-an-array-in-bash
IFS=$'\n' ids=($(sort <<<"${ids[*]}")); unset IFS
for id in ${ids[@]}; do
if [ "$id" != "$builtin" ]
then
if [ -z "$screenone" ]
then
screenone=$id
else
screentwo=$id
fi
fi
done
builtin=34CF6778-5B8D-0DE8-96DF-EB47172E8121
echo builtin:$builtin
echo screenone:$screenone
echo screentwo:$screentwo
case $DISPLAY_CONFIG in
0)
displayplacer \
"id:$screentwo res:2560x1440 hz:60 color_depth:8 scaling:off origin:(0,0) degree:0" \
"id:$screenone res:1440x2560 hz:60 color_depth:8 scaling:off origin:(-1440,-540) degree:90" \
"id:$builtin res:1792x1120 hz:59 color_depth:8 scaling:on origin:(402,1440) degree:0"
;;
1)
displayplacer \
"id:$screenone res:2560x1440 hz:60 color_depth:8 scaling:off origin:(0,0) degree:0" \
"id:$screentwo res:1440x2560 hz:60 color_depth:8 scaling:off origin:(-1440,-540) degree:90" \
"id:$builtin res:1792x1120 hz:59 color_depth:8 scaling:on origin:(402,1440) degree:0"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment