Search code examples
macososx-mavericksresolutionosx-yosemite

Determine and Use Resolution as a Variable in UNIX script on Mac


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 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).

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)

Thanks!


Solution

  • To use the first result, store the height and width in variables, and then do something, you can use head like so:

    $ system_profiler SPDisplaysDataType | grep Resolution | head -1
    

    Then you can chop off the resolution with awk and assign it to a bash variable:

    $ resolution=$(system_profiler SPDisplaysDataType | grep Resolution | head -1 | awk -F: '{print $2}')
    

    Finally, you can an if statement on the value of the resolution variable like so:

    $ if [[ $resolution == " 2560 x 1440" ]]; then \
          echo match; \
      else \
          echo no; \
      fi
    match