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

时间:2011-05-03 06:41:38

标签: macos bash applescript multiple-monitors

我设置了两台显示器,我正在尝试将应用程序的窗口放在第二台显示器中,但我所做的一切似乎都无法正常工作。例如,我正在使用我的笔记本电脑,终端窗口在屏幕上最大化。然后我插上一个外接显示器。然后我想运行applescript并让终端在更大的第二台显示器上最大化。

这就是我现在所拥有的:

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 got an error: Can’t make position of window 1 into type specifier. (-1700)

我很确定我只是使用设置位置和设置尺寸完全错误:(当我使用边界时它有点工作......

奖金问题: 如何在打开的窗户中循环并获得它们的大小?谢谢!

3 个答案:

答案 0 :(得分:2)

你有什么尝试?

我认为要解决此问题,您需要计算第二台显示器的屏幕尺寸和坐标。例如,您的主监视器从位置{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的工具here,它将为您提供这些工具。您可能需要根据屏幕是从左下角还是左上角进行测量来调整坐标,但这只是简单的数学运算。

我希望有帮助...

答案 1 :(得分:1)

这是一个处理多个显示配置的保存和恢复大小和位置的脚本。它可能与全屏应用有一些问题,但它似乎工作正常。

-- 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

答案 2 :(得分:0)

使用边界而不是位置,它可以工作。你可以像这样得到窗口的界限:

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编辑器底部的“回复”标签,您将看到所有获得结果。

希望它有所帮助。