5

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.

3

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.

  • Thanks for bringing the numbers point to my attention; I had completely forgotten about this. – rubik's sphere Apr 13 '17 at 4:14
  • @rubik's sphere, Auto-correct changed alnum to album, so it is correct now. – user3439894 Apr 13 '17 at 5:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .