Closing a Safari window when it reaches a certain URL?

I want to make an applescript that launches/activates Safari, makes a new window, goes to a URL in the window, and then closes the window WHEN the window reaches a certain other URL.

So far I have:

tell application "Safari"
	launch
	make new document at the beginning of documents
	set the URL of the front document to "http://INITIALWEBPAGE/"
end tell

I just have to figure out how to get it to close the window when it gets to a second, specified webpage (because the user will be surfing through some stuff that will eventually take him/her there). Thanks!

Model: G5 dual 2.3ghz, 2gb RAM
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

Hi UniAce,

You could try something like:

tell application "Safari"
	if URL of front document contains "http://LASTWEBPAGE" then
		close front window
	end if
end tell

You will need to set it up to watch for these conditions by having a repeat loop of some desciption. Possibly something along the lines of:

tell application "Safari"
	repeat
		if URL of front document contains "http://LASTWEBPAGE" then exit repeat
		delay 2 -- seconds
	end repeat
	close front window
end tell

Best wishes

John M

I am afraid that might bring Safari almost to a halt, as it causes Safari delays of 2 secs each.
Using

tell me to do shell script ("Sleep 2")

instead of:


delay 2

causes the script to wait instead of Safari without processor load.

Thanks very much for the help. The IF statement was causing trouble (maybe needed an else or something), so here’s what I’ve got working now:

tell application "Safari"
	launch
	make new document at the beginning of documents
	set the URL of the front document to "http://FIRSTWEBPAGE"
	delay 1
	
	repeat until URL of the front document contains "http://LASTWEBPAGE"
		tell me to do shell script ("Sleep 2")
	end repeat
	close front window
end tell

The “delay 1” was needed to give Safari a chance to load the URL before Applescript tried to check it.

PS - I’m assuming “document” and “window” are interchangeable?