开发者

Positioning a window with AppleScript using dual monitors

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.

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 c开发者_高级运维an I loop through the open windows and get their size? Thanks!


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

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 here called 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.

I hope that helps...


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.

Hope it helps.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜