Switching Single and Double Quotes

I am looking to make a clipboard filter that switches single for double quotes and double for single quotes, wraps the whole clipboard in double quotes, and then makes them all smart quotes. In an ideal world, this would preserve rich-text formatting.

I've tried replacing double quotes with @@@ and single quotes with @@, and then replacing those with single and double quotes respectively, but this clutters up the history list when I run the filter on a previous item in the list. (If I were always running it on the current clipboard, I could just delete past clipboards when done.) Plus, for some reason, I can't get the smart quote filter to work on the final result.

Any help would be appreciated!

You could isolate the System clipboard by using a Named clipboard. But I'd suggest copying the text from the clipboard to a local variable and working on it there. You can put the result back on the clipboard or paste it into the front document.

And I'd think you'd be better off coding this in Perl, say, because there are a lot of little gotchas in this kind of translation that can easily overwhelm you navigating a pile of Keyboard Maestro actions. You can get an idea of how this works in Literary Toolbox III and Text Toolbox I in the Smart/Straighten quote macros.