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

An Applescript to match window heights and positions System
I really liked GaryA's hint on getting Exposé to spread out an app's windows in a line, so I wrote a little AppleScript to arrange them as necessary automatically. It makes the rest of the app's windows match the height and vertical position of the frontmost window. You have to hard-code which application to run this with, though, as I couldn't figure out how to get an AppleScript to simply affect whatever app happens to be in the front.


tell application "Microsoft Word"
  set winrect to bounds of 1st window
  set x to 1st item in winrect
  set y to 2nd item in winrect
  set w to (3rd item in winrect) - x
  set h to (4th item in winrect) - y
  set winlist to every window
   
  repeat with curwin in every item in winlist
    tell curwin
      set currect to bounds of curwin
      set curx to 1st item in currect
      set cury to 2nd item in currect
      set curw to (3rd item in currect) - curx
      set curh to (4th item in currect) - cury
      -- swap the commenting of the next two lines
      -- if you want horizontals to match too
      set bounds of curwin to {curx, y, curx + curw, y + h}
      --set bounds of curwin to {x, y, x + w, y + h}
    end tell
              
  end repeat
end tell
Note: if you have multiple displays, you'll need to swap the comments on the set lines as indicated in the script, or it could possibly position windows completely off of any display ... this will make it match horizontal position & size as well.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[4,455 views]  

An Applescript to match window heights and positions | 9 comments | Create New Account
Click here to return to the 'An Applescript to match window heights and positions' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An Applescript to match window heights and positions
Authored by: sjonke on Dec 10, '03 11:15:24AM
Here is how to make this script work with the frontmost application:

  tell application "System Events"
	  set theApp to get name of first process whose frontmost is true
  end tell

  tell application theApp
  ... [rest of original script...]

Note that for this to work the script would have to be run from a script menu, and not as a script application (since then the script application would be frontmost)

---
--- What?

[ Reply to This | # ]

An Applescript to match window heights and positions
Authored by: jonathanmcd on Dec 10, '03 12:33:10PM
Or, more simply:
tell application whose frontmost is true


[ Reply to This | # ]
An Applescript to match window heights and positions
Authored by: jonathanmcd on Dec 10, '03 12:39:20PM

Oops... scratch that.

sjonke was correct, you must call system events.



[ Reply to This | # ]
An Applescript to match window heights and positions
Authored by: sjonke on Dec 10, '03 12:40:24PM

Perhaps simpler, but not functional. ;) "application whose frontmost is true". Gives an error. You have to query "System Events" for the *process* whose frontmost is true, and use its name. Ah, the quirks of AppleScript.... :)

---
--- What?



[ Reply to This | # ]
An Applescript to match window heights and positions
Authored by: sinjin on Dec 10, '03 04:10:17PM

sjonke, thank you!

I was going nuts a while ago trying to get the syntax correct for that tell block and gave up. Now I can resurrect my script project (which is essentially the same as this hint).

Cheers!



[ Reply to This | # ]
Not working from script menu
Authored by: ubi on Dec 11, '03 03:26:48AM
I used the script editor to save the generalized version of this, which I called matchall.scpt, and another version that maximizes all windows for my 12-inch powerbook (maxall.scpt; see below). If I click Run in the script editor, it works (only for the script editor windows, of course). But when I select it from the script menu, it doesn't function. Help, please!

-- begin maxall.scpt --
tell application "System Events"
	set theApp to get name of first process whose frontmost is true
end tell
tell application theApp
	set winlist to every window
	repeat with curwin in every item in winlist
		tell curwin
			set bounds of curwin to {0, 20, 1023, 724}
		end tell
	end repeat
end tell
-- end maxall.scpt


[ Reply to This | # ]
Not working from script menu
Authored by: ags on Dec 13, '03 12:50:29PM

I couldn't get the general version of this script to work from the Script Menu either. The variable theApp always contained "System Events". I'm no AppleScript guru, so I borrowed a few lines from an example on Apple's web site called "Add Front App to Menu". There's probably a shorter way to set theApp, but this works for me:


try
 set the front_app to the path to frontmost application
 set the theApp to the displayed name of (info for the front_app)
end try

tell application theApp
 set winrect to bounds of 1st window
 set x to 1st item in winrect
 set y to 2nd item in winrect
 set w to (3rd item in winrect) - x
 set h to (4th item in winrect) - y
 set winlist to every window
	
 repeat with curwin in every item in winlist
  tell curwin
   set currect to bounds of curwin
   set curx to 1st item in currect
   set cury to 2nd item in currect
   set curw to (3rd item in currect) - curx
   set curh to (4th item in currect) - cury
   -- swap the commenting of the next two lines
   -- if you want horizontals to match too
   set bounds of curwin to {curx, y, curx + curw, y + h}
   --set bounds of curwin to {x, y, x + w, y + h}
  end tell
 end repeat
end tell


[ Reply to This | # ]
Fixed!
Authored by: ubi on Dec 14, '03 02:26:14PM

Thanks, ags.

Your solution works. Finally, I can control how my windows get zoomed.



[ Reply to This | # ]
An Applescript to match window heights and positions
Authored by: mdouma46 on Jan 14, '04 10:23:26AM

I love exposé, but IMHO, I'd rather have the ability to tile or cascade my windows of the current app before I had to use exposé. I mean, heck, even Windows 3.1 had that option!

While many Carbon applications have this implemented in the actual application itself, no Cocoa apps have this feature.

I've been dreaming of coming up with an applescript that you could run from the script menu that would automatically tile the windows of the frontmost app or cascade the windows of the frontmost app. And you could always use GUI scripting in case the application itself wasn't scriptable. The only difficult part I would imagine is trying to figure out the algorithm to use to arrange the windows, based on the number of windows you have and the screen resolution you're running at (and possibly multiple monitors).

I've got too many projects I'm in the middle of, but I hope to be able to do this some day.....



[ Reply to This | # ]