Problem adding if/then/else properly

All,

Yesterday I published a script that creates a new window in Safari, pushes it to the top edge of the screen and centers it on the screen. Very handy.

This morning I’ve tried to make it a bit fancier by adding an if/then/else clause so that I could use the same script (and thus the same keystroke in FastScripts) to create a new email in Mail that would do the same thing. For some reason I’m having difficulty finding the right place for the if/then/else commands and can’t get the script to work with both Safari and Mail. (My penalty, I guess, for trying to fancy-fy something simple.)

Here is the script that I posted yesterday that works perfectly with just Safari:

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

Here is what I’m trying to add so that it does the same thing in Mail:


	if theName is "Mail" then
		tell application "Mail" to activate
		tell application "System Events"
			keystroke "n" using {command down}
		end tell


Can some kind coder help me figure out where to put this other command and how to properly enter the if/then clauses so that this script will work with either Safari or Mail, depending on which one is the frontmost application?

Thanks in advance.

It would be something along the lines of:

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

if theName is "Safari" then
	
	(*Creates a new browser window*)
	tell application "Safari" to activate
	tell application "Safari"
		make new document
	end tell
else if theName is "Mail" then
	tell application "Mail" to activate
	tell application "System Events"
		keystroke "n" using {command down}
	end tell
end if


(*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

That works perfectly. Many thanks. You’ve made an obsessive window-centerer very happy today. I really appreciate your assistance.

You’re welcome. Welcome to MacScripter :slight_smile: