AppleScript如何获取当前显示器的分辨率?

13

我想要获取当前显示器的分辨率,具体取决于鼠标指针所在的位置。

例如,当鼠标指针位于第一个显示器上时,我想要获取这个显示器的分辨率。

通过一个 shell 脚本,我可以获取两个显示器的分辨率:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2}'")

但我不清楚当前是哪个显示器处于“活动”状态。

有什么想法吗?


所有关于AppleScript的答案都是错误的,因为它们将多个显示器加在一起。所有关于system_profiler的答案也是错误的,因为有效的Retina分辨率可能与其所述不同。 - Nakilon
8个回答

11

Applescript无法通过System Events访问光标位置。抱歉。

[有一些商业解决方案,但我猜在这种情况下它们并不值得麻烦吧?我想我也可以快速创建一个命令行工具,它只返回当前光标位置...是值得麻烦吗?]

p.s. awk很擅长查找匹配的行:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")

11
这就是解决问题的方法:
tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell

2
在多个显示器的情况下,“桌面窗口的边界”报告一个单一的、组合的大小,它是基于它们在系统偏好设置中定义的空间排列的所有显示器所包围的矩形。 换句话说:你无法知道有多少个显示器,并且报告的矩形可能包含实际上不能显示的区域。同样地,标准套件“窗口”对象(通过“bounds”访问可AppleScript应用程序的窗口)和进程套件“窗口”对象(通过“position”访问上下文为“系统事件”的窗口)以这个组合矩形为基础报告它们的坐标。 - mklement0
这基本上很好用,也得到了一个赞。我的脚本创建了2个Finder窗口并将它们并排显示,就像Norton Commander一样。但是如果我连接一个外部显示器到我的Macbook上,它会使用外部显示器的分辨率。外部显示器被配置为次要屏幕(不是镜像)。有没有什么方法可以始终引用内部屏幕? - Anticro

9

为了更加完整,以下是获取特定显示器(主屏或内置屏幕)的宽度、高度和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的这篇文章以及这里提供的其他答案。

我必须将结果设置在一个变量中,然后在“tell application”块之外使用它: tell application "Finder" set dimensions to words of (do shell... end tell将宽度设置为dimensions的第1项 将高度设置为dimensions的第2项 - 2ni

7
以下内容并不能解决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}}

这很棒,你能想到一种方法或知道哪个是活动桌面吗?我在考虑寻找一种方法来知道哪个桌面是活动窗口所在的。 - perrohunter
1
@perrohunter 我不知道如何仅使用AppleScript通用地完成此操作。虽然您可以按以下方式获取最前面应用程序的活动窗口的边界:bounds of first window of application (path to frontmost application as text),但这些边界是以包含所有显示器的虚拟矩形报告的,就像在“Finder”上下文中由bounds of window of desktop报告的一样。如果不知道您的显示器是如何排列的(垂直、水平、向左、向右等),则无法推断出具体的显示器。 - mklement0

4

为了完整起见,这里是获取屏幕高度的代码:

do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}

4

多显示器和Retina检测

获取所有显示器宽度高度缩放比例(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}}

2
在我的电脑上,system_profiler 几乎需要一秒钟才能返回结果。对于我的需求来说,这太长了。
在10.12版本之前,我使用了 ASObjC Runner,但显然它现在已经不起作用了。
对于我来说,这个更快: tell application "Finder" to get bounds of window of desktop (摘自https://superuser.com/a/735330/64606

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接