How-To: Randomly-Selected Snippets in TextExpander


A couple of buddies had mentioned on the Interblags that they wished TextExpander supported what they dubbed “random snippets”. In a nut, they want to be able to type (using Shawn’s example) “ttypo” and have TextExpander choose a random snippet from a predefined list. This is something I’ve thought about in the past, but had never given a whole lot of thought. Thankfully, TextExpander can do this out of the box with a little scripting-fu.

Part of what makes TextExpander so badass is that, in addition to its normal text substitution capabilities, you can also have it run scripty things (either shell scripts or AppleScript). Because we have access to the shell, we also have access to a whole host of full-blown programming languages from within TextExpander. This particular capability is what helped me quickly (like, in under five minutes) implement the feature my friends wanted. Here’s how I did it.

First, I wanted to be able to edit the “candidate snippets” — that is, the collection of snippets from which a random selection would be made — without having to edit the script itself. Luckily, TextExpander provides a way to embed snippets in other snippets. So, I started with the following snippets:

  • typo1 - Found a typo!
  • typo2 - Found another typo!
  • typo3 - Find yet another typo!

Next, I created a new snippet and set it to type “Shell Script”. Here’s the code:

If you’ve written python code before, this is pretty straightforward. The first line, known as the “shebang” line, tells the shell to run the script using the Python interpreter. The next line imports the `random` and `sys` modules. The third line defines a collection of snippets (using their embedded-snippet syntax so we can edit the snippets themselves without monkeying around with this script). The last line chooses one element from our list at random and writes it to the system’s standard-out interface (STDOUT). This is where TextExpander listens for script output. It’s not terribly important that you understand all the guts of what’s going on here. The line where we define the list of snippets is really all you’ll need to edit when you customize this. The only known issue here is (and this may disappoint Shawn) is that your snippets can’t contain newlines (what’s inserted when you strike the Return key). If you want to include newlines in your snippet, you’ll have to express them using the newline character:

I realize this is a bit of a pain in the ass, but such is life.

Post any questions you have in the comments and I’ll try to sort you out. Go TextExpander!

Trackbacks

  1. of january. the poor ups driver experiences…

    a large increase in the number of elliptical trainers and treadmills he has to unload and deliver. and everyone starts researching products that help us get started. “detox” and “body cleanse” are entered into search engines at lightening speed. in…

  2. vital to check how it all looks…

    once it has hit the www. often when you transfer content from your computer to your website, you can get formatting issues or spacing issues, so make sure you always cast a quick eye over your latest offering once it is…

  3. commentators on your blog is also a…

    good move. it encourages visitors to comment on your blog to gain the top positions.so, start following the above simple tips and you will definitely see the difference. are you happy with the amount of traffic you’re currently getting to your…

  4. try this says:

    portfolio will increase the value of the…

    documentation available to others.take the best writing pieces and add them to your portfolio file. if you have recently learned how to create press releases, make a very solid sample to provide to new employers.working from home is a great option,…

  5. site or specific subpages within your site….

    this is key, since the number of links pointing to your site or specific subpages plays a major role in how high they come up in the search engines. with your articles published in different article websites and your link on…

  6. see this says:

    if they have mastered the mechanics of…

    how to be a good writer. this is mainly because good writing comes from experience. experiences fuel the creative thought process behind someone’s writing and they drive the creation of new written material.as you begin your process to becoming a bett…

  7. click this says:

    pre-loaded into wordpress and should be found…

    in the plugin section. this will need a key for it to be enabled and you will need to register with akismet to get a key. it used to be available from wordpress.org but unfortunately they have stopped doing that. [essential]2….

  8. francise says:

    your content because such images are known…

    to generate quality traffic.do not keep your readers waiting bloggers love to find new contents every time they visit the web. it is therefore very important to write quality articles regularly to generate and keep the interest of your prospects. keepi…

  9. you gain expertise, would you mind updating…

    your blog with more information? it is extremely helpful for me. i would like to thank you for the efforts you have made in writing this article and i am hoping the same best work from you in the future as…

  10. there are many other cool ways to…

    get awesome backlinks and i am going to share some. the first and greatest is ezinearticles; this is the best and fastest way to get a respected backlink to your blog. just write an article and add a link back to…

  11. website says:

    money blogging, the only thing stopping you…

    is yourself. get out there and start dreaming up new blogs and start creating content for the web. before you know it you will start to see a new source of income doing something you enjoy in your spare time, and…

  12. offering a $120 a month seo offer….

    (which is super inexpensive, and well below the going industry rate as well….and offer tremendous value to your clients and customers as a result)when you combine a sale, with a simple service offer follow up, as outlined above…..getting to $75k (o…