AppleScript how to get current display resolution?
Asked Answered
K

8

13

I'm trying to get the current display resolution of both of my displays depending on where the mouse cursor is.

i.e. when the mouse cursor is on the first display I want to get the resolution of this display.

With a shell script I can get both resolutions:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2}'")

But I don't get which display is currently "active".

Any ideas?

Kearns answered 8/12, 2009 at 13:16 Comment(1)
All answers about applescript are wrong because they add multiple displays together. All answers about system_profiler are wrong because effective Retina resolution can be different from what it says.Stieglitz
F
11

Applescript does not have any access to cursor location, even via System Events. Sorry.

[There are a couple commercial solutions, but I'm guessing they're not worth the trouble in this case? I suppose I could also whip up a quick command-line tool that just returns the current cursor location... worth the trouble?]

p.s. awk is great at finding matching lines:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
Foregut answered 23/2, 2010 at 21:8 Comment(0)
S
11

This does the trick:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
Septempartite answered 30/10, 2011 at 10:49 Comment(2)
With multiple displays, bounds of window of desktop reports a single, combined size that is the encompassing rectangle around all displays, based on their spatial arrangement as defined in System Preferences. In other words: you can't tell how many displays there are and the rectangle reported may contain areas that aren't actually displayable. Similarly, both Standard Suite window objects (windows of AppleScriptable applications, via bounds) and Process Suite window objects (context "System Events", via position) report their coordinates in terms of this combined rectangle.Isotope
This basically works nice and got an upvote. My script creates 2 Finder windows and shows them side-by-side, like Norton Commander. But if I connect an external display to my Macbook, it will use the resolution of the external display. The external display is configured to be the secondary screen (and it doesn't mirror). Is there a clue to always reference the internal screen?Pretended
R
9

For the sake of even more completeness, here is the code to get the width, height, and Retina scale of a specific display (main or built-in).

This is the code to get the resolution and Retina scale of the built-in display:

set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Built-In: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'")

And this is the code to get the resolution and Retina scale of the main display:

set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'")

The code is based on this post by Jessi Baughman and the other answers given here.

Roundworm answered 4/5, 2014 at 2:54 Comment(1)
I had to set the results in one variable and then use it outside of the "tell application" block: tell application "Finder" set dimensions to words of (do shell... end tell set width to item 1 of dimensions set height to item 2 of dimensionsElectrotype
I
7

The following does not solve the OP's problem, but may be helpful to those wanting to determine the resolution of ALL attached displays in AppleScript (thanks to @JoelReid and @iloveitaly for the building blocks):

set resolutions to {}
repeat with p in paragraphs of ¬
  (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf \"%s %s\\n\", $2, $4 }'")
  set resolutions to resolutions & {{word 1 of p as number, word 2 of p as number}}
end repeat
# `resolutions` now contains a list of size lists;
# e.g., with 2 displays, something like {{2560, 1440}, {1920, 1200}}
Isotope answered 25/10, 2013 at 15:51 Comment(2)
This is great, can you think of a way or know which is the active desktop? I was thinking of looking for a way of know in which desktop is the active window sittingStudner
@Studner I wouldn't know how to do this generically in AppleScript alone. While you can get the bounds of the frontmost application's active window as follows: bounds of first window of application (path to frontmost application as text), these bounds are reported in terms of a virtual rectangle encompassing all displays, as reported by bounds of window of desktop in the Finder context. Without also knowing how your displays are arranged (vertically, horizontally, to the left, to the right, ...), you won't be able to infer the specific display.Isotope
L
4

For the sake of completeness, here is the code to grab the screen height:

do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}
Labiate answered 29/6, 2011 at 17:14 Comment(0)
T
4

Multi-Monitor and Retina detection

To get the width, height and scaling (retina = 2, else = 1) for all monitors:

set resolutions to {}
repeat with p in paragraphs of ¬
    (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf \"%s %s %s\\n\", $2, $4, ($5 == \"Retina\" ? 2 : 1) }'")
    set resolutions to resolutions & {{word 1 of p as number, word 2 of p as number, word 3 of p as number}}
end repeat

get resolutions

Based on answers above.

Results in something like this:

{{2304, 1440, 2}, {1920, 1080, 1}}
Testerman answered 9/2, 2018 at 11:52 Comment(0)
M
2

On my machine system_profiler takes nearly a second to return a reply. For my purposes, that way too long.

Pre-10.12, I used ASObjC Runner but apparently that no longer works.

This is much faster for me:

tell application "Finder" to get bounds of window of desktop

(Taken from https://superuser.com/a/735330/64606)

Metabolize answered 16/11, 2017 at 15:47 Comment(0)
J
0

I have a shell script that makes use of cliclick and displayplacer, both available in Homebrew: https://github.com/huyz/trustytools/blob/master/mac/get-bounds-of-mouse-display.sh

To use from within AppleScript:

set displayBounds to do shell script "PATH=/opt/homebrew/bin:$PATH /Users/huyz/bin/get-bounds-of-mouse-display | xargs -n 1 echo"
set displayBounds to the paragraphs of displayBounds
Jurywoman answered 15/2, 2023 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.