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

发布于 2024-08-13 20:38:57 字数 291 浏览 6 评论 0原文

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

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

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

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

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

有什么想法吗?

I'm trying to get the current display resolution of both of my displays depending on where the mouse cursor is.

i.e. when the mouse cursor is on the first display I want to get the resolution of this display.

With a shell script I can get both resolutions:

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

But I don't get which display is currently "active".

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

软甜啾 2024-08-20 20:38:57

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

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

ps awk 非常适合查找匹配行:

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

Applescript does not have any access to cursor location, even via System Events. Sorry.

[There are a couple commercial solutions, but I'm guessing they're not worth the trouble in this case? I suppose I could also whip up a quick command-line tool that just returns the current cursor location... worth the trouble?]

p.s. awk is great at finding matching lines:

set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
峩卟喜欢 2024-08-20 20:38:57

这可以解决问题:

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

This does the trick:

tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
铁轨上的流浪者 2024-08-20 20:38:57

为了更加完整,这里是获取特定显示器(主显示器或内置显示器)的宽度、高度和 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}'")

这是获取主显示屏的分辨率和视网膜比例的代码:

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 的这篇文章以及此处给出的其他答案。

For the sake of even more completeness, here is the code to get the width, height, and Retina scale of a specific display (main or built-in).

This is the code to get the resolution and Retina scale of the built-in display:

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}'")

And this is the code to get the resolution and Retina scale of the main display:

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}'")

The code is based on this post by Jessi Baughman and the other answers given here.

瀟灑尐姊 2024-08-20 20:38:57

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

The following does not solve the OP's problem, but may be helpful to those wanting to determine the resolution of ALL attached displays in AppleScript (thanks to @JoelReid and @iloveitaly for the building blocks):

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}}
昇り龍 2024-08-20 20:38:57

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

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

For the sake of completeness, here is the code to grab the screen height:

do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'"}
霞映澄塘 2024-08-20 20:38:57

多显示器和视网膜检测

获取所有显示器的宽度高度缩放(视网膜 = 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

基于答案

结果是这样的:

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

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}}
怎会甘心 2024-08-20 20:38:57

在我的机器上,system_profiler 需要近一秒钟才能返回回复。就我的目的而言,那太长了。

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

这对我来说要快得多:

告诉应用程序“Finder”获取桌面窗口的边界

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

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)

凉城 2024-08-20 20:38:57

我有一个使用 cliclickdisplayplacer 的 shell 脚本,两者都可以在 Homebrew 中使用: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

I have a shell script that makes use of cliclick and displayplacer, both available in Homebrew: https://github.com/huyz/trustytools/blob/master/mac/get-bounds-of-mouse-display.sh

To use from within 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文