Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Determine multiple display layout via bash script UNIX
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.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (5 votes cast)
 
[10,900 views]  

Determine multiple display layout via bash script | 6 comments | Create New Account
Click here to return to the 'Determine multiple display layout via bash script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Determine multiple display layout via bash script
Authored by: kd35a on Feb 11, '09 11:47:57AM
It doesn't work correctly for me, I get the following output:
Display Unit: 1 with height 900 and width 1600 is positioned at (0, 0)
Display Unit: 0 with height 800 and width 1280 is positioned at (0, 900)
when screen 1 has the resolution 1920x1200 (which is what my System Preferences also tells me), but otherwise it's correct.

[ Reply to This | # ]
Not as glamourous but a one-liner
Authored by: SOX on Feb 11, '09 02:32:40PM
defaults read /Library/Preferences/com.apple.windowserver | perl -nwe 'chomp;chop;print "$_\n" if m/^\s+(Height|Width|OriginX|OriginY|Unit)/; print "\n" if m/\}/'
This prints out groupings like this:

                Height = 1024
                OriginX = 0
                OriginY = 0
                Unit = 1
                Width = 1600

                Height = 900
                OriginX = 336
                OriginY = 1024
                Unit = 0
                Width = 1440


[ Reply to This | # ]
Not as glamourous but a one-liner
Authored by: Cerberus on Feb 21, '09 08:40:18AM

This works for me, but the main one does not... I am usually a good debuger but AWK gives me a headache ;-)



[ Reply to This | # ]
Determine multiple display layout via bash script
Authored by: mzs on Feb 12, '09 01:03:21AM
Parsing the system_profiler command has worked very well for me since 10.4:

$ system_profiler SPDisplaysDataType
Graphics/Displays:

    ATI Radeon X1600:

      Chipset Model: ATY,RadeonX1600
      Type: Display
      Bus: PCIe
      PCIe Lane Width: x16
      VRAM (Total): 128 MB
      Vendor: ATI (0x1002)
      Device ID: 0x71c5
      Revision ID: 0x0000
      EFI Driver Version: 01.00.068
      Displays:
        iMac:
          Resolution: 1680 x 1050
          Depth: 32-bit Color
          Core Image: Hardware Accelerated
          Main Display: Yes
          Mirror: Off
          Online: Yes
          Quartz Extreme: Supported
          Display Asleep: Yes
          Built-In: Yes
        DELL 1901FP:
          Resolution: 1280 x 1024 @ 75 Hz
          Depth: 32-bit Color
          Core Image: Hardware Accelerated
          Mirror: Off
          Online: Yes
          Quartz Extreme: Supported
          Rotation: Supported
          Display Asleep: Yes
It doesn't tell you the screen origins though.

[ Reply to This | # ]
some applescript code mixed with some one-line shell scripts
Authored by: simondorfman on Feb 13, '09 12:17:26PM
I've been working on updating this script to give a choice for a second monitor. This is what I'm using right now:

set screenWidth to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set screenHeight to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
set secondMonitorWidth to word 6 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set secondMonitorHeight to word 6 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number


[ Reply to This | # ]
Determine multiple display layout via bash script
Authored by: cluthi on Feb 19, '09 02:03:11AM
Let's post yet another code that do the same thing.
    NSUInteger i;
    for (NSScreen *screen in [NSScreen screens])
    {
        NSSize size = [screen frame].size;
        NSPoint origin = [screen frame].origin;
        printf("Display Unit %d: %g x %g is positioned at (%g, %g)n", i++, size.width, size.height, origin.x, origin.y);
    }


[ Reply to This | # ]