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…

  13. makes a great internet marketing blog. with…

    a little bit of work you can make one of the best blogs on the internet. a good blog will exhibit certain qualities that will help to make the internet marketer a ton of money.content is kingyou will hear this said…

  14. opinion about how frequently a business should…

    blog. the real trick is sustainability. blogging every day will be great for getting exposure for your site, but only for as long as you can keep it up. as soon as you run out of topics to post, days go…

  15. to have conversational tones. at the same…

    time, they ought to pass around business tones.iii) avoid infusing personal information into the blog body.iv) make sure that volumes of writings by other bloggers and writers are read thoroughly to accustom one with popular writing styles and to devel…

  16. to find the best blog hosting services,…

    you need to see what these companies tend to offer and what they have in common. there are many companies out there that would claim that they would provide you with really good services for your blog hosting needs, yet that…

  17. my blog says:

    credibility.it’s not brain surgery!it’s really not hard…

    to build your credibility on the internet. continue to put out good information, great products, and always be helpful to others. say what you mean and mean what you say. make sure your products always give more value than your customer…

  18. quality of your posts.just in case you…

    are new, blogs and forums.in essence a blog or forum simply allows someone to post content, questions, or topics of discussion on somebody else’s website. this means that you can post to these websites based on any restrictions website owners have…

  19. door bell says:

    good writers will produce custom written quality…

    content for each individual client. it takes time to research the product, research keywords and write that content. that takes time and costs money. don’t just assume that you will get the same quality of writing if you hire the lowest…

  20. does not win. 6. a legal paperwork…

    error could be a headache to try to resolve favorably or could cost you a tremendous amount of money to fix. 7. if you are submitting a manuscript for publication, editors will quickly reject and discard documents containing errors in spelling,…

  21. they have probably realized that by hiring…

    a company to write articles for them they are getting what they want out of the internet. these articles that are mentioned are a great way for any person or business to get seen. you have a content writing company write…

  22. articles. in fact, the best and professional…

    people in this industry provide attractive and meaningful content that is also search engine optimized (seo). companies or individuals providing these services know the techniques needed to improve the ranking of websites. therefore, before selecting t…

  23. visit us says:

    the power to project the passion, the…

    wisdom, and the expertise your company maintains within your field. blogging has the power to elevate your company into being viewed as an industry leader or resource.building relationships - blogs are not only great resources of information for starve…

  24. tend to scan a lot of information…

    quickly. if they see your article, they need to get the gist of it in a few lines. a descriptive headline will help a lot. in fact, you should write like a journalist, emphasizing the who, what, when, where, and why.know…

  25. if your book wasn’t good enough for…

    them, why would it be good enough for me? you want to present your book in the best light possible so don’t mention the rejections. similarly, don’t in any way belittle yourself or your book such as “i know the opening…

  26. a freelance writer is to interview multiple…

    candidates and get a sense of what’s out there — a benchmark by which you can understand what is available, and what different writers charge for different skill levels. of course, it will be up to you to determine skill level…

  27. not take you more than 15 minutes…

    and you will need one or two hours to make a good research on the market that you want to promote. before you pick a product make sure that they are going to be paying you at least $20 for every…

  28. relationship with them before embarking on profiting…

    from them. after all, would you buy anything from somebody you don’t know or trust? building a responsive list of subscribers is the most effective way to make money from the best blog sites. why only the best blog software will…

  29. include guest posting and commenting on other…

    blogs. you will want to do this after you’ve been around a little while and found other bloggers in a similar, or the same, niche. you do want to ensure that you are posting on well read and respected. you may…

  30. more info says:

    post is just a reflection of how…

    creative a writer can be with a simple set of words. there are certain points to keep in mind while writing a blog post.researchafter choosing the topic you want to write on, first thing you need to do is a proper…

  31. ipad says:

    active clearance, a clean record (no bad…

    credit or legal history), a current passport, and experience in the area where you want to work always help persuading the employer that you are a viable job candidate. before you post an ad for an seo article writer or finalize…

  32. Titanic says:

    decent amount people visit your blog then…

    adsense approves, it is proved in the blog of experts. but its better to wait two months. but, in Titanic the records its found that people got approval in 2 days only.keep reading productive & earning hobbies to learn more about adsense…

  33. that you already have. you just have…

    to put it to action.take action, join blog groups, give them your feeds. together we will turn the tide on the niche marketer and get our readers back! social networking is a branch of internet business where you can market any…

  34. been using because you think it’s going…

    to be a lot of hard work. it does take some work. but the pay-off is well worth the effort. there are many ideas for marketing your business that you’ll find all over the internet, from your friends, and from your…

  35. deadly streets of riverside county in california,…

    that making it alive on the other side is either impossible or improbable.blog writing as moral and social responsibilityi’ve read and seen handful of internet writers who obviously write only for egocentric reasons. few months back, i’ve read an art…

  36. if a video that you upload to…

    youtube is around 100mb big… it’s going to take a long time for that 1 video to upload. and by the time you’ve Titanic finished uploading all of your videos, you will have to wait in the same way while you submit…

  37. Recommeneded websites…

    [...]Here are some of the sites we recommend for our visitors[...]……

  38. car stereo says:

    a couple of time tested methods that…

    have always yielded better results for the website. while the website will gain recognition, it is also one of the surest ways of converting potential customers into real ones.things to look for things that a webmaster or entrepreneur should look for…