How can I get the current screen resolution from the command line on OS X?

40,113

Solution 1

system_profiler SPDisplaysDataType | grep Resolution

Solution 2

For a quick reading on the current virtual resolution of a single retina display:

$ osascript -e 'tell application "Finder" to get bounds of window of desktop'
0, 0, 2048, 1280

Results for multi-monitor setups vary based on which display is primary and how they are arranged. Read more here

Solution 3

I use the utility screenresolution to get the screen resolution:

$ /usr/local/bin/screenresolution get 2>&1 | grep -oE 'Display 0: [0-9]+' | grep -Eo '[0-9]+$'  
1920

Solution 4

This is a little one-liner I came up to return the the resolution of the main display, that doesn't require any dependencies to be installed on macOS:

system_profiler -json SPDisplaysDataType 2>/dev/null | python -c "import sys,json;d=next(i for i in json.load(sys.stdin)['SPDisplaysDataType'][0]['spdisplays_ndrvs'] if 'spdisplays_main' in i);print d['_spdisplays_pixels']"

If you need to filter based on different criteria, just replace the 'spdisplays_main' in i expression with a more suitable one.

I know someone on the internet will take offence at the fact that this does in fact depend on Python... Python has come bundled with macOS since Jaguar, so even if you're running a Power Macintosh G3 that hasn't been updated in the last 15 years, you won't need to download Python.

Solution 5

I wrote displayplacer, which can help with this. Execute displayplacer list and it will show the current resolution (and more info) for all screens.

$ displayplacer list
Persistent screen id: A46D2F5E-487B-CC69-C588-ECFD519016E5
Contextual screen id: 1124216237
Type: 40 inch external screen
Resolution: 3840x2160
Hertz: 60
Color Depth: 4
Scaling:off
Origin: (0,0) - main display
Rotation: 0
Resolutions for rotation 0:
  mode 0: res:3840x2160 hz:60 color_depth:4 <-- current mode
  mode 1: res:3840x2160 hz:60 color_depth:8
  mode 2: res:3840x2160 hz:30 color_depth:4
...
Persistent screen id: 2960D639-F605-5BB4-A53D-A3263008894C
Contextual screen id: 69733451
Type: MacBook built in screen
Resolution: 1680x1050
Hertz: N/A
Color Depth: 4
Scaling:on
Origin: (-1680,1291)
Rotation: 0 - rotate internal screen example (may crash computer, but will be rotated after rebooting): `displayplacer "id:2960D639-F605-5BB4-A53D-A3263008894C degree:90"`
Resolutions for rotation 0:
  mode 0: res:1440x900 color_depth:4 scaling:on
  mode 1: res:1440x900 color_depth:8 scaling:on
  mode 2: res:720x450 color_depth:4 scaling:on

grep is a simple approach to parse the output.

$ displayplacer list | grep -e Resolution: -e Scaling:
Resolution: 3840x2160
Scaling:off
Resolution: 1680x1050
Scaling:on

Also available via Homebrew brew tap jakehilborn/jakehilborn && brew install displayplacer

Share:
40,113

Related videos on Youtube

cwd
Author by

cwd

Updated on September 18, 2022

Comments

  • cwd
    cwd over 1 year

    How can I get the current display resolution from the command line in OS X?

  • studgeek
    studgeek almost 11 years
    On a Macbook pro (10.8.3) this only returns the LCD's max resolution, not the current/selected display resolution.
  • Jim Stewart
    Jim Stewart almost 11 years
    On my Air running 10.8.4 with an external display, this shows both the Air's resolution and the external display's resolution, on separate lines.
  • Gerry
    Gerry about 10 years
    @studgeek I'm not sure if this is still an issue, but on my Air it displays the current resolution. It's very strange the the Pro would alter this behaviour.
  • daviewales
    daviewales almost 10 years
    It displays the current resolution on a MacBook Pro 2011 model, running Mavericks.
  • aymericbeaumet
    aymericbeaumet over 9 years
    It shows the current resolution on a MBP with OSX 10.9.4
  • Fuzzy
    Fuzzy over 7 years
    This works, the answer provided by Gerry is incorrect.
  • David Moles
    David Moles almost 7 years
    For a Retina screen, this shows the physical resolution, not the scaled effective resolution.
  • Dan
    Dan over 6 years
    Great! This is so much faster than system_profiler!
  • Nakilon
    Nakilon about 5 years
    They give different numbers in case of Retina though.
  • Scott - Слава Україні
    Scott - Слава Україні almost 5 years
    (1) I assume that you’re the author of this program (the name being the same).  You should say so, clearly and explicitly, as soon as you mention the program.  (2) This would be a better answer if you actually showed how to parse the output of your program to get what the question asks for, rather than just touting your program. … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.
  • Chris
    Chris almost 4 years
    This doesn't work for me, I get KeyError: 'spdisplays_ndrvs'. This is because the first item in SPDisplaysDataType is for the integrated graphics card (kHW_IntelUHDGraphics630Item), but I'm using the discrete graphics which is the 2nd item in the list. Also if you're looking for no-dependency solutions, this is a bit more concise and doesn't require python. Though it might be more obtuse. system_profiler -json SPDisplaysDataType | grep _spdisplays_pixels | cut -d '"' -f 4
  • Chris
    Chris almost 4 years
    This is a better solution, since it returns the "logical" resolution when you're using retina graphics. If you're using this in a script and need to put the height and width into variables, this might help: SCREEN_WIDTH=$(osascript -e 'tell application "Finder" to get bounds of window of desktop' | tr -d ',' | awk '{print $3}'); SCREEN_HEIGHT=$(osascript -e 'tell application "Finder" to get bounds of window of desktop' | tr -d ',' | awk '{print $4}')
  • Chris
    Chris almost 4 years
    displayplacer is great! If you're working on something personal where new dependencies are fine, I really recommend others check this out. I have setup keyboard shortcuts to call out to displayplacer so I can easily change my resolution depending on the task. It also lets you set display resolutions that aren't possible to choose in the system preferences even if you know about the obscure trick of holding ⌥ while clicking "Scaled". Not sure why this got downvotes. It's an amazing util.
  • Eva Lacy
    Eva Lacy over 2 years
    The problem with using grep is that it returns multiple resolutions, use this instead to get the main display system_profiler -json SPDisplaysDataType 2>/dev/null | jq -r '.. | objects | select(.spdisplays_main) | ._spdisplays_pixels'