Yet Another Newbie Question about String extraction

Hello there,

Sorry to bother you with such questions, but here is my problem.

My grand-uncle and grand-aunt have recently received their first mac. One of the main use will be genealogy. They use a *nix tool for that, and for the while, they have to use a xterm to launch a script, then have to use a web browser (with an address like http://localhost:portnumber/). But xterm is quite hard to use - just quitting is a mess. So I’d like to write a little script for them which will · Launch the shell script · Open a browser (say, Safari) at the right page So they just have to launch the AppleScript.

But I don’t want the script to open a thousand Safari window. Let’s say that it will firstly try find if a document is already on the right page, if not found, then try to find a document with no page laden, and if still not found, open a new document. It then load the right URL in the chosen window.

The problem is how to detect if a document’s URL is already right. If one’s URL is http://localhost:portnumber/a/long/but/nice/path/, I’d like the Script to make the window in front, without changing the UR nor opening a new window.

The current version is following.

do shell script "/Volumes/Documents/Users/dou/Documents/tst_script"

tell application "Safari"
	activate
	set gwserver to "http://localhost:631/" as string
	set search to true
	repeat with i from 1 to count each document
		if search then
			set current_document to item i of documents
			if not (exists URL of current_document) then
				set URL of current_document to gwserver
				set search to false
			else
				-> string extraction would be great in the next line
				if URL of current_document = gwserver then
				set search to false
			end if
		end if
	end repeat
	if search then
		set current_document to make new document
		set URL of current_document to gwserver
	end if
end tell

Could you please help ? Thanks.

And sorry about the English, but, you know, we French have trouble with foreign languages… :oops:

This works here (not for tabs, of course):

set urlToSearchFor to "http://blah.net"

tell application "Safari"
	try
		do JavaScript "self.focus()" in (first document whose URL is urlToSearchFor)
	on error
		make new document with properties {URL:urlToSearchFor}
	end try
end tell

I guess I wasn’t clear enough.

Here come an exemple. Let’s say were travelling on http://blah.net/pictures/. I’d like the script not to open a new window with URL http://blah.net/.

Still, thanks. Your code is much cleaner (and I’d have never guess that there was such features as first document whose…)

So, if you find a window whose domain is “blah.net” (eg, “blah.net/dir/page.html”), you would like loading the “start page” in such window? (instead of specifically bringing to the front the window whose url is exactly “blah.net”) Eg:

set urlToSearchFor to "http://blah.net" 

tell application "Safari" 
   try
      set theDoc to first document whose URL starts with urlToSearchFor
      do JavaScript "self.focus()" in theDoc --> bring to the front
      set url of theDoc to urlToSearchFor --> navigate to "start" page
   on error 
      make new document with properties {URL:urlToSearchFor} 
   end try 
end tell

Well, in fact, yes, because the server just has to be started once. I guess that if they use the script a second time, It’s because they’ll want to be back to the starting page.

But I’ll see that with somebody who knows the software.

Thank you and happy new year !