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

dan*_*dan 8 applescript resolution

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

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

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

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

但我不知道哪个显示器目前处于"活动状态".

有任何想法吗?

Joe*_*eid 9

即使通过系统事件,Applescript也无法访问游标位置.抱歉.

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

ps awk很适合找到匹配的行:

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


Ron*_*ann 9

这样做的诀窍:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于单个显示器. (4认同)
  • 对于多个显示器,“桌面窗口边界”会根据系统偏好设置中定义的空间排列报告“单个组合”尺寸,即“所有显示器周围的包围矩形”。换句话说:您无法判断有多少个显示器,并且报告的矩形可能包含实际上不可显示的区域。类似地,Standard Suite `window` 对象(AppleScriptable 应用程序的窗口,通过 `bounds`)和 Process Suite `window` 对象(上下文 `"System Events"`,通过 `position`)根据这个组合矩形报告它们的坐标。 (2认同)

Syn*_*oli 6

为了更加完整,下面是获取特定显示(主要或内置)的宽度,高度和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}'")
Run Code Online (Sandbox Code Playgroud)

这是获得主显示器的分辨率和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}'")
Run Code Online (Sandbox Code Playgroud)

该代码基于Jessi Baughman的这篇文章以及此处给出的其他答案.


mkl*_*nt0 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}}
Run Code Online (Sandbox Code Playgroud)