.xprofile checking if the output display exists

So I have a laptop and a external monitor which I often use at home. In order the external monitor displays the proper resolution, I added a .xprofile file in my home directory which contains:

xrandr --newmode "1368x768_60.00"   85.25  1368 1440 1576 1784  768 771 781 798 -hsync +vsync
xrandr --addmode DP-1 1368x768_60.00
xrandr --output DP-1 --mode 1368x768_60.00

This works every time I boot my laptop with the external monitor plugged in. But when the monitor isn’t plugged in and I am using only the laptop monitor, an error window shows up and says that it could not find the output display, i.e. DP-1.

Is it possible to do some checks before running the .xprofile, for example if the display exists or something like that?

Asked By: cainam

||

If you know the name of your device, in your case "DP-1," you can simply add this code around the commands you want to execute in your .xprofile file; it will check if "DP-1" is connected.

if xrandr --query | grep '^DP-1 connected'; then
  # Your code here
fi

If you are unsure which device you want to check, run xrandr --query in your terminal, and it will display all of them.

Answered By: Saxtheowl