Eliminating leading and trailing white space from clipboard text

I’m updating an oldish script and found a handler I’ve been using, possibly since before MacOSX.

I know there are nifty ways of stripping whitespace from the beginning and endings of paragraphs, but this works, and is plenty fast for my purposes.


property lastSearch : ""
set allGraphs to true
set textFromClipboard to GetClipboard(allGraphs)
set allGraphs to false
set textFromClipboard to GetClipboard(allGraphs)

on GetClipboard(allGraphs)
	try
		set clipBoardString to the clipboard as text
	on error
		set clipBoardString to lastSearch
		return {clipBoardString, lastSearch}
	end try
	if allGraphs then
		set clipBoardString to paragraphs of (clipBoardString)
		repeat with x from 1 to count of clipBoardString
			set item x of clipBoardString to ClearLeadingTrailingWhiteSpace(item x of clipBoardString)
		end repeat
		if the (count of clipBoardString) > 1 then
			set displayString to item 1 of clipBoardString & "..."
		else
			set displayString to item 1 of clipBoardString
		end if
	else
		
		set clipBoardString to ClearLeadingTrailingWhiteSpace(paragraph 1 of clipBoardString)
		return {clipBoardString, clipBoardString}
	end if
	set saveTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {return}
	set clipBoardString to clipBoardString as text
	set AppleScript's text item delimiters to saveTID
	
	return {clipBoardString, displayString}
end GetClipboard

on ClearLeadingTrailingWhiteSpace(aString)
	set saveTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {""}
	set charsToClear to {return, linefeed, tab, space}
	set aString to text items of (aString as text) as list
	repeat 2 times
		set aString to the reverse of aString
		repeat with x from 1 to count of aString
			set thisChar to item x of aString
			if thisChar is not in charsToClear then
				set aString to items x thru -1 of aString
				exit repeat
			end if
		end repeat
	end repeat
	set aString to aString as text
	set AppleScript's text item delimiters to saveTID
	return aString
end ClearLeadingTrailingWhiteSpace


1 Like

I’m updating an oldish script and found a handler I’ve been using, possibly since before MacOSX.

I know there are nifty ways of stripping whitespace from the beginning and endings of paragraphs, but this works, and is plenty fast for my purposes.


property lastSearch : ""
set allGraphs to true
set textFromClipboard to GetClipboard(allGraphs)
set allGraphs to false
set textFromClipboard to GetClipboard(allGraphs)

on GetClipboard(allGraphs)
	try
		set clipBoardString to the clipboard as text
	on error
		set clipBoardString to lastSearch
		return {clipBoardString, lastSearch}
	end try
	if allGraphs then
		set clipBoardString to paragraphs of (clipBoardString)
		repeat with x from 1 to count of clipBoardString
			set item x of clipBoardString to ClearLeadingTrailingWhiteSpace(item x of clipBoardString)
		end repeat
		if the (count of clipBoardString) > 1 then
			set displayString to item 1 of clipBoardString & "..."
		else
			set displayString to item 1 of clipBoardString
		end if
	else
		
		set clipBoardString to ClearLeadingTrailingWhiteSpace(paragraph 1 of clipBoardString)
		return {clipBoardString, clipBoardString}
	end if
	set saveTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {return}
	set clipBoardString to clipBoardString as text
	set AppleScript's text item delimiters to saveTID
	
	return {clipBoardString, displayString}
end GetClipboard

on ClearLeadingTrailingWhiteSpace(aString)
	set saveTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {""}
	set charsToClear to {return, linefeed, tab, space}
	set aString to text items of (aString as text) as list
	repeat 2 times
		set aString to the reverse of aString
		repeat with x from 1 to count of aString
			set thisChar to item x of aString
			if thisChar is not in charsToClear then
				set aString to items x thru -1 of aString
				exit repeat
			end if
		end repeat
	end repeat
	set aString to aString as text
	set AppleScript's text item delimiters to saveTID
	return aString
end ClearLeadingTrailingWhiteSpace