TextExpander: Smarten/Straighten Quotes

I’ve been a long time user of TextExpander, though I haven’t tapped into its full potential until recently.

I often find myself converting between "straight" quotes and “curly” quotes. Some applications can perform this conversion internally, while others use plug-ins. Mac OS X 10.9 (Mavericks) can even “smarten” your quotes and dashes automatically. (System Preferences > Keyboard > Text > Use smart quotes and dashes.) I do a fair amount of text editing where “smart” quotes aren’t desired. TextExpander to the rescue!

Here are two snippets that will transform your clipboard contents into the desired quote format. (Be sure to change the content type to: Shell Script.)

Smarten Quotes:

#!/usr/bin/env php
$str = `pbpaste`;
$str = preg_replace('/(^|[-\xe2\x80\x94\/(\[{"\s])\'/', "$1\xe2\x80\x98", $str);
$str = preg_replace('/\'/', "\xe2\x80\x99", $str);
$str = preg_replace('/(^|[-\xe2\x80\x94\/(\[{\xe2\x80\x98\s])"/', "$1\xe2\x80\x9c", $str);
$str = preg_replace('/"/', "\xe2\x80\x9d", $str);
$str = preg_replace('/--/', "\xe2\x80\x94", $str);
$str = preg_replace('/\.\.\./', "\xe2\x80\xa6", $str);
echo $str;

Straighten Quotes:

#!/usr/bin/env php
$str = `pbpaste`;
$find = array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6");
$replace = array("'", "'", '"', '"', '-', '--', '...');
echo str_replace($find, $replace, $str);


Add your thoughts...




Image Verification: