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?
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.
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}'")
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}}
This does the trick:
tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With