10

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

即当鼠标光标位于第一个显示器上时,我想获得该显示器的分辨率。

使用 shell 脚本,我可以获得两种分辨率:

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

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

有任何想法吗?

4

7 回答 7

11

这可以解决问题:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
于 2011-10-30T10:49:56.957 回答
10

Applescript 无法访问光标位置,即使通过系统事件也是如此。对不起。

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

ps awk 擅长查找匹配行:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
于 2010-02-23T21:08:13.280 回答
8

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

于 2014-05-04T02:54:03.763 回答
6

以下内容不能解决 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}}
于 2013-10-25T15:51:20.220 回答
4

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

do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}
于 2011-06-29T17:14:02.570 回答
3

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}}
于 2018-02-09T11:52:16.247 回答
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)

于 2017-11-16T15:47:39.373 回答