Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript how to get current display resolution?

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?

like image 439
dan Avatar asked Dec 08 '09 13:12

dan


5 Answers

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.

like image 149
Synoli Avatar answered Oct 21 '22 00:10

Synoli


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}'")
like image 26
Joel Reid Avatar answered Oct 20 '22 23:10

Joel Reid


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}}
like image 30
luckydonald Avatar answered Sep 30 '22 07:09

luckydonald


This does the trick:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
like image 11
Ronald Hofmann Avatar answered Oct 21 '22 00:10

Ronald Hofmann


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}}
like image 6
mklement0 Avatar answered Oct 20 '22 23:10

mklement0