使用AppleScript在双显示器上定位窗口

8
我有两个显示器设置好,现在我正在尝试将应用程序窗口定位到第二个显示器,但是无论我做什么都不起作用。例如,我正在使用笔记本电脑,终端窗口已经最大化了。然后我插入外部显示器。然后我想运行苹果脚本,并使终端窗口最大化到更大的第二个显示器上。
这是我现在拥有的:
set monitorTwoPos to {1050, -600}
set monitorTwoSze to {1200, 1920}

tell application "Microsoft Outlook"
    set position of window 1 to monitorTwoPos
    set size of window 1 to monitorTwoSze
end tell

这是我收到的错误信息:

/Users/vcutten/AppleScripts/SpacesWork.scpt:1291:1332: execution error: 
Microsoft Outlook遇到了一个错误:无法将窗口1的位置转换为类型说明符。 (-1700)

我相信我完全错误地使用了set position和set size :( 当我使用bounds时,它有点起作用...

额外问题: 如何循环遍历打开的窗口并获取它们的大小?谢谢!

3个回答

5

你尝试过什么?

我认为要解决这个问题,你需要计算第二个显示器的屏幕大小和坐标。例如,你的主显示器从位置{0,0}开始。所以第二个显示器的起始位置必须有所不同,你需要找到它。幸运的是,我写了一个工具,可以给你两个显示器的起始坐标和屏幕大小。一旦你有了大小和位置,那就很简单了。系统事件可以设置窗口的大小和位置,所以你可以像这样做...

set monitorSize to {800, 600}
set monitorPosition to {-800, 0}

tell application "System Events"
    tell process "Terminal"
        set frontWindow to first window
        set position of frontWindow to monitorPosition
        set size of frontWindow to monitorSize
    end tell
end tell

从上面的脚本中,你只需要大小和位置变量。你可以在这里获取我的工具hmscreens,它将为您提供这些变量。根据屏幕是从左下角还是左上角测量,您可能需要调整坐标,但这只是简单的数学计算。

希望这能帮到你...


谢谢你的回答!我已经根据你的要求更新了问题……我感觉自己只是没有正确地使用set position :( - gdoubleod

3

使用边界而不是位置,它能够起作用。您可以像这样获取窗口的边界:

tell application "Microsoft Outlook"
    get bounds of first window
end tell

奖励问题的答案:

tell application "Microsoft Outlook"
    repeat with nextWindow in (get every window)
        get bounds of nextWindow
    end repeat
end tell

如果你在Applescript编辑器的底部打开“回复”选项卡,你将看到所有的获取结果。
希望这有所帮助。

2
这是一个处理保存和还原多屏幕配置大小和位置的脚本。在全屏应用程序方面可能存在一些问题,但它似乎运行良好。
-- allSettings is a list of records containing {width:? height:? apps:{{name:? pos:? size:?},...} 
-- for each display setup store the apps and their associated position and size
property allSettings : {}

-- create a variable for the current settings
set currentSettings to {}

display dialog "Restore or save window settings?" buttons {"Restore", "Save"} default button "Restore"
set dialogResult to result

tell application "Finder"

    -- use the desktop bounds to determine display config
    set desktopBounds to bounds of window of desktop
    set desktopWidth to item 3 of desktopBounds
    set desktopHeight to item 4 of desktopBounds
    set desktopResolution to desktopWidth & "x" & desktopHeight

    -- find the saved settings for the display config
    repeat with i from 1 to (count of allSettings)
        if (w of item i of allSettings is desktopWidth) and (h of item i of allSettings is desktopHeight) then
            set currentSettings to item i of allSettings
        end if
    end repeat

    if (count of currentSettings) is 0 then
        -- add the current display settings to the stored settings
        set currentSettings to {w:desktopWidth, h:desktopHeight, apps:{}}
        set end of allSettings to currentSettings
        --say "creating new config for " & desktopResolution
    else
        --say "found config for " & desktopResolution
    end if

end tell

tell application "System Events"

    if (button returned of dialogResult is "Save") then
        say "saving"
        repeat with p in every process
            if background only of p is false then
                tell application "System Events" to tell application process (name of p as string)

                    set appName to name of p

                    if (count of windows) > 0 then
                        set appSize to size of window 1
                        set appPosition to position of window 1
                    else
                        set appSize to 0
                        set appPosition to 0
                    end if

                    set appSettings to {}

                    repeat with i from 1 to (count of apps of currentSettings)
                        if name of item i of apps of currentSettings is name of p then
                            set appSettings to item i of apps of currentSettings
                        end if
                    end repeat

                    if (count of appSettings) is 0 then
                        set appSettings to {name:appName, position:appPosition, size:appSize}
                        set end of apps of currentSettings to appSettings
                    else
                        set position of appSettings to appPosition
                        set size of appSettings to appSize
                    end if

                end tell
            end if
        end repeat
    end if

    if (button returned of dialogResult is "Restore") then
        if (count of apps of currentSettings) is 0 then
            say "no window settings were found"
        else
            say "restoring"
        repeat with i from 1 to (count of apps of currentSettings)
            set appSettings to item i of apps of currentSettings
            set appName to (name of appSettings as string)
            try
                tell application "System Events" to tell application process appName
                    if (count of windows) > 0 then
                        set position of window 1 to position of appSettings
                        set size of window 1 to size of appSettings
                    end if
                end tell
            end try
        end repeat
        end if
    end if
end tell

https://gist.github.com/cmackay/5863257


未找到任何窗口设置。脚本行为和属性是否发生了变化? - roberthuttinger
我已经很多年没有使用那个脚本了。我不确定它是否仍然有效。我目前使用一个叫做Moom的应用程序来管理我的窗口位置。https://manytricks.com/moom/ - craigm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接