Monday 29 July 2019

linux - How to get the current monitor resolution or monitor name (LVDS, VGA1, etc)


I would like to get the resolution of the current monitor (the screen from where I run the script) or the name of the screen (LVDS, VGA1, etc).


If I can't get the resolution but only the monitor name, I could grep 'xrandr -q' output to get current resolution.


Thanks in advance.



Answer



You should be able to do this by a combination of xrandr and xwininfo.




  1. Get the screens, their resolutions and offsets:


    $ xrandr | grep -w connected  | awk -F'[ \+]' '{print $1,$3,$4}'
    VGA-0 1440x900 1600
    DP-3 1600x900 0


  2. Get the position of the current window


    $ xwininfo -id $(xdotool getactivewindow) | grep Absolute
    Absolute upper-left X: 1927
    Absolute upper-left Y: 70


So, by combining the two you should be able to get the resolution of the current screen:


#!/usr/bin/env bash

## Get screen info
screen1=($(xrandr | grep -w connected | awk -F'[ +]' '{print $1,$3,$4}' |
head -n 1))
screen2=($(xrandr | grep -w connected | awk -F'[ +]' '{print $1,$3,$4}' |
tail -n 1))

## Figure out which screen is to the right of which
if [ ${screen1[2]} -eq 0 ]
then
right=(${screen2[@]});
left=(${screen1[@]});
else
right=(${screen1[@]});
left=(${screen2[@]});

fi

## Get window position
pos=$(xwininfo -id $(xdotool getactivewindow) | grep "Absolute upper-left X" |
awk '{print $NF}')

## Which screen is this window displayed in? If $pos
## is greater than the offset of the rightmost screen,
## then the window is on the right hand one
if [ "$pos" -gt "${right[2]}" ]
then
echo "${right[0]} : ${right[1]}"
else
echo "${left[0]} : ${left[1]}"
fi

The script will print the name and resolution of the current screen.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...