Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Move and resize app windows via AppleScript System
I'm a neatness freak, and I hate lots of messy overlapping windows everywhere. But I'm also a keyboard junkie, and I hate using the trackpad a lot. And I don't want anymore background processes hogging memory, or more applications to open for such a simple task -- so what to do? Answer: AppleScript. I wrote a collection of AppleScripts that use System Events to control the window size and position of the frontmost application. Also, the window snaps to a grid to make it easy to place next to other windows without overlapping.

You can find the scripts on my iDisk public folder (Go » iDisk » Other User's Public Folder, then enter phildooher). Download the zip file and put the (nine) scripts in your usual script folder. The scripts are known to work on OS X 10.4.11, but are untested on 10.5. [robg adds: I tested a few of these, but not all, on 10.5, and they seem to work as described.]

Assign keyboard shortcuts to these scripts (using Quicksilver or your other favorite tool), and you can move and resize many application windows without using the mouse. Also, there is a 'Finder window 1 remember position' script to stop those pesky Finder windows wandering about after closing. These scripts work in many apps, including at least these: Finder, Safari, Mail, Firefox, TextEdit, Script Editor, Preview, OpenOffice Aqua, Skype, and many others. They don'e seem to work in iTunes, NeoOffice (window 1 resizes, but no scroll bars), and Word 2004. Apps that require specific window sizes, or have floating windows, can be uncooperative. QuickTime Player usually resizes windows proportionally, so video playback might fit to the window instead of being the correct aspect ratio. There is more info on how the scripts work inside the scripts (especially the 'Frontmost application window snap to grid move left' script).

I have a 13" MacBook, so the screen size for the scripts is set to 1280x800 (my Dock is on the right, hidden.) Change the _screenHeight and _screenWidth variables if your size is different (or if you don't want windows behind the Dock). The grid size I have chosen is 64 horizontally and 50 vertically (64 fits into 1280 twenty times, and 50 fits into 800 sixteen times.) Change the variable _gridSize if you prefer a different grid (ie minimum movement size).

The scripts cause the window to snap to a grid before actually being moved. Therefore, some windows will move slightly the opposite way than intended at first. The 'Finder window 1 remember position' script works by turning the window toolbar off and on. You can also stick the window by clicking on the toolbar with the mouse (but hey -- that's cheating). I use keyboard shortcuts that are (for me) easy to remember:
  • Move window left: Shift-Command-Option-Left Arrow
  • Move window right: Shift-Command-Option-Right Arrow
  • Move window up: Shift-Command-Option-Up Arrow
  • Move window down: Shift-Command-Option-Down Arrow
  • Reduce window size horizontally: Shift-Command-Option-Control-Left Arrow
  • Increase window size horizontally: Shift-Command-Option-Control-Right Arrow
  • Reduce window size vertically: Shift-Command-Option-Control-Down Arrow
  • Increase window size vertically: Shift-Command-Option-Control-Up Arrow
  • Remember Finder window 1 position - Shift-Command-Option-Enter
Check your apps to make sure these shortcuts don't conflict with any that are already assigned -- I noticed some funny behaviour in NeoOffice, for instance. The window size is limited to the screen size. The window can be moved off the sides of the screen, but it can't disappear completely.

Quicksilver fans: You can move the window whilst the Quicksilver interface is open -- this is useful for dropping folders into open/save dialogs, etc.

That's it -- any constructive criticism on how the scripts are written and documented, or on how to make this idea better would be greatly appreciated. (Maybe an AppleScript application that opens on start-up and picks up the keystrokes? This would avoid Quicksilver being required for the shortcuts.)

[robg adds: I have mirrored the AppleScripts here on macosxhints.com, just in case the .Mac source ever goes away.]
    •    
  • Currently 2.55 / 5
  You rated: 3 / 5 (11 votes cast)
 
[41,696 views]  

Move and resize app windows via AppleScript | 13 comments | Create New Account
Click here to return to the 'Move and resize app windows via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Move and resize app windows via AppleScript
Authored by: philostein on May 28, '08 08:32:02PM

Sorry, my keyboard shortcuts didn't show - the move window scripts use:
command, option, shift and the cursors.

The resize scripts use:
command, option, shift, control and the cursors.

The 'Remember Finder window position' script uses:
command, option, shift and enter.

PhilHints



[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: taozhyn on May 30, '08 11:55:35PM

I was having problems with running the scripts, until I found some similar scripts (spooky).
In their setup script they test if "access for assistive devices" is enabled in Universal Access.

Once I enabled this your scripts started to work.

Thanks.



[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: philostein on May 31, '08 04:27:31AM

You're right - I turned off 'Enable access for assistive devices' and the scripts stopped working. Anybody know why?

The reasons I wrote these scripts are because: I couldn't find anything similar; I thought someone might find them useful; It's great to get input on how to make scripts better. If you've found something better (i.e. a script that checks for assistive devices) the can you please post a link so I can see for myself. (Also I love ghost stories...)




[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: matters on Jun 01, '08 06:55:57AM
Those scripts are cool and all, but I think you'll find that this app, although requiring the mouse/trackpad, will give you a much more elegant and innovative way of controlling window positions and sizes. I've found now that I can't live without it. :)

Zooom/2
http://coderage-software.com/zooom/

[ Reply to This | # ]
Closest thing to UltraMon or MultiMon
Authored by: gabrielradic on Apr 14, '09 08:05:56AM

These are awesome! I simplified them to only move stuff from a screen to another. I also removed the checks for the screen edges, and let Mac OS take care of that: Aqua won't let windows move out of the screen entirely.

Also, I prefer Butler for my shortcuts and AppleScript, among other stuff good too.



[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: fruityth1ng on Apr 30, '09 06:16:00AM

PhilHints, thanks a bundle for this! -and RobG, seriously, thanks for the mirror - the .mac source was indeed gone :)



[ Reply to This | # ]
Great scripts!
Authored by: lsloan on Mar 22, '11 07:01:39AM

These are great scripts. I was looking for information about using AppleScript to move and resize windows so that I could emulate a "tile windows" function. These are great examples for me to follow for my experiment. Of course, they're great on their own, too.

---
Lance E Sloan



[ Reply to This | # ]
Great scripts!
Authored by: philostein on Mar 23, '11 04:22:51AM

Thanks. I still use them. I'd like to hear about any improvements you come up with (especially if you can think of a way to move hud style windows properly).



[ Reply to This | # ]
Great scripts!
Authored by: lsloan on Mar 23, '11 07:27:20AM

What exactly is "hud style"? Can you give a link to an example screenshot?

---
Lance E Sloan



[ Reply to This | # ]
Great scripts!
Authored by: philostein on Mar 23, '11 02:03:16PM
Stuff like ITunes' Mini Player and Preview's Adjust Color… window. Windows that generally want to float above other windows and apps. They react to the shortcuts, but often go the opposite way when pressing left or right, and always go up when pressing up or down.

[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: bgebhardt on Mar 17, '12 11:14:24AM

Very cool scripts. I added some code to automatically detect the screen size and pick a grid size. Kinda a quick hack without much testing, but thought I’d share it for the adventurous. Only works for one screen.

The first is for increasing screen size (Frontmost application window snap to grid reduce horizontally script). Paste at the top of the script and remove the duplicated set’s to the _gridSize and _screenWidth. The second bit is for decreasing screen size (Frontmost application window snap to grid increase horizontally script). It tries to pick a reasonable minimum window size.

These can be adapted for the other scripts, but that is left as an exercise for the reader.

FIRST BIT OF CODE

-- function returns width and height of the primary display
on displaySize()
	-- this only works with one display for now
	if my isOneDisplay() is true then
		tell application "Finder"
			set screen_resolution to bounds of window of desktop
			set screen_width to item 3 of screen_resolution
			set screen_height to item 4 of screen_resolution
		end tell
		return {screen_width, screen_height}
	else
		display dialog "Script doesn't work with multiple displays right now. [function: displayWidth()]"
		return 0
	end if
end displaySize

-- returns if one display or not.
on isOneDisplay()
	tell application "System Events"
		if (count every desktop) > 1 then
			return false
		else
			return true
		end if
	end tell
end isOneDisplay

-- picks a grid size automatically that gives 16 increments.  For width use item1 of displaySize().  For height use item 2 of displaySize()
set _gridSize to round ((item 1 of my displaySize()) / 16)
set _screenWidth to (item 1 of my displaySize()) -- change this to your screen width (or make it bigger than your screen width if you want bigger windows)

SECOND BIT OF CODE STARTS HERE:

on max(x, y)
	if x ≤ y then
		return y
	else
		return x
	end if
end max

-- function returns width and height of the primary display
on displaySize()
	-- this only works with one display for now
	if my isOneDisplay() is true then
		tell application "Finder"
			set screen_resolution to bounds of window of desktop
			set screen_width to item 3 of screen_resolution
			set screen_height to item 4 of screen_resolution
		end tell
		return {screen_width, screen_height}
	else
		display dialog "Script doesn't work with multiple displays right now. [function: displayWidth()]"
		return 0
	end if
end displaySize

-- returns if one display or not.
on isOneDisplay()
	tell application "System Events"
		if (count every desktop) > 1 then
			return false
		else
			return true
		end if
	end tell
end isOneDisplay

-- picks a grid size automatically that gives 16 increments.  For width use item1 of displaySize().  For height use item 2 of displaySize()
set _gridSize to round ((item 1 of my displaySize()) / 16)

-- set _gridSize to 64
set _safeSize to max(256, _gridSize * 2) -- attempts to automatically set to a reasonable size.  
-- change this if your new _gridSize is not a factor of 256 - not too small though! You can mess up the window!



[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: philostein on Mar 02, '14 02:02:51AM

Looks good!

Are you still using these scripts? (I'm a little late replying…)



[ Reply to This | # ]
Move and resize app windows via AppleScript
Authored by: bgebhardt on Mar 02, '14 10:10:41PM

No I'm not. There are lots of great window utilities out there like Divvy (the one I use).



[ Reply to This | # ]