How to get MacBook Display Size via Script

“I need to get the screen size for each device in my fleet”, I was told.

I had to scratch my head for a second and ask some followup questions. Did they mean the currently user-set resolution? The maximum possible resolution of the display?

It turns out, they wanted a way to programatically capture the size of the physical hardware display in inches for use in renaming the Apple laptops in their fleet. They needed to use the display size as part of the laptop hostnames for asset/inventory purposes.

Fair enough! How do we find that? Doesn’t Apple usually include the display size in the model name like “MacBook Air, 13-inch 2020”?

system_profiler seems like the most obvious choice to find that model name. While it does offer specs related to a display’s resolution, it does not provide the actual display size. You could certainly reverse engineer the display size using the resolution and some pixel math, but it wasn’t the approach I wanted to take and I knew it would be a headache given different PPI specs for various models. You could also create a dictionary with every single model identifier and its screen size and then match them up in a script, but that would be cumbersome and not very future proof. So we’ve got to find something else to work with.

General rule of thumb: If system_profiler doesn’t have the hardware data that you’re looking for, then ioreg probably does.

Sure enough, ioreg had the model name information I needed.

% ioreg -l
.....
"product-name" = <"MacBook Pro (16-inch, 2021)">
.....

It has the full model name, including the screen size. A quick awk command to strip the display size out of the product name, and we’re off to the races!

% ioreg -l | awk -F\" '/product-name/ {if (match($4, /[0-9]+-inch/)) {print substr($4, RSTART, RLENGTH-5)}}'

16

Seems almost too easy? I felt that way as well, so I wanted to double-check my logic against Apple’s own list of model names for MacBook Pro, MacBook Air, and MacBook. Sure enough, there were some outliers.

Screenshot of Apple MacBook Air laptops for 2020 and 2022 that did not include a display size in the model name.

Dangit. Across all of Apple’s recent laptops, there were two years when the MacBook Air did not include a display size in the model name. There’s always an edge case isn’t there? Not a problem. Those laptops have a known model identifier, and they only came in one size (13-inch), so we’ll explicitely do a check to see if the model is either of those if we don’t get a display size returned from our original query.

display_size=$(ioreg -l | awk -F\" '/product-name/ {if (match($4, /[0-9]+-inch/)) {print substr($4, RSTART, RLENGTH-5)}}')

if [[ -z $display_size ]]; then
    echo "Display size not found in model name. Looking up known models..."
    model_identifier=$(system_profiler SPHardwareDataType | awk -F': ' '/Model Identifier/ {print $2}')
    if [[ "$model_identifier" == "MacBookAir10,1" || "$model_identifier" == "Mac14,2" ]]; then
        echo "Model is an early M-series MacBook Air. Assuming display size is 13-inch."
        display_size="13"
    else
        echo "Error: Screen size not found and the device is not a recognized MacBook Air."
        exit 1
    fi
  fi

So we’re off to a good start here. We can add a quick check so the script only runs on Apple laptops, and we should have everything we need to create a display_size variable that can be re-purposed in other scripts, for naming devices, or whatever your needs may be!

#!/bin/zsh
# Determines the hardware screen size of an Apple laptop by using the Model Name (i.e. MacBook Air, 13-inch, 2024)

# Model name to determine if it's an Apple portable device.
model_name=$(system_profiler SPHardwareDataType | awk -F': ' '/Model Name/ {print $2}')

# Check if the device is an Apple laptop
if [[ "$model_name" =~ "Book" ]]; then
  # Get display size from model name
  display_size=$(ioreg -l | awk -F\" '/product-name/ {if (match($4, /[0-9]+-inch/)) {print substr($4, RSTART, RLENGTH-5)}}')
  
  # Check that a value is returned
  if [[ -z $display_size ]]; then
    echo "Display size not found in model name. Looking up known models..."
    model_identifier=$(system_profiler SPHardwareDataType | awk -F': ' '/Model Identifier/ {print $2}')
    if [[ "$model_identifier" == "MacBookAir10,1" || "$model_identifier" == "Mac14,2" ]]; then
        echo "Model is an early M-series MacBook Air. Assuming display size is 13-inch."
        display_size="13"
    else
        echo "Error: Screen size not found and the device is not a recognized MacBook Air."
        exit 1
    fi
  fi
  echo "Screen size: $display_size inches."
else
  echo "This computer is not a MacBook"
  exit 0
fi

Another Approach

Looking for something with a little more precision? Want to know if that 13-inch display is exactly 13.3 or 13.6 inches? Pico from MacAdmins Slack actually tackled this problem two years ago and developed a sweet one-liner that uses JavaScript for Automation (JXA) to find the built-in display size in mm using CGDisplayIsBuiltin and calculates its diagonal size in inches.

exact_size=$(osascript -l 'JavaScript' -e 'ObjC.import("AppKit"); for (const thisScreen of $.NSScreen.screens.js) { const thisScreenNumber = thisScreen.deviceDescription.js.NSScreenNumber.js; if ($.CGDisplayIsBuiltin(thisScreenNumber)) { const physicalScreenSize = $.CGDisplayScreenSize(thisScreenNumber); (Math.round((Math.sqrt(Math.pow(physicalScreenSize.width, 2) + Math.pow(physicalScreenSize.height, 2)) / 25.4) * 10) / 10); break } }'
)

This will give you specific display dimensions rather than a general “13-inch” or “16-inch” depending on what level of specificity you’re trying to achieve.

Hope this post helps you to get the display/screen size information you need to use in scripts, device naming, or anything else!