使用Applescript检索窗口的屏幕分辨率



在多个屏幕的情况下,有没有办法知道应用程序的文档显示在哪个屏幕上?

例如,在Photoshop中,我能够通过以下方式检索文档窗口的位置:

tell application "System Events" to tell application process "Adobe Photoshop CS6"
---------- document's window list
set docsWinList to {}
copy (the windows whose name ≠ "") to docsWinList
set docsWinCount to (count of docsWinList)
if (docsWinCount < 1) then return -- no opened documents
---------- position of the first document's window
tell item 1 of docsWinList
set hvCoords to position -- list with 2 items : h and v coords
end tell
end tell

但是这些坐标对所有屏幕都是绝对的(并且可能是负数(。

另一方面,我能够通过此处找到的脚本了解所有屏幕的分辨率:

on getScreensResolution()
set screensSizes to {}
repeat with p in paragraphs of (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution:/{ printf "%s %s\n", $2, $4 }'")
set screensSizes to screensSizes & {{word 1 of p as number, word 2 of p as number}}
end repeat
# `screensSizes` now contains a list of size lists;
# e.g., with 2 displays, something like {{2560, 1440}, {1920, 1200}}
return screensSizes
end getScreensResolution

由于之前找到的窗口坐标可能是负数,具体取决于窗口是否显示在第二个屏幕上,并且此屏幕实际位于主屏幕的左侧(由操作系统定义(,我想知道此文档窗口屏幕的分辨率。

我不确定这是否可能,或者它可能涉及大量计算。

(除了你的想法,我想说的是,即使你只使用一个屏幕,窗口也可能有负的位置值:只需将任何窗口移动到屏幕的左边框之外......

如果你只有两个屏幕,你的问题很容易回答。
从您的第一个脚本开始,只需添加此行...

if (item 1 of (size of window 1 as list)) * -1 > (item 1 of hvCoords)) then display dialog "Your window is located on a screen left of your active one."

– "如果...然后"决定"窗口 1"的负 x 值是否大于其实际宽度
——"显示对话框"是您计划添加的任何代码的占位符/反馈......

例如:如果大小为 750x400 像素的窗口返回 x 值 -1,122,则上面的行将计算
750* -1 > -1,122 => -750 > -1,122 => true => window located on left-screen.

此代码假设只有当窗口完全传输到左侧屏幕时,窗口才会显示在左侧屏幕上。
如果窗口在移动"其大小的一半"后已经转移,请相应地调整上面的代码:

(item 1 of (size of window 1 as list)) *-1/2
                                                                                    =====(
仅当您使用 2 个以上的屏幕时,您才需要第二个脚本的值。

最新更新