QS, AppleScript and moving windows

Posted by Kevin Way Mon, 20 Aug 2007 06:53:00 GMT

A 15” laptop has a relatively small display, so I find myself routinely wanting to put a window all the way to one edge or the other, to help maximize the use of this.

I decided that this should be simple, something along the lines of ^⌥⌘← to slam a window to the left edge of my screen and so on.

There were a few hitches, but I did it as follows:

First I made some scripts:

move_left

tell application "Finder" 
    set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop
end tell

tell application "System Events" 
    set frontmostApplication to name of the first process whose frontmost is true

    tell process frontmostApplication
        tell window 1
            set {x1, y1} to position
            set position to {disp_x1, y1}
        end tell
    end tell
end tell

move_right

tell application "Finder" 
    set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop
end tell

tell application "System Events" 
    set frontmostApplication to name of the first process whose frontmost is true

    tell process frontmostApplication
        tell window 1
            set {x1, y1} to position
            set {x_off, y_off} to size

            set position to {disp_x2 - x_off, y1}
        end tell
    end tell
end tell

move up

tell application "Finder" 
    set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop
end tell

tell application "System Events" 
    set frontmostApplication to name of the first process whose frontmost is true

    set disp_y1 to disp_y1 + 22

    tell process frontmostApplication
        tell window 1
            set {x1, y1} to position

            set position to {x1, disp_y1}
        end tell
    end tell
end tell

move down

tell application "Finder" 
    set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop
end tell

tell application "System Events" 
    set frontmostApplication to name of the first process whose frontmost is true

    tell process frontmostApplication
        tell window 1
            set {x1, y1} to position
            set {x_off, y_off} to size

            set position to {x1, disp_y2 - y_off}
        end tell
    end tell
end tell

And at the suggestion of Alex L.

move center

tell application "Finder" 
    set {disp_x1, disp_y1, disp_x2, disp_y2} to bounds of window of desktop
end tell

tell application "System Events" 
    set frontmostApplication to name of the first process whose frontmost is true

    set disp_y1 to disp_y1 + 22

    tell process frontmostApplication
        tell window 1
            set {x1, y1} to position
            set {x_off, y_off} to size

            set position to {disp_x1 + (disp_x2 - x_off) / 2, disp_y1 + (disp_y2 - y_off) / 2}
        end tell
    end tell
end tell

I added these to QS hotkeys, and used the same technique to make windows do incremental moves as well.

Credit to the MacNN Forums, who helped me find a useful answer in this thread.

Follow-up: Zachary Cohen wrote a correction: Seems like a cool little article, but you need to make sure that Access for assistive devices is turned on. You do this by going to “System Preferences” then selecting the “Universal Access” from the options. Then click the click the checkbox at the bottom “Enable access for assistive devices” then these scripts should work.

Comments

Leave a response

Comments