Search for the perfect ATID handler
Search for the perfect ATID handler
- Subject: Search for the perfect ATID handler
- From: Joseph Weaks <email@hidden>
- Date: Sat, 20 Mar 2004 13:20:23 -0600
So, here's the current version of my all-purpose Applescript Text Item
Delimiters handler, for using in creating a list, truncating a string,
grabbing an item, and find/replace.
I think restricting it to max of 4 passed variables keeps it functional
and easy to remember in regular use.
Anyone care to improve on it, suggest other "Functions", or post theirs
for comparison?
Cheers,
Joe
to useTIDs(theString, theDelim, theFunction, theVariable)
(*
-- A utility handler for Applescript's Text Item Delimiters
-- Contributed by Joe Weaks
-- theFunction options:
-- replace =
-- replaces theDelim with theVariable and returns string
-- select =
-- returns list of items
-- if theVariable is a list of two numbers, then that range as list
-- such as 1 thru -2 with theDelim of "." to remove an extension or ":"
to parse a path
-- if theVariable is a single number, returns that items as list
-- if theVariable is 0 or random, returns a random item from the list
-- count =
-- returns the count of text items in the resulting list
*)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelim
set theResult to text items of theString
try
if theFunction is "replace" then
set AppleScript's text item delimiters to theVariable
set theResult to theResult as string
else if theFunction is in {"select", "pick", "truncate"} then
if (class of theVariable is list) and ((count text items in
theVariable) is 2) then
set theResult to items (item 1 of theVariable as string) thru (item
2 of theVariable as string) of theResult
else if (theVariable is 0) or (theVariable is "0") or (theVariable is
"random") then
set theResult to some item of the theResult
else
set theResult to item theVariable of theResult
end if
else if theFunction is "count" then
set theResult to count text items in theResult
end if
on error errmsg
-- set AppleScript's text item delimiters to tid
-- Display dialog errmsg
-- Returns theString as a list
end try
set AppleScript's text item delimiters to tid
return theResult
end useTIDs
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.