我想要获取当前显示器的分辨率,具体取决于鼠标指针所在的位置。
例如,当鼠标指针位于第一个显示器上时,我想要获取这个显示器的分辨率。
通过一个 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}'")
但我不清楚当前是哪个显示器处于“活动”状态。
有什么想法吗?
Applescript无法通过System Events访问光标位置。抱歉。
[有一些商业解决方案,但我猜在这种情况下它们并不值得麻烦吧?我想我也可以快速创建一个命令行工具,它只返回当前光标位置...是值得麻烦吗?]
p.s. awk很擅长查找匹配的行:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
为了更加完整,以下是获取特定显示器(主屏或内置屏幕)的宽度、高度和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}'")
将宽度设置为dimensions的第1项
将高度设置为dimensions的第2项
- 2niset 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}}
bounds of first window of application (path to frontmost application as text)
,但这些边界是以包含所有显示器的虚拟矩形报告的,就像在“Finder”上下文中由bounds of window of desktop
报告的一样。如果不知道您的显示器是如何排列的(垂直、水平、向左、向右等),则无法推断出具体的显示器。 - mklement0为了完整起见,这里是获取屏幕高度的代码:
do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}
获取所有显示器的宽度、高度和缩放比例(retina = 2,否则为1):
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
结果类似于这样:
{{2304, 1440, 2}, {1920, 1080, 1}}
system_profiler
几乎需要一秒钟才能返回结果。对于我的需求来说,这太长了。ASObjC Runner
,但显然它现在已经不起作用了。tell application "Finder" to get bounds of window of desktop
(摘自https://superuser.com/a/735330/64606)我有一个 shell 脚本,利用了 Homebrew 中可用的 cliclick
和 displayplacer
:https://github.com/huyz/trustytools/blob/master/mac/get-bounds-of-mouse-display.sh
从 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