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?

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.
    – Nakilon
    Apr 26, 2019 at 14:34

8 Answers 8

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}'")
11

This does the trick:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
2
  • 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.
    – mklement0
    Oct 25, 2013 at 15:42
  • 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?
    – Anticro
    Apr 1, 2021 at 6:43
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.

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 dimensions
    – 2ni
    Nov 13, 2018 at 14:39
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}}
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 sitting Oct 15, 2014 at 19:30
  • 1
    @perrohunter 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.
    – mklement0
    Oct 15, 2014 at 20:31
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}'"}
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}}
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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.