2

I’m deploying a NetRestore image to multiple kinds of Macs: MacBooks, MacBook Pros, Mac Pros, iMacs and Mac Minis. I have several custom designed login screens that I’d like to use but everything before 10.10 requires that you know the monitor resolution otherwise the login background won't show correctly.

Is there a way to use the output of this code as a variable for a series of if/then statements?

system_profiler SPDisplaysDataType |grep Resolution

The normal output shows like this

Resolution: 2560 x 1440
Resolution: 1920 x 1200
Resolution: 1920 x 1200

(This is if you have three monitors… I’d prefer to use the first result).

My intention is for the script to run when binding the Mac to Active Directory—which has to be done manually—so the resolution should already be available. It would select the correct resolution image and then copy it into the required location.

Or by chance does anyone have any good idea on how to create custom login window backgrounds and user backgrounds to work on any type of Mac with any resolution just like the OS does when you select “Fit to Screen” in System Preferences?

1 Answer 1

2

There are so many ways to do this but if you want to stick to only/mostly bash, here's two approaches.

  • system_profiler has an option to output to XML. Use this to put the output to a variable or file like so making sure to include the double quotes:

    someVar="$(system_profiler SPDisplaysDataType -xml)"
    
  • This returns an array under _items called _IODisplayEDID which lists the display name as _name and the resolution as _IODisplayEDID

  • If you're committed/required to keep it in bash, here's a good response on StackOverflow.com for How to parse XML in bash that I won't rehash. But at this point you should consider using an XML parser link XMLStartlet or xsltproc which is included in the Xcode Dev Tools.

All that said, you can still go the grep route but have to make some assumption on the overall length of the output and the placement of "Resolution" to the display you're interested in. Here's a crude test based on my 2 display system:

  • First get the Display names. To account for spaces in the name, you can temporarily reset $IFS to use only a newline then use grep -A to get 50 lines (a guesstimate) after "Displays", not return the first line and get the name values since they end in : (that's a lot of assumptions to make!)

    IFStemp=${IFS}
    IFS=$'\n'
    var=($(system_profiler SPDisplaysDataType|grep -A 50 \ Displays|grep -v Displays|egrep ':$'))
    
  • Then use this to get guesstimate where "Resolution" will be. You can obviously sub printf for a variable name but I wanted to show the output. I used sed to trim the leading spaces.

    for displayName in ${var[@]}; do
       printf '%s = %s\n' "${displayName%?}" "$(system_profiler SPDisplaysDataType|grep -A 3 "${displayName}"|grep Resolution|sed -e 's/^[[:space:]]*//g' -e 's/Resolution\:\ //g')"
    done
    
  • Now reset you $IFS

    IFS=${IFStemp}
    unset IFStemp
    

And here's what the output looked like for me:

Color LCD = 2880 x 1800 Retina
DELL 2007WFP = 1920 x 1200 @ 60 Hz

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .