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)
但我不知道哪个显示器目前处于"活动状态".
有任何想法吗?
即使通过系统事件,Applescript也无法访问游标位置.抱歉.
[有一些商业解决方案,但我猜他们在这种情况下不值得麻烦?我想我也可以启动一个快速命令行工具,只返回当前光标位置...值得麻烦?]
ps awk很适合找到匹配的行:
set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
Run Code Online (Sandbox Code Playgroud)
这样做的诀窍:
tell application "Finder"
set screen_resolution to bounds of window of desktop
end tell
Run Code Online (Sandbox Code Playgroud)
为了更加完整,下面是获取特定显示(主要或内置)的宽度,高度和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的这篇文章以及此处给出的其他答案.
以下内容不能解决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)