Hey,
Newbie at Applescript needing to remove any trailing spaces from the end of a line of text or characters.
I have this working:
set newText to value of variable "FMID_Test"
set {old_delim, text item delimiters} to {text item delimiters, space}
set {newText, text item delimiters} to {words of newText as string, old_delim}
newText
set value of variable "FMID_Test" to newText
with the exception that if the string contains a character other that text, e.g. “-” that gets
removed as well. 233-555-5555 becomes 233 555 5555.
Any help with a fix greatly appreciated!
Thanks,
Carl
there is an tool called sed in bash (unix) that you can use to remove some patterns
set theString to "Hello world! "
do shell script "sed 's/[ ]*$//' <<< " & quoted form of theString
it removes trailing spaces and tabs. you can put any trailing character (read: not substrings) between those brackets that are not allowed at the end of the line.
Here’s 2 handlers I’ve used over the years to remove leading and trailing spaces from text. Good luck.
on removeLeadingSpaces(theString)
repeat while theString begins with space
set theString to text 2 thru -1 of theString
end repeat
return theString
end removeLeadingSpaces
on removeTrailingSpaces(theString)
repeat while theString ends with space
set theString to text 1 thru -2 of theString
end repeat
return theString
end removeTrailingSpaces
Thanks a bunch for all the help.
Been trying to get this version to work:
set theString to value of variable "FMID_Test"
on removeTrailingSpaces(theString)
repeat while theString ends with space
set theString to text 1 thru -2 of theString
end repeat
return theString
set the value of variable "FMID_Test" to theString
end removeTrailingSpaces
but I can’t make go. Maybe something to do with what I have defined for the theString?
Thanks,
Carl
set theString to value of variable "FMID_Test"
set the value of variable "FMID_Test" to theString
What are those 2 lines? They do not make sense to applescript. “value of the variable” is wrong so you need to show where your string is coming from.
So first you need a string and then you feed it to the handler like this…
set someString to "some text with spaces at the end. "
set noTrailingSpacesString to removeTrailingSpaces(someString)
-- this is a separate handler and just stick it at the end of your code.
-- you call the handler as I showed above
on removeTrailingSpaces(theString)
repeat while theString ends with space
set theString to text 1 thru -2 of theString
end repeat
return theString
end removeTrailingSpaces
I’m actually running this script within my home automation app, Indigo, which does recognize the variable
in the script. This script works fine to remove any spaces but also removes any characters that aren’t text letters.
set newText to value of variable "FMID_Test"
set {old_delim, text item delimiters} to {text item delimiters, space}
set {newText, text item delimiters} to {words of newText as string, old_delim}
newText
set value of variable "FMID_Test" to newText
I tried this version as well but it doesn’t remove the spaces.
set someString to value of variable "FMID_Test"
set noTrailingSpacesString to removeTrailingSpaces(someString)
-- this is a separate handler and just stick it at the end of your code.
-- you call the handler as I showed above
on removeTrailingSpaces(theString)
repeat while theString ends with space
set theString to text 1 thru -2 of theString
end repeat
return theString
set the value of variable "FMID_Test" to theString
end removeTrailingSpaces
Sorry, I’m a real noob at all this and do appreciate the help.
Thanks,
Carl
Doh…got it working…just removed the return theString.
Many thanks!!
Carl
I’m glad you got it working. Here’s how I would write your script. Good luck.
set someString to value of variable "FMID_Test"
set the value of variable "FMID_Test" to removeTrailingSpaces(someString)
on removeTrailingSpaces(theString)
repeat while theString ends with space
set theString to text 1 thru -2 of theString
end repeat
return theString
end removeTrailingSpaces
With ‘any trailing spaces’ I though the amount of trailing spaces was unknown. My pro against the ‘text 1 thru -2’ solution is that if there is no trailing space or even hundred trailing spaces it works as expected.
I tried the above script as well with variations of text, characters and spaces and it works fine.
Thanks again!
Carl