Get Macbook screen size from terminal/bash

6.3k views Asked by At

Does anyone know of any possible way to determine or glean this information from the terminal (in order to use in a bash shell script)?

On my Macbook Air, via the GUI I can go to "About this mac" > "Displays" and it tells me: Built-in Display, 13-inch (1440 x 900)

I can get the screen resolution from the system_profiler command, but not the "13-inch" bit. I've also tried with ioreg without success. Calculating the screen size from the resolution is not accurate, as this can be changed by the user.

Has anyone managed to achieve this?

4

There are 4 answers

0
BSUK On BEST ANSWER

I've found that there seem to be several different Apple URLs for checking this info. Some of them seem to work for some serial numbers, and others for other machines. e.g:

https://selfsolve.apple.com/wcResults.do?sn=$Serial&Continue=Continue&num=0 https://selfsolve.apple.com/RegisterProduct.do?productRegister=Y&country=USA&id=$Serial http://support-sp.apple.com/sp/product?cc=$serial (last 4 digits) https://selfsolve.apple.com/agreementWarrantyDynamic.do

However, the first two URLs are the ones that seem to work for me. Maybe it's because the machines I'm looking up are in the UK and not the US, or maybe it's due to their age?

Anyway, due to not having much luck with curl on the command line (The Apple sites redirect, sometimes several times to alternative URLs, and the -L option doesn't seem to help), my solution was to bosh together a (rather messy) PHP script that uses PHP cURL to check the serials against both URLs, and then does some regex trickery to report the info I need.

Once on my web server, I can now curl it from the terminal command line and it's bringing back decent results 100% of the time. I'm a PHP novice so I won't embarrass myself by posting the script up in it's current state, but if anyone's interested I'd be happy to tidy it up and share it on here (though admittedly it's a rather long winded solution to what should be a very simple query). This info really should be simply made available in system_profiler. As it's available through System Information.app, I can't see a reason why not.

1
clt60 On

The next script:

model=$(system_profiler SPHardwareDataType | \
/usr/bin/perl -MLWP::Simple -MXML::Simple -lane '$c=substr($F[3],8)if/Serial/}{
print XMLin(get(q{http://support-sp.apple.com/sp/product?cc=}.$c))->{configCode}')

echo "$model"

will print for example:

MacBook Pro (13-inch, Mid 2010)

Or the same without perl but more command forking:

model=$(curl -s http://support-sp.apple.com/sp/product?cc=$(system_profiler SPHardwareDataType | sed -n '/Serial/s/.*: \(........\)\(.*\)$/\2/p')|sed 's:.*<configCode>\(.*\)</configCode>.*:\1:')
echo "$model"

It is fetched online from apple site by serial number, so you need internet connection.

3
dibi On

I think you could only get the display model-name which holds a reference to the size:

ioreg -lw0 | grep "IODisplayEDID" | sed "/[^<]*</s///" | xxd -p -r | strings -6 | grep '^LSN\|^LP'

will output something like:

LP154WT1-SJE1

which depends on the display manufacturer. But as you can see the first three numbers in this model name string imply the display-size: 154 == 15.4''

EDIT

Found a neat solution but it requires an internet connection:

curl -s http://support-sp.apple.com/sp/product?cc=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -c 9-` |
sed 's|.*<configCode>\(.*\)</configCode>.*|\1|'

hope that helps

1
inukaze On

Hi there for my bash script , under GNU/Linux : I make the follow to save

# Resolution Fix
echo `xrandr --current | grep current | awk '{print $8}'` >> /tmp/width
echo `xrandr --current | grep current | awk '{print $10}'` >> /tmp/height
cat /tmp/height | sed -i 's/,//g' /tmp/height
WIDTH=$(cat /tmp/width)
HEIGHT=$(cat /tmp/height)
rm /tmp/width /tmp/height
echo "$WIDTH"'x'"$HEIGHT" >> /tmp/Resolution
Resolution=$(cat /tmp/Resolution)
rm /tmp/Resolution
# Resolution Fix

and the follow in the same script for restore after exit from some app / game

in some S.O This its execute command directly

ResolutionRestore=$(xrandr -s $Resolution)

But if dont execute call the variable with this to execute the varible content

$($ResolutionRestore)

And the another way you can try its with the follow for example

RESOLUTION=$(xdpyinfo | grep -i dimensions: | sed 's/[^0-9]*pixels.*(.*).*//' | sed 's/[^0-9x]*//')
VRES=$(echo $RESOLUTION | sed 's/.*x//')
HRES=$(echo $RESOLUTION | sed 's/x.*//')