ezsnippets for Marked 2, text expansion on the fly

[Tweet : nvALT]

First, I’d like to thank everyone who’s responded to my “pledge drive.” It’s encouraging, and I appreciate all the new subscribers and the ideas that have been shared with me for adding value to the proposition!

In the spirit of productivity, here’s another preprocessor script for Marked 2 that you might find useful. It’s based on a great idea that Stephen Margheim developed for Alfred.

What it does is allow you to create “snippets” as you write a document, similar to TextExpander but limited just to the current document. You can create them on the fly as you write and then use them throughout the document. Obviously TextExpander is the better choice for phrases that you use regularly, but this solution works really well for words and phrases that you want to abbreviate but not necessarily pollute your TextExpander snippet collection with.

To set the preprocessor up in Marked 2:

  1. Download the script from this Gist and save it to a script folder on your drive as ezsnippets.rb.
  2. Make it executable with chmod a+x ezsnippets.rb
  3. Open up Marked 2 preferences (⌘,), and go to the “Advanced” tab in preferences1.
  4. In the “Custom Processor” box, choose the Preprocessor tab.
  5. Enter the path to the script in the path field

You can optionally enable “Automatically enable for new windows” to have the script run by default on all new files you open. If the file doesn’t contain any snippets, it won’t do anything, so it doesn’t hurt to just keep it on all the time. If you choose not to do this, you can enable the processor by clicking the yellow indicator in the bottom toolbar of a document, or by pressing ^⌘⌥C.

To use the new snippet functionality you just define snippets and abbreviations as you write. The format is {text to insert::shortcut}. When you write a word or phrase that you’d like to have an abbrevation for, just surround it in curly brackets, and at the end add a double colon followed by an abbreviation containing no spaces. The abbreviation can be anything you like, including a prefix character if you want to make absolutely sure you avoid accidental triggering. For example:

I occasionally write for {The Unofficial Apple Weblog::,tuaw}.

Now, in the rest of the document, I can use the abbreviation ,tuaw anywhere I want “The Unofficial Apple Weblog” inserted. Nifty, right?

The caveat to this is that your original Markdown file will now contain the abbreviations and they won’t expand unless you’re viewing in Marked with the preprocessor enabled. In cases where you want to use the Markdown file elsewhere, you’ll want to run the script separately or as a Service that will update the actual source text. Because the script runs fine outside of Marked (as all custom processors will), creating a Service for it is simple:

  1. Create a new file in Automator.app and select “Service” as the type.
  2. Add a Run Shell Script action
  3. Set the interpreter to Ruby and the “Pass input” to “as stdin”
  4. Paste in the ezsnippets.rb script
  5. Save it as EZSnippets

Now you can select all the text in a document, right click and choose EZSnippets from the contextual menu. It will replace all of the abbreviations in your text and give you a “clean” document.

I want to start collecting custom processor and preprocessor scripts for Marked in a GitHub repository (similar to the Custom Style repo), so if you have any clever ones you’d like to add, let me know. In the long run, these scripts are going to be modular so you can load multiple scripts, chain them, and enable/disable them as desired. It’s not on the immediate todo list, but it’s definitely on the roadmap.

  1. Only in the latest version. In older versions, it’s under “Behavior.”