Two TID-based text handlers: findtext, removetext
Two TID-based text handlers: findtext, removetext
- Subject: Two TID-based text handlers: findtext, removetext
- From: Charles Arthur <email@hidden>
- Date: Sun, 3 Jun 2001 19:11:02 +0100
Hi..
This is likely to be old hat and obvious to experienced scripters, but more
for the benefit of people who are just wondering about the benefits of
using text item delimiters (for find and replace, where they're invaluable).
One thing I realised is that while scriptable text editors are OK, you can
actually do many of the text-based work you like using TIDs. And the speed
advantages are huge; and being Applescript-native, they're portable to all
sorts of machines, back to... whenever TIDs were introduced.
Anyway, Bill Briggs wrote a good article about using TIDs to do search +
replace at maccentral.com. I've used that to develop two more handlers:
one, "findtext", where you provide a string, a starting search string and a
finishing search string (which has to be different from the start), and
extracts a list of strings that lie between those in the string.
The other, removetext, *takes out* strings that lie between the starting
search string and the finish search string.
The handlers return a list of strings, or {""} if there's nothing matching
the terms. I think they can be very useful for (a) finding URLs or
instances of a phrase, or alternatively (b) taking out stuff, eg all HTML
tags (as they're between "<" and ">").
Some examples:
set thestring to "that1this2that3this4that5"
removetext(thestring, "that", "this")
--> {"", "2", "4", ""} - the strings which don't lie between "that" and "this"
set thestring to "that1this2that3this4that5"
findtext(thestring, "that", "this")
--> {"1", "3", "5"} - the strings which do lie between "that" and "this"
The way they work is-
-first, turn the string into a list delimited by the first searchterm.
-next (if you're finding text) get rid of the first item in the list: it
must have the searchterm in front of it (in the original string) so it
can't be right
-alternatively, if you're removing text, keep that first item as it wasn't
after the first search term
-next, repeat for each item you've retained:
-set the TID to the second searchterm; now either keep (when
finding) or discard (when removing) the first item in the list thus created.
Here are the handlers. Comments welcome - I suspect it would be feasible to
speed them up further by creating the list of result strings using "a
reference to"?
----
on removetext(passedstring, startterm, endterm)
--from passedstring, removes every item of text occuring which lies
between startterm and endterm
--and puts what is left into returnlist
set returnlist to {}
set AppleScript's text item delimiters to ""
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to startterm
set thelist to text items of passedstring
--if (count of items in thelist) > 1 then
-- there must be something to find.
--But don't know if starterm was at the beginning or end; have to
deal with that
set returnlist to {item 1 of thelist}
--for when the thing to be removed lies after the first character:
-- item 1 you are keeping
if (count items in thelist) > 1 then
repeat with tempcount from 2 to (count items in thelist)
set thetemp to item tempcount of thelist
--now look for the end point
set AppleScript's text item delimiters to endterm
set thetemp to text items of thetemp
-- item 1 of this list is now what we want to get
RID of
set whatsleft to the rest of thetemp
set AppleScript's text item delimiters to astid
set whatsleft to whatsleft as string
set returnlist to returnlist & whatsleft
end repeat
end if
set AppleScript's text item delimiters to astid -- TID police!
return returnlist
end removetext
----
on findtext(passedstring, startterm, endterm)
--from passedstring, extracts every item of text occuring
--which lies between startterm and endterm
--and puts it into returnlist
set returnlist to {}
set AppleScript's text item delimiters to ""
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to startterm
set thelist to text items of passedstring
if (count of items in thelist) > 1 then
set thelist to items 2 thru -1 of thelist
--display dialog "Count of items is " & (count items in
thelist)
repeat with tempcount from 1 to (count items in thelist)
set thetemp to item tempcount of thelist
set AppleScript's text item delimiters to astid
set thetemp to thetemp as string
--display dialog thelist
set AppleScript's text item delimiters to endterm
set thetemp to text items of thetemp
set theURL to item 1 of thetemp
--display dialog theURL as string
set AppleScript's text item delimiters to astid
set theURL to theURL as string
set returnlist to returnlist & theURL
end repeat
else -- if there's only 1 item in the list, ie no matches
set returnlist to {""}
end if
return returnlist
end findtext
----
Charles
http://www.ukclimbing.com : 1,000+ British crags, 350+ British climbing walls
- searchable by distance rock type, etc, with 5-day weather forecasts for
every one - plus maps, articles, news, and the New Routes database. There's
even a cool shop attached...