Search code examples
macosfinder

Open windows to preset size and position


I was wondering if there is a way to create a script or automator task that will open windows with a preset size and position. I'd like to do this with apps, as well as with finder windows.

For example, I want to open Sublime and have it take up the top half of my screen, and open Chrome taking up the bottom half of my screen.
Another example is I'd like to open up four finder windows, each taking up a quarter of the screen.


Solution

  • I would recommend using Script Editor to write some AppleScripts that will cater to your different layout requirements for different applications.

    I'll demonstrate how this can be done using your first example featuring Sublime and Google Chrome, which are to occupy the top half and the bottom half of the screen, respectively.

    This script assumes the applications are already running with a visible window available in each application to manipulate. It's actually a very short script, but looks long because of the comment lines (they start with --) that I inserted at each stage to guide you through what each part of the script does:

        tell application "System Events"
            set Chrome to the front window of application process "Google Chrome"
            set Sublime to the front window of application process "Sublime Text"
    
            -- Get the size of the screen's display in pixels
            get the size of scroll area 1 of application process "Finder"
            set Display to {width:item 1, height:item 2} of the result
    
            -- First attempt to make Sublime take up the full width and
            -- height of the display. 
            set position of Sublime to [0, 0]
            set size of Sublime to [the Display's width, the Display's height]
    
            -- However, the presence of a visible menu bar and dock
            -- will create natural barriers that the window won't cross.
    
            -- Retrieve the actual size and position that Sublime's
            -- window ended up adopting.
            set [_w, _h] to the size of Sublime
            set [_x, _y] to the position of Sublime
    
            -- Half Sublime's window's height.  It should now be
            -- correctly positioned and sized.
            set the size of Sublime to [_w, _h / 2 - 1]
    
            -- Position Chrome first so that its horizontally aligned
            -- with Sublime, and the top edge of Chrome's window's meets 
            -- the bottom edge of Sublime's.
            set position of Chrome to [_x, _y + _h / 2]
            -- Then size Chrome's window identically to that of Sublime.
            set size of Chrome to [_w, _h / 2 - 1]
        end tell
    
    
        -- Lastly, bring both applications to the foreground
        activate application "Sublime Text"
        activate application "Google Chrome"
    

    Bear in mind this is just a quick example of how you might approach this task given an optimal starting point (both apps open with one active window each). However, you'll likely want to incorporate ways of dealing with less optimal situations, such as how you'd want your script to behave if you accidentally triggered it before opening Chrome; or if you wished to arrange four Finder windows into separate corners of the screen, but there were five open at the time.


    Alternatively, you could spend about $1 and get an app from the App Store that will size and position windows in just about any arrangement you could wish for. Here's one: