开发者

AppleScript how to get current display resolution?

开发者 https://www.devze.com 2022-12-13 17:18 出处:网络
I\'m trying to get the current display resolution of both of my displays depending on where the mouse cursor is.

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 SPDisp开发者_StackOverflow社区laysDataType | grep Resolution | awk '{print $2}'")

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

Any ideas?


This does the trick:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell


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


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.


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}}


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

do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $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}}


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

精彩评论

暂无评论...
验证码 换一张
取 消