Text cleaner- removes returns, formatting & repeat space

Hi Everyone

The below script removes returns, formatting & repeat spaces. It’s put together from bits I found on this site and macosxhints. It seems to work as I want it to, but I’m not a programmer and this is one of my first applescripts - could any experts tell me if there’s any ‘bad practice’ in here? Also I want to offer this as an app on my website. If I save it as a “stay open” app, it always says “this application is not responding” if I click and hold on the dock. Is this bad?

thanks in advance!


on run
	set the message_text to the clipboard
	set the message_text to replace_chars(message_text, return, space)
	repeat while (space & space) is in the message_text
		set the message_text to replace_chars(message_text, (space & space), space)
	end repeat
	set the message_text to (the message_text as string)
	set the clipboard to the message_text
	do shell script "pbpaste | pbcopy"
end run

on reopen
	set the message_text to the clipboard
	set the message_text to replace_chars(message_text, return, space)
	repeat while (space & space) is in the message_text
		set the message_text to replace_chars(message_text, (space & space), space)
	end repeat
	set the message_text to (the message_text as string)
	set the clipboard to the message_text
	do shell script "pbpaste | pbcopy"
end reopen


on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Not exactly “bad practice” as such, but since your ‘run’ and ‘reopen’ handlers are identical, you could reduce the amount of code in the script by transfering the working parts to a third handler, which the first two could then call. This would have the additional advantage that any adjustments to the code would only have to be made in one handler rather than two. (Unless of course you then wanted the ‘run’ and ‘reopen’ handlers to be different!)

In the repeat loop, the concatenation ‘space & space’ is done every time round the loop, and again each time in the parameters for ‘replace_chars()’. It doesn’t matter much in this particular script, but “better practice” would be to do the concatenation just once, store the result in a variable, and then use the variable in the repeat.

on run
   edit_clipboard_text()
end run

on reopen
   edit_clipboard_text()
end reopen

on edit_clipboard_text()
   set the message_text to the clipboard
   set the message_text to replace_chars(message_text, return, space)
   set double_space to space & space
   repeat while double_space is in the message_text
      set the message_text to replace_chars(message_text, double_space, space)
   end repeat
   -- replace_chars() returns a string, so the following line isn't necessary.
   -- set the message_text to (the message_text as string)
   set the clipboard to the message_text
   do shell script "pbpaste | pbcopy"
end editText

There are two schools of thought about what’s good practice with regard to text item delimiters. Some argue that TIDs should always be set to “” or {“”} after they’re used (which is what you’ve done). Others insist that the current value — whatever that happens to be — should be stored first and then restored afterwards. Whichever school you join, don’t casually mix the two in the same script.

on replace_chars(this_text, search_string, replacement_string)
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to the search_string
   set the item_list to every text item of this_text
   set AppleScript's text item delimiters to the replacement_string
   set this_text to the item_list as string
   set AppleScript's text item delimiters to astid
   return this_text
end replace_chars