In AppleScript, what is the easiest way to remove all punctuation from a string?

I have an Automator Service that is comprised of a single "Run AppleScript" action. The Service receives selected text in any application.

The Service incorporates this selected text into a pre-determined URL format. The Service then opens the newly-assembled URL in a new tab in a web browser window.

Here is a string that the Service might receive:

My Plant's Dead! (But, Who Cares? I Have 12 Plants.)

I want to use AppleScript to remove every and any punctuation symbol that exists in the string, including spaces. I also need the string to be converted to all lowercase characters. Numbers should not be removed from the string.

So, the desired string is:

myplantsdeadbutwhocaresihave12plants

I understand that I can accomplish the desired effect by writing an individual text item delimiters statement for every possible punctuation symbol and for 26 uppercase letters. I could use a replace_characters sub-routine to make this method a bit neater.

Is there an easier or shorter way to accomplish this effect in AppleScript, as opposed to using dozens of text item delimiters blocks? Such as, one line of code that reduces a string to letters?

I know that other programming languages have a "strip punctuation" shortcut, which is why I ask.

This is, essentially, an AppleScript-specific "Code Golf" question.


Try the following:

set stringToConvert to "My Plant's Dead! (But, Who Cares? I Have 12 Plants.)"
set returnedString to do shell script "echo " & quoted form of stringToConvert & " | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]'"

It returns: myplantsdeadbutwhocaresihave12plants

tr commands explained:

  • tr -dc '[:alnum:]' deletes all non alphanumeric characters (including white space).
  • tr '[:upper:]' '[:lower:]' converts all upper-case characters to lower-case characters.

See the man page for tr.