Determine multiple display layout via bash script

Feb 11, '09 07:30:00AM

Contributed by: Kerrnel

I searched far and wide, but only found third party utility references (an app called cscreen, for one) to get the current display layout programmatically in a script (AppleScript / bash). This could be easily munged into AppleScript, but here's what I came up with for a bash function that output something like:

Display Unit: 0 with height 900 and width 1440 is positioned at (0, 0)
Display Unit: 1 with height 1024 and width 1280 is positioned at (503, -1024)


However, you can massage the output to suit your needs. The script only returns the last set-up displays. In other words, if a display is hot-unplugged, the script will still report the last layout set in System Preferences.

Here's the script; it's only been tested on 10.5.6:

#!/bin/sh

DisplayListMac()
 {
    defaults read /Library/Preferences/com.apple.windowserver 'DisplaySets' | awk 'BEGIN { aDepth=0; iDepth=0; aNum=0; iNum=0; dID="" }
    /\(/ {
            ++aDepth
            iNum=0
#            print "D:" aDepth
         }
    /\)/ {
            --aDepth
            ++aNum
#            print "D:" aDepth " N:" aNum
         }
    /\{/ {
            ++iDepth
#            print "I:" iDepth
            if (dID != "")
             {
                print dUnit ":" dX ":" dY ":" dW ":" dH
                dID=""
             }
         }
    /\}/ {
            --iDepth
            ++iNum
#            print "I:" iDepth " N:" iNum
         }
    /=/ {
#            print "K:" $1 ":" $3 ":"
            if (aNum == 0)
             {
                # Remove semicolon
                v=substr($3, 1, length($3) - 1)
                if ($1 == "Height" )
                    dH = v
                else if ($1 == "Width" )
                    dW = v
                else if ($1 == "OriginX" )
                    dX = v
                else if ($1 == "OriginY" )
                    dY = v
                else if ($1 == "DisplayID")
                    dID = v
                else if ($1 == "Active")
                    dAct = v
                else if ($1 == "Depth")
                    dDepth = v
                else if ($1 == "Unit")
                    dUnit = v
             }
        }
'
 }

for i in $(DisplayListMac); do
    u=${i%%:*}
    rest=${i#*:}
    x=${rest%%:*}
    rest=${rest#*:}
    y=${rest%%:*}
    rest=${rest#*:}
    w=${rest%%:*}
    rest=${rest#*:}
    h=${rest%%:*}

    echo "Display Unit: $u with height $h and width $w is positioned at ($x, $y)"
done
[robg adds: To use the script, copy and paste the above into vi, emacs, BBEdit, TextEdit, or other pure-text editor. Save the file with a meaningful name (screeninfo). Then, in Terminal, cd to the directory where you saved the file, and run this command to make the file executable: chmod a+x screeninfo. You can then run the script (assuming it's been saved somewhere on your path) by typing screeninfo (or from within the current directory with ./screeninfo). It worked fine on my dual-monitor setup.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090208121119440