Agh! 15 second Q: closing a Safari window w/ a given URL

Agh! I KNOW this has an easy answer:

How do I tell Safari to close the browser window (‘document’) that has a specific URL?
And to make it real fancy, I guess I should check that such a document exists first.
Thanks!

Hi UniAce,

Here’s an example:

tell application “Safari”
set doc_ref to first document whose URL is “http://macscripter.net/
try
close doc_ref
end try
end tell

gl,

That should do the trick, kel. You can also check directly for the existence of the document, thus avoiding the try statement:

tell application "Safari" to tell (first document whose URL is "http://macscripter.net/") to if exists then close

so this is exactly what I need but I want to set it to a list instead of just one URL. I tried set theList to String … but that didnt work either. I know its going to be humbling when I get an answer, but that’s life.


set theList to {"http://www.hmv.co.jp", ¬
			"http://www.cduniverse.com", ¬
			"http://www.google.com", ¬
			"http://www.amazon.com", ¬
			"http://www.allmusic.com/", ¬
			""}
		
		tell application "Safari" to tell (first document whose URL contains theList) to if exists then close

Below is the original script, but I modified the “is” to “contains” hoping to be able to close based on just part of the URL. If that isn’t the way to do it, can someone please enlighten me. :smiley:


		tell application "Safari" to tell (first document whose URL is theList) to if exists then close

Model: Dual 2 GHz PowerPC G5
AppleScript: 2.2
Browser: Safari 523.10.6
Operating System: Mac OS X (10.5)

You need to repeat the check and close for each item in the list

set theList to {"http://www.hmv.co.jp", ¬
	"http://www.cduniverse.com", ¬
	"http://www.google.com", ¬
	"http://www.amazon.com", ¬
	"http://www.allmusic.com/", ¬
	""}
repeat with i from 1 to number of items in theList
	
	set this_item to item i of theList
	tell application "Safari" to tell (first document whose URL contains this_item) to if exists then close
	
end repeat

something like this


set theList to {"http://www.hmv.co.jp", ¬
	"http://www.cduniverse.com", ¬
	"http://www.google.com", ¬
	"http://www.amazon.com", ¬
	"http://www.allmusic.com/", ¬
	""}

tell application "Safari"
	set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	try
		set theURL to text items 1 thru 3 of (get URL of document 1) as text
		if theURL is in theList then close document 1
	end try
	set AppleScript's text item delimiters to ASTID
end tell

WOOT! thanks mark and stefan. i tried marks first, cuz it was there… but it only worked in AS and not studio which is what im working in, but failed to mention. ha ha… ah sigh. but stefan, that works quite nicely. just plug and play!
thanks guys!

Oh, actually. How do i get this to check all safari windows and close them?


tell application "Safari"
   set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
   try
       set theURL to text items 1 thru 3 of (get URL of document 1) as text
       if theURL is in theList then close document 1