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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5865989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:55:03  来源:igfitidea点击:

Positioning a window with AppleScript using dual monitors

macosbashapplescriptmultiple-monitors

提问by gdoubleod

I have two monitors set up and I am trying to position the window of an application in the second monitor but nothing I do seems to work. For example I am using my laptop and the terminal window is maximized on the screen. Then I plug in an external monitor. I then want to run the applescript and have the terminal maximize on the larger second monitor.

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

Here is what I have right now:

这是我现在所拥有的:

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

Here is the error I get:

这是我得到的错误:

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

I'm pretty sure I'm just using set position and set size completely wrong :( When I used bounds it kind of works...

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

Bonus Question: How can I loop through the open windows and get their size? Thanks!

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

回答by regulus6633

What have you tried?

你试过什么?

I think to solve this you need to calculate the screen size and coordinates of the second monitor. For example, your main monitor starts at position {0,0}. So the starting position of the second monitor has to be something different and you need to find that. Luckily I have written a tool that will give you both the starting coordinates and screen size of your monitors. Once you have the size and position then it's simple. System events can set the size and position of a window so you could do something like this...

我认为要解决这个问题,您需要计算第二台显示器的屏幕尺寸和坐标。例如,您的主监视器从位置 {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

So from the above script you just need the size and position variables. You can get my tool herecalled hmscreens which will give you those. You may need to do some adjusting of the coordinates depending on if the screen is measured from the lower left corner or upper left, but that's just simple math.

因此,从上面的脚本中,您只需要大小和位置变量。你可以在这里得到我的工具hmscreens,它会给你这些。根据屏幕是从左下角还是左上角测量,您可能需要对坐标进行一些调整,但这只是简单的数学运算。

I hope that helps...

我希望这有帮助...

回答by craigm

Here is a script that handles saving and restoring size and postion for multiple display configurations. It may have some issues with fullscreen apps but it seems to work ok.

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

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

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

回答by craigm

Use bounds instead of position, it works. You can get bounds of the window like this:

使用边界而不是位置,它有效。您可以像这样获得窗口的边界:

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

Answer to the bonus question:

回答奖金问题:

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

If you open Replies tab at bottom part of Applescript editor, you will see all get results.

如果您打开 Applescript 编辑器底部的回复选项卡,您将看到所有得到的结果。

Hope it helps.

希望能帮助到你。