Saturday, November 27, 2021

#1 2006-09-07 01:41:06 pm

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Trim [Remove Spaces]

trim:

Applescript:

-- theseCharacters : A list of characters to trim
-- someText : The text to be trimmed
--
on trim(theseCharacters, someText)
   -- Lazy default (AppleScript doesn't support default values)
   if theseCharacters is true then set theseCharacters to ¬
       {" ", tab, ASCII character 10, return, ASCII character 0}
   
   repeat until first character of someText is not in theseCharacters
       set someText to text 2 thru -1 of someText
   end repeat
   
   repeat until last character of someText is not in theseCharacters
       set someText to text 1 thru -2 of someText
   end repeat
   
   return someText
end trim

-- Example
trim(" Hello, World!    ", true)

A slightly simpler version that only removes spaces:

Applescript:

on trim(someText)
   repeat until someText does not start with " "
       set someText to text 2 thru -1 of someText
   end repeat
   
   repeat until someText does not end with " "
       set someText to text 1 thru -2 of someText
   end repeat
   
   return someText
end trim


Filed under: trim, strip

Offline

 

#2 2015-08-10 01:21:37 am

JMichaelTX
Member
From:: Houston, TX (The Woodlands)
Registered: 2014-07-12
Posts: 142

Re: Trim [Remove Spaces]

@Bruce Phillips:  Thanks for sharing your code for a trim function.

I have optimized it somewhat to hopefully:
1. Improve performance for large strings by updating the source string only after calculating the area to be trimmed.
2. Use more descriptive variable names.
3. Added option to trim left, right, or both sides of string.

But the core code is the same.

Applescript:


--

set strTest to "\tsome text here and here\t "
my trimThis(strTest, true, "full")

on trimThis(pstrSourceText, pstrCharToTrim, pstrTrimDirection)
   
   -- pstrCharToTrim     : A list of characters to trim, or true to use default
   -- pstrSourceText : The text to be trimmed
   -- pstrTrimDirection : Direction of Trim ("right","left", "full")
   
   set strTrimedText to pstrSourceText
   
   ---    USE DEFAULT IF true IS PASSED ---
   -- Lazy default (AppleScript doesn't support default values)
   
   if pstrCharToTrim is true then
       set pstrCharToTrim to {" ", tab, ASCII character 10, return, ASCII character 0}
   end if
   
   --- TRIM LEFT SIDE OF STRING ---
   
   if (pstrTrimDirection = "full") or (pstrTrimDirection = "left") then
       set iLoc to 1
       repeat until character iLoc of strTrimedText is not in pstrCharToTrim
           set iLoc to iLoc + 1
       end repeat
       
       set strTrimedText to text iLoc thru -1 of strTrimedText
   end if
   
   --- TRIM RIGHT SIDE OF STRING ---
   
   
   if (pstrTrimDirection = "full") or (pstrTrimDirection = "right") then
       set iLoc to count of strTrimedText
       repeat until character iLoc of strTrimedText is not in pstrCharToTrim
           set iLoc to iLoc - 1
       end repeat
       
       set strTrimedText to text 1 thru iLoc of strTrimedText
       
   end if
   
   return strTrimedText
   
end trimThis


2019 iMac-27 512GB SSD, 3.6Ghz i9 CPU, 40GB RAM
macOS 10.14.6 (Mojave)

Offline

 

#3 2015-08-10 04:39:30 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Trim [Remove Spaces]

Thanks for the update Michael but when bringing old posts back to live you should consider changes of AppleScript through the years as well. Improvements over Michaels scripts are:

- Since AppleScript 2.0 the script should use (character) id instead of ASCII character command.
- AppleScript 2.0 is Unicode, by default all Unicode spaces are supported now, not only limited to MacRoman whitespaces
- BugFix: The handler will return "" if the string contains only whitespaces
- BugFix: The handler will return "" if the string is empty
- Optimization: The string will only be trimmed once, not twice (text n thru n ...)
- Use justification constants left and right to indicate direction, missing value (any value) a full trim will be applied.
- pstrCharToTrim works now with a more obvious value than a boolean. Use missing value (read: undefined) to use default whitespaces. It will also use default values when the given object is not a list instead of returning an error.

Applescript:

set strTest to "    Hello World! "
trimThis(strTest, missing value, left) -- result: "Hello World! "
trimThis(strTest, missing value, right) -- result: "    Hello World!"
trimThis(strTest, {tab, return, linefeed}, missing value) -- result: "Hello World! "

on trimThis(pstrSourceText, pstrCharToTrim, pstrTrimDirection)
   -- pstrCharToTrim     : A list of characters to trim, or true to use default
   -- pstrSourceText : The text to be trimmed
   -- pstrTrimDirection : Direction of Trim left, right or any value for full
   
   set strTrimedText to pstrSourceText
   
   -- If undefinied use default whitespaces
   if pstrCharToTrim is missing value or class of pstrCharToTrim is not list then
       -- trim tab, newline, return and all the unicode characters from the 'separator space' category
       -- [url]http://www.fileformat.info/info/unicode/category/Zs/list.htm[/url]
       set pstrCharToTrim to {tab, linefeed, return, space, character id 160, character id 5760, character id 8192, character id 8193, character id 8194, character id 8195, character id 8196, character id 8197, character id 8198, character id 8199, character id 8200, character id 8201, character id 8202, character id 8239, character id 8287, character id 12288}
   end if
   
   set lLoc to 1
   set rLoc to count of strTrimedText
   
   --- From left to right, get location of first non-whitespace character
   if pstrTrimDirection is not right then
       repeat until lLoc = (rLoc + 1) or character lLoc of strTrimedText is not in pstrCharToTrim
           set lLoc to lLoc + 1
       end repeat
   end if
   
   -- From right to left, get location of first non-whitespace character
   if pstrTrimDirection is not left then
       repeat until rLoc = 0 or character rLoc of strTrimedText is not in pstrCharToTrim
           set rLoc to rLoc - 1
       end repeat
   end if
   
   if lLoc ≥ rLoc then
       return ""
   else
       return text lLoc thru rLoc of strTrimedText
   end if
end trimThis

Last edited by DJ Bazzie Wazzie (2015-08-10 04:44:23 am)

Offline

 

#4 2015-08-10 07:04:52 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6729

Re: Trim [Remove Spaces]

DJ Bazzie Wazzie wrote:

you should consider changes of AppleScript through the years as well.


And why not wink

Applescript:

use AppleScript version "2.4"
use framework "Foundation"

set strTest to "    Hello World! "
trimThis(strTest, missing value, left) -- result: "Hello World! "
trimThis(strTest, missing value, right) -- result: " Hello World!"
trimThis(strTest, tab & return & linefeed, missing value) -- result: "Hello World! "

on trimThis(pstrSourceText, pstrCharToTrim, pstrTrimDirection)
   -- pstrCharToTrim : A list of characters to trim, or true to use default
   -- pstrSourceText : The text to be trimmed
   -- pstrTrimDirection : Direction of Trim left, right or any value for full
   if pstrCharToTrim = missing value or pstrCharToTrim = true then
       set setToTrim to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
   else
       set setToTrim to current application's NSCharacterSet's characterSetWithCharactersInString:pstrCharToTrim
   end if

   set anNSString to current application's NSString's stringWithString:pstrSourceText
   if pstrTrimDirection = left then
       set theRange to anNSString's rangeOfCharacterFromSet:(setToTrim's invertedSet())
       if |length| of theRange = 0 then return ""
       set anNSString to anNSString's substringFromIndex:(theRange's location)
   else if pstrTrimDirection = right then
       set theRange to anNSString's rangeOfCharacterFromSet:(setToTrim's invertedSet()) options:(current application's NSBackwardsSearch)
       if |length| of theRange = 0 then return ""
       set anNSString to anNSString's substringToIndex:(theRange's location)
   else
       set anNSString to anNSString's stringByTrimmingCharactersInSet:setToTrim
   end if
   return anNSString as text
end trimThis

Considerably slower on the sample string, but gets more competitive with longer strings.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2015-08-10 08:59:22 am

JMichaelTX
Member
From:: Houston, TX (The Woodlands)
Registered: 2014-07-12
Posts: 142

Re: Trim [Remove Spaces]

DJ Bazzie Wazzie wrote:

Thanks for the update Michael but when bringing old posts back to live you should consider changes of AppleScript through the years as well. Improvements over Michaels scripts are:

- Since AppleScript 2.0 the script should use (character) id instead of ASCII character command.
- AppleScript 2.0 is Unicode, by default all Unicode spaces are supported now, not only limited to MacRoman whitespaces
- BugFix: The handler will return "" if the string contains only whitespaces
- BugFix: The handler will return "" if the string is empty
- Optimization: The string will only be trimmed once, not twice (text n thru n ...)
- Use justification constants left and right to indicate direction, missing value (any value) a full trim will be applied.


@DJ:  Thanks for the backhanded compliment.  sad

When providing what you think is an improved version of someone else's script, it is not necessary to be critical if nothing wrong was done.  Whether or not your version is better will be left up to the reader.

I see nothing wrong with using the ASCII character command.  It works and is more obvious than the character ID approach.
I also think it is more obvious to use a value of "full" rather than missing value for parameter pstrTrimDirection.  There's no reason not to allow for both.

Your "optimization" of trimming once instead of twice is a very minor optimization.  But it good.

Thanks for sharing your version.  It is good the thread is alive again.  cool


2019 iMac-27 512GB SSD, 3.6Ghz i9 CPU, 40GB RAM
macOS 10.14.6 (Mojave)

Offline

 

#6 2015-08-10 10:20:01 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Trim [Remove Spaces]

JMichaelTX wrote:

@DJ:  Thanks for the backhanded compliment.  sad


That was no my intention and didn't meant to.

JMichaelTX wrote:

I see nothing wrong with using the ASCII character command.


Me neither but I prefer to use Apple's guidelines and the AppleScript Language Guide who clearly says no longer to use the ASCII number and ASCII character commands since AppleScript 2.0.

JMichaelTX wrote:

Whether or not your version is better will be left up to the reader.


With that analogy Bruce's version can still be the best, depending on the mind that's reading it wink

I still consider removing the possible errors and making it AppleScript 2.0 compatible as an improvement which is more than a preference. The improvement is not in performance but in reliability and support. The "right" "left" "full" was to make it cleaner (personal preference to make the if statement faster/cleaner inside the routine) and not to use true but missing value is based on the global gentlemen agreement between programmers. The latter is technically not wrong, but it's uncommon and may lead to confusing.

Offline

 

#7 2015-08-10 10:50:38 am

JMichaelTX
Member
From:: Houston, TX (The Woodlands)
Registered: 2014-07-12
Posts: 142

Re: Trim [Remove Spaces]

DJ Bazzie Wazzie wrote:
JMichaelTX wrote:

I see nothing wrong with using the ASCII character command.


Me neither but I prefer to use Apple's guidelines and the AppleScript Language Guide who clearly says no longer to use the ASCII number and ASCII character commands since AppleScript 2.0.


OK, I did not realize the ASCII commands had been depreciated.

From the Apple Guidelines:

id
    Access:    read-only
    Class:    integer or list of integer
    A value (or list of values) representing the Unicode code point (or code points) for the character (or characters) in the text object. (A Unicode code point is a unique number that represents a character and allows it to be represented in an abstract way, independent of how it is rendered. A character in a text object may be composed of one or more code points.)
    This property, added in AppleScript 2.0, can also be used as an address, which allows mapping between Unicode code point values and the characters at those code points. For example, id of "A" returns 65, and character id 65 returns "A".
    The id of text longer than one code point is a list of integers, and vice versa: for example, id of "hello" returns {104, 101, 108, 108, 111}, and string id {104, 101, 108, 108, 111} returns "hello". (Because of a bug, text id ... does not work; you must use one of string, Unicode text, or character.)
    These uses of the id property obsolete the older ASCII character and ASCII number commands, since, unlike those, they cover the full Unicode character range and will return the same results regardless of the user's language preferences.


Maybe even better is to use the constants:

AS-WhiteSpace-Constants.gif


2019 iMac-27 512GB SSD, 3.6Ghz i9 CPU, 40GB RAM
macOS 10.14.6 (Mojave)

Offline

 

#8 2015-08-10 08:02:51 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6729

Re: Trim [Remove Spaces]

JMichaelTX wrote:

I see nothing wrong with using the ASCII character command.  It works


AS you've found, it's deprecated. And yes, it does work -- but only for true ASCII characters -- that is, for numbers 0 to 127. Above that, it's unreliable.

But there's another reason to prefer the id approach: it's much faster. The difference is insignificant in this example because you're only calling it once, but it's worth keeping in mind. Using ASCII character involves sending an Apple event, where using id does not. For example:

Applescript:

set theStart to current date
repeat 3000 times
   repeat with i from 0 to 127
       ASCII character i
   end repeat
end repeat
return (current date) - theStart

And this:

Applescript:

set theStart to current date
repeat 3000 times
   repeat with i from 0 to 127
       string id i
   end repeat
end repeat
return (current date) - theStart

Using a constant like linefeed is quicker still, but only by the narrowest of margins.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2015-08-10 09:36:49 pm

JMichaelTX
Member
From:: Houston, TX (The Woodlands)
Registered: 2014-07-12
Posts: 142

Re: Trim [Remove Spaces]

Shane Stanley wrote:
JMichaelTX wrote:

I see nothing wrong with using the ASCII character command.  It works


AS you've found, it's deprecated. And yes, it does work -- but only for true ASCII characters -- that is, for numbers 0 to 127. Above that, it's unreliable.


You're beating a dead horse.  wink

I noted it was depreciated, and even quoted the Apple Guidelines.
I was already in agreement to not use the ASCII commands.

JMichaelTX wrote:

OK, I did not realize the ASCII commands had been depreciated.

From the Apple Guidelines:
. . .


2019 iMac-27 512GB SSD, 3.6Ghz i9 CPU, 40GB RAM
macOS 10.14.6 (Mojave)

Offline

 

#10 2021-09-01 07:03:39 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Trim [Remove Spaces]

I had occasion to use Shane's script from post 4--its 6 years old but still does the job. Anyways, I happened to notice an issue when using the script to trim whitespace characters from the right side of a string, and the issue is that the last character of the string is truncated. Thus, if the returned result should be  "  Hello World!" it is instead  "  Hello World".

I don't fully understand the code in that immediate section of the script but changing the first line below to the second line below seems to fix the issue.

Applescript:

set anNSString to anNSString's substringToIndex:(theRange's location)
set anNSString to anNSString's substringToIndex:((theRange's location) + 1)

Last edited by peavine (2021-09-01 07:15:49 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#11 2021-09-02 01:07:50 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5438

Re: Trim [Remove Spaces]

peavine wrote:

I don't fully understand the code in that immediate section of the script


Hi peavine.

theRange is set to the range of the last character in the string which isn't in the set of characters to be trimmed. The code's then meant to return the text from the beginning of the string up to and including that character. But in fact substringToIndex: "Returns a new string containing the characters of the receiver up to, but not including, the one at a given index." So to include that last character, the given index has to be the character's location plus its length:

Applescript:

set anNSString to anNSString's substringToIndex:((theRange's location) + (theRange's |length|))


NG

Online

 

#12 2021-09-02 07:03:32 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Trim [Remove Spaces]

Thanks Nigel. That works great.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#13 2021-09-02 09:53:56 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Trim [Remove Spaces]

Shane's script includes the ability to remove characters from the beginning, the end, or both the beginning and end of a string. I seldom need to do all of these in one script and, for economy of code, decided to divide Shane's script into three handlers, which I've included below. I've also included a fourth handler which works on a string with multiple paragraphs.

The operation of these scripts requires no explanation, except to note that the handlers' second parameter can either be specific characters or missing value, and, in the latter case, the character set whitespaceAndNewlineCharacterSet is used. So, when calling one of the handlers, all of the following are valid:

trimLeadingCharacters(theString, "-" & " ")
trimTrailingCharacters(theString, space & tab)
trimLeadingAndTrailingCharacters(theString, missing value)

I ran some timing tests with these handlers with single-paragraph strings and they generally took about 3 milliseconds to run--this assumes that the foundation framework is already in memory. I tested the fourth handler with a string that contained 301 paragraphs, and the timing result was 265 milliseconds.

TRIM LEADING CHARACTERS

Applescript:

on trimLeadingCharacters(theString, theCharacters)
   if theCharacters = missing value then
       set theCharacters to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
   else
       set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
   end if
   
   set theString to current application's NSString's stringWithString:theString
   set theRange to theString's rangeOfCharacterFromSet:(theCharacters's invertedSet())
   if |length| of theRange = 0 then return ""
   return (theString's substringFromIndex:(theRange's location)) as text
end trimLeadingCharacters

TRIM TRAILING CHARACTERS

Applescript:

on trimTrailingCharacters(theString, theCharacters)
   if theCharacters = missing value then
       set theCharacters to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
   else
       set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
   end if
   
   set theString to current application's NSString's stringWithString:theString
   set theRange to theString's rangeOfCharacterFromSet:(theCharacters's invertedSet()) options:(current application's NSBackwardsSearch)
   if |length| of theRange = 0 then return ""
   return (theString's substringToIndex:((theRange's location) + (theRange's |length|))) as text
end trimTrailingCharacters

TRIM LEADING AND TRAILING CHARACTERS

Applescript:

on trimLeadingAndTrailingCharacters(theString, theCharacters)
   if theCharacters = missing value then
       set theCharacters to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
   else
       set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
   end if

   set theString to current application's NSString's stringWithString:theString
   set theString to theString's stringByTrimmingCharactersInSet:theCharacters
   return theString as text
end trimLeadingAndTrailingCharacters

TRIM LEADING AND TRAILING CHARACTERS - STRING HAS MULTIPLE PARAGRAPHS

Applescript:

on trimLeadingAndTrailingCharacters(theString, theCharacters)
   if theCharacters = missing value then
       set theCharacters to current application's NSCharacterSet's whitespaceCharacterSet()
   else
       set theCharacters to current application's NSCharacterSet's characterSetWithCharactersInString:theCharacters
   end if
   
   set theStrings to (current application's NSString's stringWithString:theString)'s componentsSeparatedByString:linefeed
   set cleanedStrings to current application's NSMutableArray's new()
   repeat with aString in theStrings
       set aString to (aString's stringByTrimmingCharactersInSet:theCharacters)
       (cleanedStrings's addObject:aString)
   end repeat
   return (cleanedStrings's componentsJoinedByString:linefeed) as text
end trimLeadingAndTrailingCharacters

Last edited by peavine (2021-09-04 07:12:37 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#14 2021-09-05 10:13:12 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Trim [Remove Spaces]

Just for the sake of thoroughness, I decided to write a script that worked the same as script 4 above except with basic AppleScript. I ran timing tests with a string that contained 3001 paragraphs with spaces and tabs at the beginning and end of each paragraph. The results were:

Script 4 above - 2.540 seconds
The script below without a script object - 0.312
The script below - 0.047

Applescript:

set theCleanedText to trimText(theText, {space, tab})

on trimText(theText, theCharacters)
   script o
       property untrimmedParagraphs : (paragraphs of theText)
       property trimmedParagraphs : {}
   end script
   
   repeat with aParagraph in o's untrimmedParagraphs
       set aParagraph to contents of aParagraph
       try
           repeat while text 1 of aParagraph is in theCharacters
               set aParagraph to text 2 thru -1 of aParagraph
           end repeat
           repeat while text -1 of aParagraph is in theCharacters
               set aParagraph to text 1 thru -2 of aParagraph
           end repeat
       on error
           set aParagraph to ""
       end try
       set the end of o's trimmedParagraphs to aParagraph
   end repeat
   
   set {TID, text item delimiters} to {text item delimiters, linefeed}
   set o's trimmedParagraphs to (o's trimmedParagraphs as text)
   set text item delimiters to TID
   return o's trimmedParagraphs
end trimText

Last edited by peavine (2021-09-05 06:35:03 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#15 2021-09-06 04:14:10 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Trim [Remove Spaces]

KniazidisR. Thanks for looking at my scripts and for your suggestion.

Our scripts appear to perform somewhat different tasks. My script removes the specified characters (whitespace as currently written) from the beginning and end of every paragraph in the text. Thus, in my timing tests, the script trims 3001 paragraphs. Your script removes the specified characters from the beginning and end of the entire text rather than the individual paragraphs. If that is correct, it doesn't appear the timing-test results of our scripts should be compared. Am I confused as to what your script does?

Last edited by peavine (2021-09-08 06:36:21 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#16 2021-09-06 11:32:23 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Trim [Remove Spaces]

No, you're right, I got it mixed up. This means that my script is useless, because when trimming per 1024 paragraphs, it is 50 times slower than yours. Therefore, I am deleting my post. And only your version goes to my collection of scripts.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#17 2021-09-21 09:38:03 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Trim [Remove Spaces]

Anyone reading this thread may have need of a script that trims text in a file or on the clipboard, and I've included below scripts that perform these tasks. Their operation requires no real explanation, except to note that the file-trimming script creates a new file, which has the same name as the original but with the word "trimmed" appended. Also, the scripts trim both leading and trailing characters, although this can be changed by disabling the applicable repeat loop.

This script trims text in a file:

Applescript:

-- Revised 2021.10.05

use framework "Foundation"
use scripting additions

on main()
   set theCharacters to space & tab -- add characters if desired but precede with ampersand
   set characterEncoding to current application's NSUTF8StringEncoding
   
   set theFile to POSIX path of (choose file)
   set theTrimmedFile to getFileName(theFile)
   
   set theText to ((current application's NSString's stringWithContentsOfFile:theFile encoding:characterEncoding |error|:(missing value)) as text)
   if theText = "missing value" then display alert "An error has occurred" message "The selected file may not be a text file" buttons {"OK"} cancel button 1 as critical
   set theText to trimText(theText, theCharacters)
   (current application's NSString's stringWithString:theText)'s writeToFile:theTrimmedFile atomically:true encoding:characterEncoding |error|:(missing value)
end main

on trimText(theText, theCharacters)
   script o
       property untrimmedParagraphs : (paragraphs of theText)
       property trimmedParagraphs : {}
   end script
   
   repeat with aParagraph in o's untrimmedParagraphs
       set aParagraph to contents of aParagraph
       try
           repeat while text 1 of aParagraph is in theCharacters -- remove leading characters
               set aParagraph to text 2 thru -1 of aParagraph
           end repeat
           repeat while text -1 of aParagraph is in theCharacters -- remove trailing characters
               set aParagraph to text 1 thru -2 of aParagraph
           end repeat
       on error
           set aParagraph to ""
       end try
       set the end of o's trimmedParagraphs to aParagraph
   end repeat
   
   set text item delimiters to {linefeed}
   set o's trimmedParagraphs to (o's trimmedParagraphs as text)
   set text item delimiters to {""}
   return o's trimmedParagraphs
end trimText

on getFileName(theFile)
   set theFile to current application's NSString's stringWithString:theFile
   set fileBase to theFile's stringByDeletingPathExtension()
   set fileExtension to theFile's pathExtension()
   return ((fileBase's stringByAppendingString:" (trimmed)")'s stringByAppendingPathExtension:fileExtension)
end getFileName

main()

This script trims the content of the clipboard:

Applescript:

-- Revised 2021.10.05

on main()
   set theCharacters to space & tab -- add characters if desired but precede with ampersand
   try
       set theText to the clipboard as text
   on error
       display alert "An error has ocurred" message "Text was not found on the clipboard" as critical
       error number -128
   end try
   set the clipboard to trimText(theText, theCharacters)
end main

on trimText(theText, theCharacters)
   script o
       property untrimmedParagraphs : (paragraphs of theText)
       property trimmedParagraphs : {}
   end script
   
   repeat with aParagraph in o's untrimmedParagraphs
       set aParagraph to contents of aParagraph
       try
           repeat while text 1 of aParagraph is in theCharacters -- remove leading characters
               set aParagraph to text 2 thru -1 of aParagraph
           end repeat
           repeat while text -1 of aParagraph is in theCharacters -- remove trailing characters
               set aParagraph to text 1 thru -2 of aParagraph
           end repeat
       on error
           set aParagraph to ""
       end try
       set the end of o's trimmedParagraphs to aParagraph
   end repeat
   
   set text item delimiters to {linefeed}
   set o's trimmedParagraphs to (o's trimmedParagraphs as text)
   set text item delimiters to {""}
   return o's trimmedParagraphs
end trimText

main()

Last edited by peavine (2021-10-05 07:20:54 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#18 2021-11-19 09:03:24 pm

technomorph
Member
Registered: 2017-12-14
Posts: 218

Re: Trim [Remove Spaces]

Also using RegEx you can find those pesky double/triple spaces and remove them with this.
Then use NSCharacterSet white...
(much of this has been split out into separate handlers that I use for other purposes)

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property NSUTF32LittleEndianStringEncoding : a reference to 2.617245952E+9
property NSMutableCharacterSet : a reference to current application's NSMutableCharacterSet
property NSCharacterSet : a reference to current application's NSCharacterSet

property NSNotFound : a reference to 9.22337203685477E+18 + 5807

property NSRegularExpression : a reference to current application's NSRegularExpression
property NSRegularExpressionCaseInsensitive : a reference to 1
property NSRegularExpressionUseUnicodeWordBoundaries : a reference to 40
property NSRegularExpressionAnchorsMatchLines : a reference to 16
property NSRegularExpressionSearch : a reference to 1024
property NSString : a reference to current application's NSString

on cleanAllWhiteSpaceInString:aString
   set aPattern to (NSString's stringWithString:"(\\s+){2}")
   set aDirtyString to (my findInString:aString withPattern:aPattern replaceWith:" ")
   set aCleanString to (my cleanWhiteSpaces:aDirtyString)
   return aCleanString
end cleanAllWhiteSpaceInString:

on cleanWhiteSpaces:aString
   set aCharSet to NSCharacterSet's whitespaceCharacterSet()
   return (my cleanString:aString withCharacterSet:aCharSet)
end cleanWhiteSpaces:

on cleanString:aString withCharacterSet:aCharSet
   set aDirtyString to NSString's stringWithString:aString
   set aCleanString to (aDirtyString's stringByTrimmingCharactersInSet:aCharSet)
   return aCleanString
end cleanString:withCharacterSet:

on findInString:aString withPattern:aRegExString replaceWith:aReplace
   set aRegEx to my createRegularExpressionWithPattern:aRegExString
   return (my findInString:aString withRegEx:aRegEx replaceWith:aReplace)
end findInString:withPattern:replaceWith:

on findInString:aString withRegEx:aRegEx replaceWith:aReplace
   set aSource to NSString's stringWithString:aString
   set aRepString to NSString's stringWithString:aReplace
   set aLength to aSource's |length|()
   set aRange to (current application's NSMakeRange(0, aLength))
   set matches to (aRegEx's matchesInString:aSource options:0 range:aRange)
   set aCount to matches's |count|()
   if (aCount > 0) then
       set aCleanString to (aRegEx's stringByReplacingMatchesInString:aSource options:0 range:aRange withTemplate:aRepString)
   else
       set aCleanString to aSource
   end if
   return aCleanString
end findInString:withRegEx:replaceWith:

on createRegularExpressionWithPattern:aRegExString
   if (class of aRegExString) is equal to (NSRegularExpression's class) then
       --log ("it alreadry was a RegEx")
       return aRegExString
   end if
   set aPattern to NSString's stringWithString:aRegExString
   set regOptions to NSRegularExpressionCaseInsensitive + NSRegularExpressionUseUnicodeWordBoundaries
   set {aRegEx, aError} to (NSRegularExpression's regularExpressionWithPattern:aPattern options:regOptions |error|:(reference))
   if (aError ≠ missing value) then
       log {"regEx failed to create aError is:", aError}
       log {"aError debugDescrip is:", aError's debugDescription()}
       break
       return
   end if
   return aRegEx
end createRegularExpressionWithPattern:

Last edited by technomorph (2021-11-19 09:05:33 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)