Unique ID of Safari Window?

Is it possible to uniquely reference a Safari window that is created by a script, in order to do something with it later?

I am having a new Safari window being opened during the course of one of my scripts, and later in the script I want to close it, but want to make sure it closes the correct window. I don’t want to trust that it will be the frontmost window, and I will not have control over the title of the window, so I can’t use that as an identifier.

Is there a way to get or set an unique ID for a particular Safari window?

The id of the window object is what you are looking for.


tell application "Safari"
	make new document at beginning
	set window_id to id of window 1
end tell
tell application "Safari"
	make new document with properties {URL:"http://google.com"} at beginning
	set window_id to id of window 1
	close window window_id
end tell

That let’s me get the ID of the window, but I get an error when I try to use the ID to close the window using the code above.

Edit: The error is gives is: “Safari got an error: NSReceiverEvaluationScriptError: 4”

Try.


tell every window whose id is window_id to close

Try something like this:

tell application "Safari"
	make new document with properties {URL:"http://google.com"} at beginning
	set window_id to id of window 1
	close window id window_id
end tell

Cool, thanks!

So what does the “at beginning” do? Make sure that it’s Document 1? or is Document 1 always the frontmost window?

OK, so I thought this was working pretty well, but it doesn’t always seem to work correctly.

Here’s the relevant code I’ve been using


tell application "Safari"
--activate -- I'm hoping it's not necessary to activate safari for this
make new document with properties {URL:"http://www.example.com"} at beginning
set guruWindow to id of window 1
delay 1.75
set guruBits to text of document 1
try
close window id guruWindow
end try

I almost always have two windows open in safari throughout the day (with various tabs of things I’m reading up on), and this script gets run throughout the day, which temporarily adds a third window.

Normally, it will open a new window, get the text of the URL (which is the result of an intranet form), and then close the window. Occasionally though, it apparently gets confused and the script thinks the background window is still #1 (I’m guessing), because it will get the text of that window, and then close it, leaving the new one in front.

So in effect, the script is failing on both accounts of what I’m wanting to have happen - a way to uniquely “name” the new window that is created by the script, to guarantee that the right text is grabbed, and to make sure the correct window is closed.

I’ve tried it with both Safari been activated first, and without (which would be my preference), and I believe it has the same issue either way.

Any ideas on why it is inconsistent? Any help would be greatly appreciated.

Have you considered using curl in a do shell script construction to get the text of that URL? Safari isn’t involved or disturbed.

That could work - I’ve seen curl but never actually done it. During the course of my script, I access two internal pages, one is a direct link, so I think curl would work well for that. Good idea.

However, the other is an HTML form that accesses a separate perl script on our servers. Can Curl call a URL to a form page and then get the text of the resulting page (the result is a different URL)?