Pulling a link out of a text file?

I’m working on a program that pulls a link out of a text file and opens it in Safari. The safari part and making the text file aren’t hard but I can’t pull the link out of the text file in its whole. When I made a simple program to pull a link out it would only pull out things between slashes. Is there a way I can fix that?

set stringPass to "xg ghj gsdah gfsdajkk www.google.com/hello bob cat dog www.google.com/xqsat"

linkFind(stringPass)

on linkFind(stringPassed)
set linkList to {}
set linkInt to 1
set stringLength to count of words in stringPassed
repeat with i from 1 to stringLength by 1
if word i in stringPassed begins with “www” then
copy word i of stringPassed to the end of linkList
end if
end repeat
display dialog linkList
end linkFind

I know that the display dialog thing won’t work but that isn’t the problem. This will only pull out the www.google.com and not the hello or the xqsat when it gets to the links. Please help! Thanks guys.

Hi,

‘words’ in Applescript doesn’t always mean what you think. Alongside spaces, tabs etc. word delimiters include “/” (and various others I can’t remember). To only split by spaces use ‘Applescript’s text item delimiters’ (it’s probably best to do a search on this site if you want more info about TIDs).

Try this:

set stringPass to "xg ghj gsdah gfsdajkk [url=http://www.google.com/hello]www.google.com/hello[/url] bob cat dog [url=http://www.google.com/xqsat]www.google.com/xqsat"[/url]

set myLinks to linkFind(stringPass)
--> {"www.google.com/hello", "[url=http://www.google.com/xqsat]www.google.com/xqsat[/url]"}

on linkFind(stringPassed)
	set linkList to {}
	set linkInt to 1
	
	-- Using Applescript's text item delimiters.
	set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {" "}}
	set wordList to text items of stringPassed
	-- Always return to previous state.
	set AppleScript's text item delimiters to myTID
	
	set stringLength to count of wordList
	repeat with i from 1 to stringLength by 1
		if (item i of wordList) begins with "www" then
			copy (item i of wordList) to the end of linkList
		end if
	end repeat
	-- Pass the result back.
	return linkList
end linkFind

Best wishes

John M