Create new Safari window (or tab) and center on screen

I find it annoying that whenever I create a new window in Safari it appears to open in a tiled fashion. I’m a neat freak and like my windows to all be centered on the screen and touching the top edge (at the menu bar line). With the help of some bits and pieces I collected from here and around I came up with this. I hope it helps other OCD/neat freaks out, too.

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

if theName is "Safari" then
	
	(*Creates a new browser window*)
	tell application "Safari" to activate
	tell application "Safari"
		make new document
	end tell
	
	(*Centers window on screen*)
	tell application "Finder"
		set screenSize to bounds of window of desktop
		set screenWidth to item 3 of screenSize
	end tell
	
	tell application "System Events"
		set myFrontMost to name of first item of ¬
			(processes whose frontmost is true)
	end tell
	
	try
		tell application myFrontMost
			set windowSize to bounds of window 1
			set windowXl to item 1 of windowSize
			set windowYt to item 2 of windowSize
			set windowXr to item 3 of windowSize
			set windowYb to item 4 of windowSize
			
			set windowWidth to windowXr - windowXl
			
			set bounds of window 1 to {¬
				(screenWidth - windowWidth) / 2.0, ¬
				windowYt, ¬
				(screenWidth + windowWidth) / 2.0, ¬
				windowYb}
		end tell
	end try
	(*Moves window to top edge of screen*)
	set cur_app to (path to frontmost application as Unicode text)
	if cur_app ends with ":Finder.app:" then
		set Finder to true
		tell application "Finder"
			set tool_vis to toolbar visible of front window
		end tell
	else
		set Finder to false
	end if
	
	
	tell application cur_app
		tell front window
			set {x1, y1, x2, y2} to (get bounds)
			set win_height to (y2 - y1)
			set room_top to y1
			if room_top > win_height then
				if Finder then
					set y1 to (y1 - win_height)
					set y2 to (y2 - win_height)
				else
					set y1 to (y1 - win_height)
					set y2 to (y2 - win_height)
				end if
			else
				if Finder then
					set y1 to 43
					set y2 to (y1 + win_height)
				else
					set y1 to 0
					set y2 to win_height
				end if
			end if
			
			# stupid hack for 10.5/10.6 Terminal.app
			# see http://openradar.appspot.com/5765608
			if cur_app ends with ":Terminal.app:" then
				set win_height to (y2 - y1)
				set y1 to (y1 + win_height)
				set y2 to (y2 + win_height)
			end if
			
			set bounds to {x1, y1, x2, y2}
			
		end tell
	end tell
end if

I found here many unneeded operations.
And 2 bugs. 1) When the script executed from the script editor it only terminates, as the frontmost app is not Safari but script editor. 2) No need create second window (and the original window remains on the desktop) it is enough center the original window on the desktop.

No need to check if the frontmost app is “Finder”, or "Terminal after validating that frontmost app is “Safari”. No need to check if y2>y1, as it is.

So, the things are much simpler:


tell application "System Events"
	set myFrontMost to name of the first process whose frontmost is true
	if (myFrontMost is "Script Editor") or (myFrontMost is "Script Debugger") then
		set visible of process myFrontMost to false
		-- Get name of app, which was frontmost before the script editor
		set myFrontMost to name of the first process whose frontmost is true
	end if
end tell

if myFrontMost is "Safari" then -- Center Safari window on the screen
	tell application "Finder" to set desktopWidth to item 3 of (get bounds of window of desktop)
	tell application "Safari"
		tell front window
			set {x1, y1, x2, y2} to bounds
			set bounds to {(desktopWidth + x1 - x2) / 2, 0, (desktopWidth + x2 - x1) / 2, y2 - y1}
		end tell
		activate
	end tell
end if

Other way to achieve the same effect is this:


set myFrontMost to name of application ((path to frontmost application) as Unicode text)

tell application "System Events" to if (myFrontMost is "Script Editor") or ¬
	(myFrontMost is "Script Debugger") then set visible of process myFrontMost to false
delay 0.1

set myFrontMost to name of application ((path to frontmost application) as Unicode text)

if myFrontMost is "Safari" then
	tell application "Finder" to set desktopWidth to item 3 of (get bounds of window of desktop)
	tell application "Safari" to tell front window
		set {x1, y1, x2, y2} to bounds
		set bounds to {(desktopWidth + x1 - x2) / 2, 0, (desktopWidth + x2 - x1) / 2, y2 - y1}
	end tell
	tell application "Safari" to activate
end if