tell application "Finder"
set TheText to "Check out this awesome site. http://macscripter.net/ AppleScript rocks!"
set _Result to (characters 30 thru -20 of TheText as text)
display dialog _Result
end tell
You want this to work for any sentence that contains a URL? Will the URL always start with “http://” end with a “/”? Otherwise checking that a URL is valid is not a trivial pursuit.
I can assume for now that every URL will begin with http but not that it will end with ‘/’.
I’m thinking that it will be easier to work with the characters of theText rather than the words. Still, I’m having trouble conceptualizing how to extract the links using the characters.
You could just check for expected characters after the URL text and split with text item delimiters.
Something like:
-- John Maisey -- 29 Jan 2009 -- [url=http://www.nhoj.co.uk]www.nhoj.co.uk[/url] --
-- The text you're looking at.
set myText to "Check out this awesome site. http://macscripter.net/ AppleScript rocks!"
-- Holder for the result.
set myResult to {}
-- Items that you expect will be after the end of the URL. You may want to adjust this list.
set terminatorList to {" ", ">", ".", ",", ")", return, tab}
-- Split by 'http://" and only get the second to the last parts.
set myList to my textItemDelimiterSplit(myText, "http://", 2, -1)
-- Loop through list returned.
repeat with myItem in myList
-- Loop through possible ending characters .
repeat with myTerminator in terminatorList
-- Split by the ending character.
set myItem to my textItemDelimiterSplit(myItem, myTerminator, 1, 1)
end repeat
-- Add the result to an array.
set myResult to myResult & ("http://" & myItem as text)
end repeat
-- Return the array.
return myResult
--TID splitter
on textItemDelimiterSplit(theText, theTID, theFrom, theTo)
try
set my text item delimiters to theTID
set myReturn to text items theFrom thru theTo of theText
set my text item delimiters to {""}
return myReturn
on error
return {}
end try
end textItemDelimiterSplit
A word of warning. Your friend’s script fails for the example below and for any sentence that ends with a URL. John Maisey’s version gets them successfully no matter where they fall in the sentence. In the example below, add something after the last URL (beginning with a space) and the script works; e.g. “Check out this awesome site. http://macscripter.net/http://studio.golbin.net AppleScript rocks! Also see http://www.journler.com and http://forums.macosxhints.com/. Got that?” It’s the " Got that" ending that saves it.
set theText to "Check out this awesome site. http://macscripter.net/ http://studio.golbin.net AppleScript rocks! Also see http://www.journler.com and [url=http://forums.macosxhints.com/]http://forums.macosxhints.com/"[/url]
set startWord to "http"
set urlList to {}
repeat
set startOffset to the offset of startWord in theText
if startOffset is 0 then exit repeat
set offsetCount to startOffset
set theURL4list to ""
repeat
set theURL to item offsetCount of theText
if theURL is " " then
set urlList to urlList & theURL4list
exit repeat
else
set theURL4list to theURL4list & theURL
end if
set offsetCount to offsetCount + 1
end repeat
set theText to items offsetCount thru -1 of theText as string
end repeat
urlList