Can AppleScript detect when an address is exited in Safari?

I’m writing a script that will do the following:

(1) Set the user agent reported by Safari to Firefox and

(2) Open an URL that requires Firefox and won’t open in Safari unless Safari reports itself as Firefox.

If possible, I would like the script to remain open, and wait until the specified URL is closed in Safari, so that it can automatically restore Safari’s default setting, and Safari can report itself as Safari.

Is it possible for AppleScript to run a repeat loop that will check whether a specific URL is open in Safari, and perform a specific action if it is NOT open?

Thanks for any help with this.

I don’t know whether what you want to accomplish is possible, but you shouldn’t use a repeat loop to do it – that ties up a ton of cpu cycles. The normal way to do what you want is to use an “on idle” script with a return setting the time. There are lots of examples on the board.

Thank you - that was the crucial step. I’ll post my code here later on, in case anyone is interested, but meanwhile, the steps that I was asking for look something like this:

set testURL to ""
	set urlIsOpen to true
	tell application "Safari" to set url_list to URL of every document
	if not (url_list contains testURL) then
		tell me to activate
		display dialog "Exited"
		error number -128
	end if

if I put this into an on idle block, then I should be able to do what I was trying to do. Thank you again!