我正在尝试根据鼠标光标的位置获取两个显示器的当前显示分辨率。
即当鼠标光标位于第一个显示器上时,我想获得该显示器的分辨率。
使用 shell 脚本,我可以获得两种分辨率:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2}'")
但我不知道哪个显示器当前处于“活动状态”。
有任何想法吗?
我正在尝试根据鼠标光标的位置获取两个显示器的当前显示分辨率。
即当鼠标光标位于第一个显示器上时,我想获得该显示器的分辨率。
使用 shell 脚本,我可以获得两种分辨率:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2}'")
但我不知道哪个显示器当前处于“活动状态”。
有任何想法吗?
这可以解决问题:
tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
Applescript 无法访问光标位置,即使通过系统事件也是如此。对不起。
[有几个商业解决方案,但我猜在这种情况下它们不值得麻烦?我想我也可以创建一个快速的命令行工具,它只返回当前光标位置......值得麻烦吗?]
ps awk 擅长查找匹配行:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
为了更加完整,这里是获取特定显示器(主显示器或内置显示器)的宽度、高度和 Retina 比例的代码。
这是获取内置显示器的分辨率和 Retina 比例的代码:
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}'")
这是获取主显示器分辨率和 Retina 比例的代码:
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}'")
该代码基于Jessi Baughman 的这篇文章以及此处给出的其他答案。
以下内容不能解决 OP 的问题,但可能对那些想要确定 AppleScript 中所有附加显示器的分辨率的人有所帮助(感谢 @JoelReid 和 @iloveitaly 的构建块):
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}}
为了完整起见,这里是获取屏幕高度的代码:
do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}
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
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)