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


Click here to return to the 'Tile Safari windows via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Tile Safari windows via AppleScript
Authored by: deviantintegral on Apr 11, '09 09:12:02AM
Here is a version which easily allows tiling only on the primary monitor (assuming it's the largest monitor). This works for me as I have a 24" LCD connected to my Macbook, and I really don't want the script to treat them as one wide monitor. Set the first variable to the width of your primary monitor. When on the road without it connected, the script should still work as expected.
set primaryWidth to 1920

tell application "Safari"
	
	-- For some reason Safari has (two) invisible windows
	set allWindows to (every window where visible is true)
	set n to count of allWindows
	
	-- Get dock position
	set dock to do shell script "defaults read com.apple.dock orientation"
	
	-- Get actual width to help with left-side docks
	tell application "Finder"
		set screenSize to bounds of window of desktop
		set actualWidth to item 3 of screenSize
		if actualWidth is greater than primaryWidth then set actualWidth to primaryWidth
	end tell
	
	-- Determine space open on your screen, factoring in the Dock
	set screenWidth to do JavaScript "screen.availWidth" in document 1
	set screenHeight to do JavaScript "screen.availHeight" in document 1
	
	-- Get dock size
	set dockSize to actualWidth - screenWidth
	
	-- Get rid of 4 pixel buffer on Dock
	if dockSize is 4 then
		set dockSize to 0
	end if
	
	-- Dividing by zero is bad
	if n is equal to 0 then return
	
	-- Determine each window's width
	set windowWidth to (screenWidth / n)
	set x to 1
	
	-- Position each window in turn
	repeat
		tell window x
			-- 21 is the size of the Menu Bar (for 10.4 at least)
			if (dock is not equal to "left") then
				set bounds to {windowWidth * (x - 1), 21, windowWidth * x, screenHeight}
			else
				set bounds to {windowWidth * (x - 1) + dockSize, 21, windowWidth * x + dockSize, screenHeight}
			end if
		end tell
		set x to x + 1
		if x is equal to (n + 1) then exit repeat
	end repeat
end tell


[ Reply to This | # ]