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

时间:2009-12-08 13:16:04

标签: applescript resolution

我正在尝试根据鼠标光标的位置获取两个显示器的当前显示分辨​​率。

即。当鼠标光标在第一个显示器上时,我想得到这个显示器的分辨率。

使用shell脚本我可以得到两种解决方案:

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

但我不知道哪个显示器目前处于“活动状态”。

有什么想法吗?

7 个答案:

答案 0 :(得分:8)

即使通过系统事件,Applescript也无法访问游标位置。遗憾。

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

P.S。 awk很擅长找到匹配的行:

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

答案 1 :(得分:8)

这就是诀窍:

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

答案 2 :(得分:6)

为了更加完整,下面是获取特定显示(主要或内置)显示的宽度,高度和视网膜比例的代码。

这是获取内置显示器的分辨率和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的this post以及此处给出的其他答案。

答案 3 :(得分:5)

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

答案 4 :(得分:4)

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

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

答案 5 :(得分:1)

所有显示器获取宽度,高度和缩放(retina = 2,else = 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

基于answers above

结果如下:

{{2304, 1440, 2}, {1920, 1080, 1}}

答案 6 :(得分:-1)

在我的计算机上system_profiler需要将近一秒的时间才能回复。就我的目的而言,这太长了。

10.12之前,我使用了ASObjC Runner,但显然不再有效。

这对我来说要快得多:

tell application "Finder" to get bounds of window of desktop

(取自https://superuser.com/a/735330/64606