How to Trim Whitespace in Highlight summary RTF

I have a small script from fellow DTP user “cgrunenberg” to strip out the URL/Undelined Links text, but I’m looking to clearup the whitespace - i.e. multiple Tabs, multiple Carriage returns and multiple spaces.

I’ve got code;

use framework "Foundation"
use scripting additions

set theString to "    line one 
line two    "

set trimmedText to getTrimmedText(theString)

on getTrimmedText(theString)
   set thePattern to "(?m)^[ \\t]+|[ \\t]+$"
   set theString to current application's NSString's stringWithString:theString
   set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:"" options:(current application's NSRegularExpressionSearch) range:{0, theString's |length|()})
   return theString as text
end getTrimmedText

However, this is beyond my very limited skillset. I’m unsure how to integrate it with my other AppleScript which is;

tell application id "DNtp"
	set theRecord to selected record 1
	tell text of theRecord
		set i to 1
		set cnt to number of attribute runs
		repeat while i ≤ cnt
			if exists URL of attribute run i then
				set text of attribute run i to ""
				set cnt to cnt - 1
			else
				set i to i + 1
			end if
			set theRecord to item 1 of input as text
			set AppleScript's text item delimiters to {space, tab, linefeed, return}
			set theRecord to text items of theRecord
			set AppleScript's text item delimiters to {}
			set theRecord to theRecord as string
		end repeat
	end tell
	
end tell

I have found that by using the following Regex code( ^[ \t\r\n ]+|[ \t\r\n ] +$) I can get the desired results.

Any direction and or assistance is very appreciated.

My usual 0,02€: JavaScript has a trim method for strings. Makes things a lot less clumsy then this AS code. But of course, one can solve all problems in any language.

That looks like a complicated version of ^\s+|\s+$, but I may be missing something. Also, i don’t quite understand why you’d want to use a space character twice in a character class. To put it differently:
[ \n\r\t] matches the exact same characters as [ \n\r\t ]


  1. ** \t\r\n ** ↩︎