Get Macbook screen size from terminal/bash
Asked Answered
E

4

6

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?

Elizbeth answered 26/7, 2014 at 15:1 Comment(0)
E
0

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.

Elizbeth answered 28/7, 2014 at 10:39 Comment(0)
S
1

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

Sixtyfourmo answered 26/7, 2014 at 15:45 Comment(3)
Thanks. I did come across a similar line in my searches elsewhere, but unfortunately on my Air, that returns no results at all, even when getting rid of the last grep section.Elizbeth
Nothing outputs for me either.Muss
Thanks Daniel. Interesting concept to glean the info from the Apple web site, though unfortunately for me it just returns: MacBook AirElizbeth
L
0

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.

Lyophobic answered 27/7, 2014 at 7:59 Comment(1)
Thanks jm666. This looks similar to the solution by Daniel (below), though unfortunately for me it just returns: MacBook AirElizbeth
E
0

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.

Elizbeth answered 28/7, 2014 at 10:39 Comment(0)
A
0

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.*//')
Azriel answered 19/10, 2014 at 20:1 Comment(1)
Macs don't run X by default. This solution will fire up the XQuartz X11 windowing system to get the answer, which (a) I don't think is installed by default on OS X these days, (b) will be horrendously inefficient, and (c) doesn't seem to report the entire screen, e.g. on my Mac it suggests that the resolution is 1920x1057, whereas it's actually 1920x1080. I'm guessing it might be excluding the menubar.Saucepan

© 2022 - 2024 — McMap. All rights reserved.