Paste and select in Drafts

Here’s a simpler Drafts action than yesterday’s, but one that also mimics a BBEdit command. It’s Paste and Select, which is bound to ⌘⌥V. It does exactly what its name implies: pastes what’s on the clipboard into the current draft and then immediately selects it.

When would you need this? Typically it’s when the text you’re pasting needs to be reformatted in the document you’re pasting it into. For example, when I paste the source code of a script into my blog posts, I usually add line numbers and indent it right after pasting so the Markdown processor converts it to code instead of prose. It saves what can be a time-consuming and error-prone step if I don’t have to scroll around to select all the text I just pasted in.

The Paste and Select command is one of the many little affordances that make me love BBEdit. I wanted it in Drafts, too. It’s possible there’s already a Paste and Select action in the Drafts action directory, but writing it is easier than searching. It’s only a few lines of JavaScript:

javascript:
1:  // Paste the clipboard as usual and select what was pasted
2:  // for further processing.
3:  
4:  var [start, len] = editor.getSelectedRange();
5:  var clipText = app.getClipboard();
6:  editor.setTextInRange(start, len, clipText);
7:  editor.setSelectedRange(start, clipText.length);
8:  editor.activate();

As with regular pasting, if there’s an active selection, the clipboard replaces it. If there’s no active selection, the clipboard is inserted at the cursor.1

I wrote a somewhat different version of this script a while ago. It worked fine, but after writing the Prefix/Suffix script and rewriting my line numbering script, I thought it would be best if I rewrote this one, too, to make the variable naming more consistent across my scripts. Cleaning up old scripts is one of those rainy day projects that the current situation has given me time to do.