How to replace special characters (curly quotes, dashes, ellipses) in OS X?

3

Pretty simple: I need a way to convert special characters like curly quotes, ellipses, etc to their "plain text" version, i.e. … to ..., “ to ".

This is on OS X, meaning I can't use a macro in Microsoft Word to do this as I have on a PC.

This is for a plain-text email, not HTML, which is why I need them in this format.

user36452

Posted 2010-05-07T16:31:03.680

Reputation:

Answers

1

I see you want to do this in Mail. The best way to do this (imho) is with a Service Menu item. Word Service has an option to straighten/curly quotes.

If you're using Snow Leopard, it's pretty straightforward to create your own service menu item with a python/ruby/applescript to replace arbitrary special characters.

EDIT: Yeah I glossed over how to do this by saying it's "pretty straightforward" a bit disingenuously. Here's how to do it with a ruby script. ;-)

  1. Open Automator
  2. Create new Workflow, choose "Service" as your template
  3. Choose Service receives selected text in any application with the top popups
  4. Click the checkbox underneath: Replaces selected text
  5. Drag Run Shell Script from the left pane into your workflow
  6. With the Shell pupup select /usr/bin/ruby/
  7. Copy and paste in the following code, modify to your needs
  8. Save! Find it in the Services menu of your favourite application

    $KCODE = 'u'
    require 'jcode'
    # need the above two lines for ruby <1.9.1 
    # to make str.tr and str.gsub unicode aware
    
    # grab the selected text!
    theText = STDIN.gets(nil)
    
    # str.tr replaces single characters
    theText = theText.tr('“”','"')
    theText = theText.tr("‘’","'")
    
    # need to use str.gsub: replacement is > replaced characters
    theText = theText.gsub("…","...")
    theText = theText.gsub("—","--")
    
    # add more replacements as desired . . .
    
    STDOUT << theText  # replace da text!
    

ghoppe

Posted 2010-05-07T16:31:03.680

Reputation: 6 124

1+1 for phasing the answer like you are an animated paperclip. – stib – 2010-05-08T12:54:42.290

□ Don't Show me this tip again – ghoppe – 2010-05-11T13:11:08.983

Looks good, but for some reason I get "No Services Apply" from the Services menu in Microsoft Word – None – 2010-05-19T20:27:16.000

Stupid MS Word isn't a Cocoa application but you can still code support for Services into Carbon applications. I hear Entourage supports services but Word does not. Sorry. I advise not using Word. :) You mentioned this was for a plain-text email why not just apply the service there? – ghoppe – 2010-05-19T21:53:51.510

Or could you use a "Get Selected Content from Word Documents" action instead, as @Doug Harris has done with his shell script? – ghoppe – 2010-05-19T21:57:04.233

Unfortunately I work at a publishing company and everything is done in Word.

Doug's solution does work well but I just added a comment explaining the issue I'm still having. However your and Doug's suggestions have already helped a lot! – None – 2010-05-20T15:49:58.467

0

The easy point-and-click way -- Bare Bones Software has a free text editor called "TextWrangler" that has a built-in "Convert to ASCII" command. You can even automate it: http://discussions.apple.com/thread.jspa?threadID=890344

The application also has regular expression search and replace, if you need it.

David Rouse

Posted 2010-05-07T16:31:03.680

Reputation: 279

0

http://www.mbayer.de/html2text/

might be what you want.

For OS X, there's a macports port for it, if you have macports, use

sudo port install html2text

David M

Posted 2010-05-07T16:31:03.680

Reputation: 203

html2text does not replace “curly” quotes with "straight" quotes. – mfink – 2017-08-18T15:18:02.447

0

I've done this with an Automator workflow that:

  1. Uses the "Get Selected Content from Word Documents" action and passes that output to...
  2. "Run Shell Script" to use sed -e 's/“/"/g; s/”/"/g; s/…/.../g; ' -e "s/’/'/g" and passes that output to...
  3. "Copy to Clipboard" action

Save this script under ~/Documents/Microsoft User Data/Word Script Menu Items/ and it will appear in Word's script menu.

Just select the text you want to send, run the script, and then you can paste the cleaned text into an email.

Doug Harris

Posted 2010-05-07T16:31:03.680

Reputation: 23 578

Thanks, this works perfectly except that it looses the bold / italic / etc formatting when done in Word. – None – 2010-05-19T20:28:05.560

I don't understand. I thought you don't want any formatting because you're sending plain text email. – Doug Harris – 2010-05-20T13:24:34.810

Ideally, I would like to keep any bold and italic formatting but remove the special characters as I could then paste this directly into a WYSIWYG editor, which I need to use as it keeps the bold/italic and also creates all the necessary line breaks. That would be for the HTML version. You are right that it wouldn't matter for the plain text version. – None – 2010-05-20T15:48:20.547