Is this a bug or am I crazy?

I have a massive list of assorted links, some are “http://” URLs and some are “mailto:” email links and I’m just trying sort them out. When I run the following on my Intel mini it returns all the “http” web urls as expected:

set webList to {}
set mixList to {"http://www.fakesite.com", "mailto:bozo@bozotheclown.net", "http://www.apple.com", "mailto:steve@apple.com", "mailto:me@you.com"}

repeat with link in mixList
	if link contains "http:" then
		set webList to webList & link
	end if
end repeat

BUT when I run the following it returns nothing:

set emailList to {}
set mixList to {"http://www.fakesite.com", "mailto:bozo@bozotheclown.net", "http://www.apple.com", "mailto:steve@apple.com", "mailto:me@you.com"}

repeat with link in mixList
	if link contains "mailto:" then
		set emailList to emailList & link
	end if
end repeat

Now, here’s the even weirder thing; When I run these on my G4 PowerBook I get the exact opposite results. The mailto one works fine but the http one doesn’t. Any ideas on this? I’m totally stumped.

You don’t say what system you’re using, but in OS X 10.4.7 running on a dual-core G5 they both work as they should.

Obviously you can combine them:

set webList to {}
set mailList to {}
set mixList to {"http://www.fakesite.com", "mailto:bozo@bozotheclown.net", "http://www.apple.com", "mailto:steve@apple.com", "mailto:me@you.com"}

repeat with link in mixList
	if contents of link contains "//" then set end of webList to contents of link
	if contents of link contains "@" then set end of mailList to contents of link
end repeat

I’ve used “contains” because link is a reference to an item in the list, not the item. Sometimes these will coerce properly and sometimes not. Perhaps that’s your prob.

both of my machines are 10.4.7 as well.

I’ve edited my answer. Try that.

this is interesting. i actually tried “contents of” earlier with the same results, as well as “name of” or “name contains”, “begins with” etc. it now looks as though my only problem appears to be that the parsed results aren’t automatically displaying for me. just adding “return listName” shows the right results for all the scripts. it’s always something silly. :rolleyes:

thanks for the help.

On my machine, the “mailto” script returns a list but the “http” one (as presented) doesn’t return anything. This is because the last item in mixList contains “mailto” but not “http”. The very last iteration of the “mailto” repeat therefore sets emailList to emailList & link, whereas the last iteration of the “http” repeat does nothing and so returns no result.

But the list exists. If you want to see it, you have to do something after the repeat that gets it as a result:

set webList to {}
set mixList to {"http://www.fakesite.com", "mailto:bozo@bozotheclown.net", "http://www.apple.com", "mailto:steve@apple.com", "mailto:me@you.com"}

repeat with link in mixList
	if link contains "http:" then
		set webList to webList & link
	end if
end repeat

webList

Edit: I see you’ve already discovered this while I was previewing my post. :slight_smile:

Edit while posting: I see Mr G. beat me to it again. Next week I’m going on a speed-typing course! :stuck_out_tongue:

Are you running the tests on both machines with exactly the same list, Muad?

With the lists you’ve shown above, the “mailto:” search will appear to work, while the search for “http:” will seem to fail.

That’s because the last string in the list contains “mailto:” - so the last statement that the script executes is the concatenation of emailList & link. It’s this that is returned.

In the final iteration of the “http:” search loop, there’s no match - so no action is taken, and therefore nothing is returned.

So, for consistent results, you need to explicitly ask the script to return the value of emailList or webList, by placing the relevant variable at the end of the script.

(It’s also more efficient to add items to the end of a list, rather than concatenate the entire list with a new value each time. With this method in this form of repeat loop, the variable link is a reference - so you’ll also need to extract its contents.)


set webList to {}
set mixList to {"http://www.fakesite.com", "mailto:bozo@bozotheclown.net", "http://www.apple.com", "mailto:steve@apple.com", "mailto:me@you.com"}

repeat with link in mixList
	if link contains "http:" then set webList's end to link's contents
end repeat

webList (* or the explicit form: return webList *)
--> {"http://www.fakesite.com", "http://www.apple.com"}



set emailList to {}
set mixList to {"http://www.fakesite.com", "mailto:bozo@bozotheclown.net", "http://www.apple.com", "mailto:steve@apple.com", "mailto:me@you.com"}

repeat with link in mixList
	if link contains "mailto:" then set emailList's end to link's contents
end repeat

emailList (* or the explicit form: return emailList *)
--> {"mailto:bozo@bozotheclown.net", "mailto:steve@apple.com", "mailto:me@you.com"}


thanks for the efficiency tip kai. :slight_smile: