Can't figure out how to get Applescript to wait for a window to open

I am fairly new to Applescript, but was able to create the script below. The only major thing I was not able to figure out was how to get the process to wait until a certain window opens. I worked around the issue by creating big delay points, but would like the script to be as quick as possible. Any ideas on how to clean this up or make it faster? Each delay is there in the hopes that the window needed in a following step will open in less time than given.

And thanks ahead of time for any help or suggestions!

tell application "Finder"
	open location "smb://domain;user:password@192.168.1.XXX/Volume"
	delay 5 -- this is only here until I can figure out how to make the next step wait until window 1 opens
	close window 1
end tell
tell application "appname"
	activate
	delay 17  -- this is only here until I can figure out how to make the next tell wait until the window "windowname" from the next tell opens
end tell
tell application "System Events"
		tell application process "appname"
			set position of window "windowname" to {0, 22}
			set size of window "windowname" to {1440, 300}
		end tell
	end tell
tell application "secondappname"
	activate
	delay 10  -- same as before, need to wait until window "secondappwindow1" opens
end tell
tell application "System Events"
	tell process "secondappname"
		tell window "secondappwindow1"
			click button "OK"
			delay 3  -- same as before, need to wait until window "secondappwindow2" opens
		end tell
		tell window "secondappwindow2"
			click button "OK"
		end tell
		set position of window "secondappname" to {0, 284}
	end tell
end tell

This may help (shows two different methods):

tell application "Finder"
	open location "smb://domain;user:password@192.168.1.XXX/Volume"
	repeat while not (exists window 1)
	end repeat
	close window 1
end tell

activate application "appname"
delay 1
tell application "System Events"
	tell application process "appname"
		repeat
			try
				set position of window "windowname" to {0, 22}
				set size of window "windowname" to {1440, 300}
				exit repeat
			end try
		end
	end tell
end tell
	
activate application "secondappname"
delay 1
tell application "System Events"
	tell process "secondappname"
		repeat
			try
				click button "OK" of window "secondappwindow1"
				exit repeat
			end try
		end repeat
		
		repeat
			try
				click button "OK" of window "secondappwindow2"
				exit repeat
			end try
		end repeat
		
		set position of window "secondappname" to {0, 284}
	end tell
end tell

Thanks for the edits, JJ.

When I run the script I get the AppleScript Error “Finder got an error. Can’t continue.” and Script Editor is highlighting the ‘close window 1’ of the first tell. Shortly there after Finder displays Error code -43.

If I replace the first tell,

tell application "Finder"
   open location "smb://domain;user:password@192.168.1.XXX/Volume"
   repeat while not (exists window 1)
   end repeat
   close window 1
end tell

with this original:

tell application "Finder"
   open location "smb://domain;user:password@192.168.1.XXX/Volume"
   delay 5 -- this is only here until I can figure out how to make the next step wait until window 1 opens
   close window 1
end tell

then the script seems to work again, but I still have the first delay point.

I tried using ‘Mount Volume’ instead of ‘Open Location’ since ‘Mount Volume’ doesn’t open a Finder window, but I can’t get the volume to mount 100% of the time using that command.

Any other suggestions or tricks that could help?

Thanks again

Well, I did much more reading and a lot more trial and error and figured out the solution to my problem. Here is the solution I came up with, in case any of you out there are having a similar problem. Any comments or suggestions would be appreciated.

There is a problem that I’m having that I haven’t yet figured out. I have a static text that, if I click on it with the mouse, something happens. However, I was not able to find an action with the UI Element Inspector. Can anyone point me in the right direction?


tell application "Finder"
repeat
mount volume "smb://domain;user:password@192.168.1.XXX/Volume"
set theDisks to list disks
if theDisks contains "Volume" then exit repeat
end repeat
end tell


activate application "appname"
tell application "System Events"
repeat
set theApps to name of every process
if theApps contains "appname" then exit repeat
end repeat

tell application process "appname"
repeat
set theWindows to name of every window
if theWindows contains "windowname" then exit repeat
end repeat

set position of window "windowname" to {0, 22}
set size of window "windowname" to {1440, 300}
tell window "windowname"
tell button 2
click
click
end tell
end tell
end tell
end tell


activate application "secondappname"
tell application "System Events"
repeat
set theApps to name of every process
if theApps contains "secondappname" then exit repeat
end repeat

tell process "secondappname"
repeat
set theWindows to name of every window
if theWindows contains "secondappwindow1" then exit repeat
end repeat
click button "OK" of window "secondappwindow1"
repeat
set theWindows to name of every window
if theWindows contains "secondappwindow2" then exit repeat
end repeat
click button "OK" of window "secondappwindow2"
repeat
set theWindows to name of every window
if theWindows contains "secondappname" then exit repeat
end repeat
set position of window "secondappname" to {0, 284}
end tell
end tell