Text Manipulation
Manipulating text strings is one of the most common tasks performed in scripts. The following sub-routines perform some of the most common text manipulation tasks.
Trim Line
The following sub-routine can be used to trim text from the beginning or end of a string. It has three passed parameters:
- The text to trim
- The characters to trim from the passed text
- The direction indicator
The direction indicator has three possible numeric values:
0, which tells the routine to trim the indicated characters from the beginning of the passed string:
set this_text to "----1----"
trim_line(this_text, "-", 0)
--> returns: "1----"
1, which tells the routine to trim the indicated characters from the end of the passed string:
set this_text to "12345.txt"
trim_line(this_text, ".txt", 1)
--> returns: "12345"
2, which tells the routine to trim the indicated characters from both ends of the passed string:
set this_text to "*-*-fred*-*-"
trim_line(this_text, "*-", 2)
--> returns: "fred"
Here's the trimming sub-routine:
on trim_line(this_text, trim_chars, trim_indicator)
-- 0 = beginning, 1 = end, 2 = both
set x to the length of the trim_chars
-- TRIM BEGINNING
if the trim_indicator is in {0, 2} then
repeat while this_text begins with the trim_chars
try
set this_text to characters (x + 1) thru -1 of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
-- TRIM ENDING
if the trim_indicator is in {1, 2} then
repeat while this_text ends with the trim_chars
try
set this_text to characters 1 thru -(x + 1) of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return this_text
end trim_line
The following routine uses the trim_line sub-routine to remove unwanted characters from multiple paragraphs:
on trim_paragraphs(this_text, trim_chars, trim_indicator)
set the paragraph_list to every paragraph of this_text
repeat with i from 1 to the count of paragraphs of this_text
set this_paragraph to item i of the paragraph_list
set item i of the paragraph_list to trim_line(this_paragraph, trim_chars, trim_indicator)
end repeat
set AppleScript's text item delimiters to return
set this_text to the paragraph_list as string
set AppleScript's text item delimiters to ""
return this_text
end trim_paragraphs
Change Case
The following sub-routine will change the case of alpha characters of passed text to either all lower or all caps. The sub-routine has two passed parameters: 1) the passed text to alter, 2) a case indicator. A case indicator of 0 will have the routine convert the passed text to lower case. A case indicator of 1 will have the routine convert the passed text to all caps.
on change_case(this_text, this_case)
if this_case is 0 then
set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set the source_string to "abcdefghijklmnopqrstuvwxyz"
else
set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the new_text to ""
repeat with this_char in this_text
set x to the offset of this_char in the comparison_string
if x is not 0 then
set the new_text to (the new_text & character x of the source_string) as string
else
set the new_text to (the new_text & this_char) as string
end if
end repeat
return the new_text
end change_case
Replacing Characters in a String
The following sub-routine can be used to replace specific characters in a string. The sub-routine has three passed parameters: 1) the text to alter, 2) to text to search for, 3) the replacement text
set the message_text to "On Tuesday I told you to have the report ready by next Tuesday."
set the message_text to replace_chars(message_text, "Tuesday", "Friday")
--> Returns: "On Friday I told you to have the report ready by next Friday."
Here's the sub-routine:
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars