Check if window is open

I would like to check if a program is running,
if not, launch (which automatically opens a new window)
if running, then is there an open window
if not, open window
if open window, use window

Here’s my script, but it fails if a window is not open.


tell application "Terminal"
	launch
	repeat with theWindow in window 1
		do script "rsync -avz -e ..." in theWindow
		do script "passwd" in theWindow
		do script "exit" in theWindow
	end repeat
end tell


Something like this?

tell application "System Events"
	if "Terminal" is not in name of processes then launch application "Terminal"
	tell process "Terminal"
		set frontmost to true
		if (count windows) is 0 then keystroke "n" using command down
	end tell
end tell
tell application "Terminal" to tell window 1
	do script "rsync -avz -e ..." in it
	do script "passwd" in it
	do script "exit" in it
end tell