AppleScript 如何获取当前的显示分辨率?
我试图根据鼠标光标的位置获取两个显示器的当前显示分辨率。
即当鼠标光标位于第一个显示器上时,我想获取该显示器的分辨率。
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Applescript 无法访问光标位置,即使通过系统事件也是如此。对不起。
[有一些商业解决方案,但我猜在这种情况下它们不值得麻烦?我想我还可以创建一个快速命令行工具,只返回当前光标位置...值得麻烦吗?]
ps awk 非常适合查找匹配行:
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:
这可以解决问题:
This does the trick:
为了更加完整,这里是获取特定显示器(主显示器或内置显示器)的宽度、高度和 Retina 比例的代码。
这是获取内置显示屏的分辨率和视网膜比例的代码:
这是获取主显示屏的分辨率和视网膜比例的代码:
代码基于 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:
And this is the code to get the resolution and Retina scale of the main display:
The code is based on this post by Jessi Baughman and the other answers given here.
以下内容并不能解决OP的问题,但可能对那些想要确定AppleScript中所有附加显示器的分辨率的人有所帮助(感谢@JoelReid和@iloveitaly的构建块):
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):
为了完整起见,下面是获取屏幕高度的代码:
For the sake of completeness, here is the code to grab the screen height:
多显示器和视网膜检测
获取所有显示器的宽度、高度和缩放(视网膜 = 2,else = 1) :
基于答案
结果是这样的:
Multi-Monitor and Retina detection
To get the width, height and scaling (retina = 2, else = 1) for all monitors:
Based on answers above.
Results in something like this:
在我的机器上,
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)
我有一个使用
cliclick
和displayplacer
的 shell 脚本,两者都可以在 Homebrew 中使用:https://github.com/huyz/trustytools/blob/master/mac/get-bounds-of-mouse- display.sh在 AppleScript 中使用:
I have a shell script that makes use of
cliclick
anddisplayplacer
, both available in Homebrew: https://github.com/huyz/trustytools/blob/master/mac/get-bounds-of-mouse-display.shTo use from within AppleScript: