Created
October 6, 2011 03:59
-
-
Save kevinkirkup/1266475 to your computer and use it in GitHub Desktop.
Applescript Trim Subroutine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
A sub-routine for trimming unwanted characters from strings. | |
Taken from http://www.macosxautomation.com | |
*) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment