I need to trim a string of whitespace left and right. At the moment I do it like this:
-- trim theName
repeat until first character of theName is not " "
set theName to (characters 2 thru -1 of theName) as string
end repeat
repeat until last character of theName is not " "
set theName to (characters 1 thru -2 of theName) as string
end repeat
Apart from the fact that it only cares for spaces (which I could fix with a ‘…is not SPACE and is not TAB and is not …’ construct), I think my solution is not very “applescriptonic”. Is there a way to make it more elegant, preferably with less code as well?
That only works if the name doesn’t contain any whitespaces in between. A complete solution is then:
set theName to " john "
-- make sure text item delimiters is set correctly
set oldTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
set theName to words of theName as string
set AppleScript's text item delimiters to oldTIDs
return theName
But as you pseudo example it will remove all whitespaces, not only the surrounding.