November 15th, 2014 • 11:48 am
Yesterday, Jason Snell’s Six Colors web site posted a tip about using OS X’s built-in keyboard customization features to create a universal shortcut for a command for pasting as plain text.
While the tip is not useless, it neglected to mention the fact that the menu command labelled “Nisus Writer Pro, the command is a subitem in the submenu “ ” in the “ ” menu, and it is called “Paste Text Only”.” is not nearly as universal as it should be. For instance, Adobe’s InDesign has a similar command, but it’s called “ ”. In
Secondly, even when in applications where the command exists and is called “”, it does not always work as expected.
For instance, for the particular purpose that the Six Colors article mentions (converting rich-text hyperlinks to plain-text URLs), the command only works if you initially used the “” command to copy the hyperlink in the first place. If you used the regular “ ” command, “ ” is unable to convert the link into its URL. In several applications, including Microsoft Word 2011 and OS X’s own TextEdit, when my Clipboard contains a rich-text hyperlink copied using the regular “ ” command, the “ ” command does not convert the hyperlink into a plain-text URL and inserts it. It simply inserts the rich-text hyperlink with the font face, size, etc. of the underlying paragraph where it is pasted.
Nisus Writer Pro’s “” strips the hyperlink and simply pastes the text of the hyperlink label (not the URL) as plain text.
In fact, I am not aware of a single OS X application where using the “” command (or its equivalent) when the Clipboard contains a rich-text hyperlink copied using the regular “ ” command manages to paste the URL of the hyperlink as plain text. The only time it works is when the hyperlink label (i.e. the text of the link, as opposed to its underlying URL) matches the URL itself.
And, as anyone who has ever received phishing spam knows, the text label of a hyperlink not always matches its underlying URL. It can actually even be a different URL!
So, outside of the situation where the hyperlink is originally copied using the “” command, I am not quite sure how useful Six Colors’s tip actually is.
It does highlight, however, the need for some kind of universal “” command that has a consistent behaviour across all OS X applications.
Here it is:
As you can see, it does not rely on any application’s built-in command for pasting without formatting. Instead, it uses a couple of Clipboard filters included in Keyboard Maestro, a few find/replace operations with regular expressions, and a text factory included in BBEdit.
The first step is based on Keyboard Maestro’s built-in “Remove Styles” Clipboard filter. This removes bold, italics, font size, font face, and a variety of other rich-text attributes.
Then the macro uses Keyboard Maestro’s built-in “Trim Styles” Clipboard filter. This is primarily because of Microsoft Word’s idiotic word-by-word selection behaviour, which automatically selects the trailing space after the last selected word (unless there is a punctuation sign). Since I use word-by-word selection all the time when copying text from within Word documents, I constantly end up with an undesirable trailing space at the end of my Clipboard text. The Keyboard Maestro filter takes care of that. (It also trims spaces at the beginning of the Clipboard text if there are any, and also tabs and other types of “whitespace”.)
Then the next two steps are designed to work around another problem in which Microsoft Word’s idiotic design plays a significant role. If the text you are copying happens to be inside a paragraph formatted with bullets or automatic numbering, and if your selection includes the very first word in the paragraph, then Word also includes the bullet or automatic number formatting with the copied text.
Of course, because Microsoft’s engineers are narrow-minded and like to pretend (subconsciously at least) that Word users only ever use Word and have no need for compatibility and user-friendliness across applications, when you paste the copied text that includes the bullet or automatic number formatting in the middle of another paragraph without bullets or automatic numbering or even in an empty paragraph in a Word document, Word does not include the bullet or automatic numbering with the pasted text.
And if you use Word’s own “” command with such a Clipboard (which includes both the text and bullet or automatic number formatting), Word does not include the bullet or automatic numbering with the pasted plain text.
If, however, you try and paste this particular Clipboard into another application as plain text (for example, in BBEdit, or in a search field on a web page), then the bullet or automatic number will be included in its plain text form (i.e. as actual bullet or number characters, plus whatever separates them from the following text) at the beginning of the pasted text. This is extremely irritating, and I have talked about it before.
The macro above takes care of this. The find/replace operations with regular expressions remove various kinds of bullets and automatic numbering (with actual numbers or letters), as well as the extra tab character (or period plus tab character, or closing parenthesis plus tab character) between the bullet/number and the beginning of the text. (I should note that I am far from being an expert at writing regular expressions, and these might not be the most efficient way to achieve this. But they seem to be working for me.)
I have another step in the macro for removing tab characters by themselves, which I find useful in situations where the previous steps didn’t remove all the tab characters. You might consider it optional or undesirable.
And finally I have a step that uses one of BBEdit’s built-in text factories to change straight apostrophes and quotation marks to curly ones. This is because text copied from web pages and other sources often contains straight quotes and apostrophes, and the contexts where I want to use the pasted plain text usually require curly ones.
I could have used Keyboard Maestro’s own Clipboard filter for smart quotes, but it does not deal with apostrophes at all. Peter Lewis once told me, “Keyboard Maestro does not attempt to figure out apostrophes because it is almost impossible to do right.” But that’s because he’s trying to deal with the use of single quotes, which is an English-specific problem. (We don’t use single quotes in French at all, so all our apostrophes are curled to the left.)
I find that BBEdit’s text factory handles the situation quite well for me, both in English and in French.
And that’s all, folks!
Because this is a Keyboard Maestro and it’s inside my “Global” group of macros that are enabled everywhere, in all applications, I can use it in all the applications that I work with, including the ones that have their own built-in “” or “ ” command. And I have now become completely dependent on it.
In fact, I even use this macro inside other Keyboard Maestro macros that I have for looking up stuff in various on-line databases. This way, I can just select whatever I want to look up in my current document, without having to worry about the cruft that might be included with the selection when it’s copied to the Clipboard, and then invoke the macro that I have to look up stuff in a particular database, which in turn uses my “Paste plain and trimmed” macro when it needs to paste the selection into the database’s search field.
Of course, my macro is not perfect. There are a few situations where it strips stuff that I didn’t really want it to strip. If I really cared, I could create another macro, with fewer steps, that only strips some of the stuff, and try and remember to use that macro in such cases. But I cannot really be bothered. I’d rather have one universal macro, with an easy one-hand shortcut, that works well most of the time, and then fix whatever needs to be fixed manually in those few cases when I need to do so.
My macro also does not address the issue raised by Six Colors, which is the extraction of the URL as plain text from a rich-text hyperlink. For that, I am afraid I don’t have an easy solution (unless the hyperlink was originally copied using the “Edit Hyperlink” modal dialog box. If I really needed to do this all the time, I would probably try to find an easier way to do it. And it would probably involve Keyboard Maestro again.” command, which is not universal). I tend to do it “manually” whenever I need to. Unfortunately, in Microsoft Word, that involves taking repeated trips to the “
But that might be for another post, when I finally get around to it.