• Home
  • /
  • Blog
  • /
  • Save Time with Expert-Level TextExpander Snippets

This is a guest post by Helmut Hauser. He is a Lecturer (Assistant Professor) in Robotics at the University of Bristol (UK), a father of three little boys, and a productivity geek. He shares his tips and tricks on how to work smart and be remarkable from his view point as an academic on his blog and you can find him on Twitter.


TextExpander is an incredibly useful tool that expands user-defined text abbreviations (called snippets) into more complex pieces of text. This can save you hours of typing.

You can define shortcuts for your address, email or telephone number. It can be used to easily access special symbols like €, £, ⌘ or ↑. You can expand them into email signatures with logos and contact information, as well as predefined complex text structures (e.g., canned email responses, contracts, FAQs, etc.) that include drop-down lists to adapt the snippets for individual cases. You can even carry out basic date manipulation and use text from the clipboard. For an extensive video guide on all TextExpander features, have a look at this Asian Efficiency post.

Besides these standard features, TextExpander can do even more. You can use snippets to initiate your own scripts. This adds another level to the already excellent TextExpander experience. Here I will show a couple of simple but powerful examples of how to use this feature. The scripts I show in this blog post will use basic code blocks that you can use to build your own snippets. I encourage you to have a look at the code and play around with the scripts. Try to make changes and adapt them to your needs.

Even if you don’t have any experience with coding, you’ll find it super easy to make your own power snippets. Soon you’ll love TextExpander even more.

When Does a Script in TextExpander Make Sense?

Before we jump into the nitty-gritty, let’s have a look at when it makes sense to use scripts. As I mentioned earlier, TextExpander itself has already a number of powerful built-in features that can help you in a wide range of cases.

For example:

  • you can access the current content of the clipboard with %clipboard
  • you can format the output using rich text format
  • you can define drop down or pop up menus to choose from user-defined options
  • you can get time and date in different formats (e.g., October 1, 2019 or 10/01/19)
  • you can carry out simple date/time manipulations (e.g., adding five days to the current date)
  • you can set the cursor to a user-defined position

So before you start coding a complex script, I suggest that you have a look at these default functionalities of TextExpander and see if you can implement a solution with them.

Another point to consider is that TextExpander is primarily about expanding text. While this sounds obvious, there are almost no limits to what you can do with scripts. Because of this, it’s very easy to fall into the trap of using TextExpander for things that could have been implemented more easily with other tools like Apple Services, Alfred 2, KeyboardMaestro or others.

Getting Started

How to tell TextExpander to start a script

The first step is to tell TextExpander that your snippet is not simple text, but should be interpreted as a script. If you don’t do that, it will simply produce the code lines without doing actually what you want. You can tell TextExpander to treat it as a script by clicking on the drop-down list “Content,” which reveals the options “Plain Text” (default), “Formatted Text, Pictures,” as well “AppleScript,” “Shell Script” and, since version 5, also “JavaScript.”

Setup Snippet with Script

First simple example: say goodbye in random ways

An easy snippet to start with can be found on the webpage of Smile Software, the home of TextExpander. Every time you call the snippet it will expand into a greeting that will be randomly chosen from a set of predefined phrases to close your email.

First example code xbye enhancedYou can download the snippet here. You don’t have to understand all the details of the script to use it, but it basically has four steps. First, it defines a list of greetings to choose from. Second, it uses a random generator to choose one of them and assign it to the variable “selectedGreeting.” Third, it takes this greeting, adds your name and some formatting and stores it in the variable “finalOutput.” Fourth, it returns this text to TextExpander for expansion.

Open your favorite text editor (e.g., TextEdit) and run the snippet a couple of times by typing xbye. You’ll see that it randomly produces one of the greetings in the list.

Now let’s make some changes to see how this works. First, let’s change the last line from

return “finalOutput”

into

return “See you soon”

Now run the snippet a couple of times. It will only produce “See you soon,” since the randomly produced “selectedGreeting” will not be returned to TextExpander at all.

Let’s change the last line back (returnfinalOutput“) and try something else to personalize the script for you. As you can see, in the second-to-last line (step 3), we assemble the actual output from smaller text pieces. Change “Your Name” to your actual name (note that you have to put it in quotes) and run the snippet a couple of times.

As you can see, you can build more complex text by connecting smaller chunks like extra spaces, punctuation or even variables like the “selectedGreeting” variable in the example code. AppleScript uses the ampersand (“&”) to glue everything together. You can even add a new line by using the keyword “return” or add the current content of the clipboard by using the variable “the clipboard.”

For example, change the last line to the one below and see what happens:

return finalOutput & return & return & ” PS: The content of my clipboard is currently: ” & return & “————–” & return & (the clipboard) & return & “————–“

Finally, try adding another greeting of your choice to the list (e.g., “See you”) or remove one that you don’t like (see step 1 of the script). The code is flexible enough to work with different list sizes.

Tip: You can make two different snippets: one for informal greetings for friends and family and another one for your work.

Now that we have the basics down, let’s go to the next level and have a look at the magic of date and time manipulation.

Using the Power of Date Manipulation

Although TextExpander has some useful date manipulation features out of the box, you need scripts to get to the advanced stuff. With scripts, you can calculate dates and times more dynamically. You can refer to certain days of the week, consider time zones and much more. Here are a couple of examples of how you can use AppleScript to boost your TextExpander experience when dealing with time and dates.

See you next Tuesday

I often refer in emails to a certain day of the week rather than to a specific date. For example, I use Tuesdays and Fridays as my meeting days. If I have to find a time slot for a meeting, I naturally suggest scheduling it for one of those two days. Instead of looking up next Tuesday or Friday’s date, I use a nice little script in TextExpander that calculates the dates for me. For example, I can write “nxtue” and it will expand to the nicely formatted piece “next Tuesday (October 8, 2019)” by calculating the correct date. I have defined snippets for every day of the week.

Below you can see the table of abbreviations I use and their corresponding expansions when I used them on October 1, 2019 (Tuesday).

abbreviation

expands to

nxmon

next Monday (October 7, 2019)

nxtue

next Tuesday (October 8, 2019)

nxwed

next Wednesday (October 2, 2019)

nxthu

next Thursday (October 3, 2019)

nxfri

next Friday (October 4, 2019)

nxsat

next Saturday (October 5, 2019)

nxsun

next Sunday (October 6, 2019)

As you can see, it calculates the next upcoming day of the week. For example, when I look on a Wednesday for the next Thursday, I get tomorrow’s date.

Sometimes, however, what I want is the date of next week’s Thursday. For that, I use a slightly adapted version of the script and make new corresponding abbreviations like “nwmon” for next week’s Monday. You can see in the table below examples from when I called the script on October 2, 2015 (Wednesday).

abbreviation

expands to

nwmon

next Monday (October 7, 2019)

nwtue

next Tuesday (October 8, 2019)

nwwed

next Wednesday (October 9, 2019)

nwthu

next Thursday (October 10, 2019)

nwfri

next Friday (October 11, 2019)

nwsat

next Saturday (October 12, 2019)

nwsun

next Sunday (October 13, 2019)

The script basically jumps to the beginning of next week and then looks for the next upcoming day.

You can download the snippets here:

Calculate date ranges

Similar to the examples above, you can use AppleScript to calculate time ranges. For example, I use a script to automatically calculate the time range for my next week. TextExpander takes this result and puts it in a nice format.

nvALT link in OFThis comes in very handy when I make a list of goals for the upcoming week in nvALT.

The code works in such a way that, if I expand the snippet on a Monday or Tuesday, it will calculate the date range for this week, rather than for the upcoming week. The reason is that sometimes I’m not able to plan my upcoming week on Sunday, but rather on Monday or even Tuesday. You can easily tweak this, though. Also, since you now know how to format and build more complex text pieces from our first example, I suggest you give it a try and adapt it for your needs.

You can download the snippet here.

Working with Web Addresses

A great way to use TextExpander snippets and AppleScript is to fetch a URL (i.e., a web address like https://asianefficiency.com) from your browser and to do something useful with it. AppleScript is powerful at getting information from other applications, especially from Apple’s own software.

The most basic script to get a URL from Safari is just one line and is very easy to understand:

tell application “Safari” to return URL of current tab of window 1

If you have multiple Safari windows and tabs open, it will fetch the URL from the window on top and from there the currently active tab. You can get also fetch the title of the webpage (i.e., the title that shows up on the top of your browser):

tell application “Safari” to return name of current tab of window 1

URL and titleYou have to use a slightly different formulation to make it work with Chrome:

tell application “Google Chrome” to return URL of active tab of front window

tell application “Google Chrome” to return title of active tab of front window

Here is a little table of snippets that I use to get URLs. You can download them here. Note that the window can be in the background, and the snippet will still work.

abbreviation

expands to

safurl

will expand to the URL from the current Safari window

saftit

will expand to the title of the current Safari window

chrurl

will expand to the URL from the current Chrome window

chrtit

will expand to the title of the current Chrome window

Tip: You can extend these scripts to get a list of URLs of all open tabs. Google for it, and you’ll find all kinds of solutions.

Now, let’s do something more with these URLs.

Making customized links in HTML, Markdown, LaTeX, etc.

Getting the URL or the title so easily is useful, but we can take this one step further. We can format the address to serve as a link in a document — for example, in the form of an HTML link, Markdown link or LaTeX link. The table below shows the output when Safari has opened https://asianefficiency.com and the snippets have been called.

abbreviation

expands to

lhtml

<a href=”https://asianefficiency.com”> ↓ </a>

lmd

[↓](https://asianefficiency.com)

llatex

\href{https://asianefficiency.com}{ ↓ }

You can download the snippets here. Note that the down arrow “↓” will not show up; I use it here to show the location of the cursor after the expansion. TextExpander has this nice feature where you can define the cursor position after the expansion with the placeholder “%|“. In our case, this makes it quicker to add an appropriate link title right after the expansion without the need to fiddle around with the mouse or arrow keys.

Another trick I use in this snippet is the possibility to call a snippet within another one (a.k.a. a nested snippet). You simply have to use the special form %snippet:snippet_name%. For example, the AppleScript code to get an HTML link looks like this:

<a href=”%snippet:safurl%”> %|

The part %snippet:safurl% calls the snippet “safurl” and adds its output at exactly this position. As we have seen previously, this will return the URL of the current Safari browser window.

Tip: Use the “nested snippet trick” to make corresponding snippets that will get a URL from Chrome.

Another tip: Use the “nested snippet trick” to automatically add the title of the webpage instead of putting the cursor there.

Get a shortened URL

Sometimes URLs are quite long, so online services like TinyURL, Bitly and Ow.ly exist to shorten them.

I use a script that gets a URL and sends it to TinyURL to get a shortened version of the link. My abbreviation for it is “ltiny.” You can download it here. For example, for the rather long link of Mike Schmitz’s comprehensive TextExpander guide

https://www.asianefficiency.com/technology/comprehensive-textexpander-guide/

you’ll get this much shorter link:

https://tinyurl.com/ompunzj.

This is much more convenient when communicating via emails, messaging or Twitter.

Tip: Try to adapt the scripts from the previous section (e.g., “lhtml”) to include a shortened URL instead of a long one. Consider using a nested snippet.

Getting a Table of Any Size

If you want to use TextExpander to produce automatically formatted tables in Markdown, you’re normally stuck with a fixed size for your rows and columns. However, by using a script you can build tables dynamically by asking the user to provide the number of rows and columns. AppleScript provides you with nice dialog boxes to ask for input, and you can even set default values.

Mdtable rows

Mdtable columns

You can download both snippets here.

Tip: You can easily change the script to produce a table in HTML or LaTeX by adapting the formatting in the script.

Linking to nvALT Documents

If you’re a regular reader of Asian Efficiency, you’ll be familiar with nvALT (see, for example, “Quick Capturing with nvALT” or “A Simple nvALT Workflow”). It’s a wonderful tool to quickly capture and search pieces of information in text form. There’s a nice little trick that allows you to open an nvALT text file from anywhere by using a URL scheme. For example, if I put

nv://find/Top%20list%20this%20week

into my browser address bar, it will open the corresponding text file in nvALT with the title “Top list this week.” The “%20” is a placeholder for spaces. The AppleScript code in the TextExpander looks at nvALT (it has to be open, but it can be in the background) and gets this unique link to the document that is currently in front. This comes in very handy when you want to link an nvALT file within Omnifocus in the notes field.

nvALT link in OFThe snippet abbreviation I use is “lnvalt.” You can download it here.

Getting a Unique Link to an Email Message

Similar to the nvALT example, you can get a unique link to individual emails as well. The beauty of this is that this link even works when you have moved the message to another folder or deleted it. If you’re an Omnifocus user, you’ll be familiar with the Clip-o-Tron. This is an incredibly useful feature that gets you a unique link to an email. 

You can do the same with TextExpander and an AppleScript and go beyond Omnifocus. A great use is a snippet that automatically takes the last sent email and gets the unique link to it.

This is especially helpful for defining a followup task in Omnifocus. You write an email, send it off, then you fire off your Omnifocus “Quick Entry Short,” write something like “waiting for John to answer my question,” go to the note field and call the snippet to add the link to the email.

Mail link in OFYou can download this snippet here if you want to try it yourself. Note that you will have to adapt the code to make it work with your system. Simply change “ACCOUNT_NAME” to your actual account name. You can find it in your Mail preferences under accounts and there in “Description.”

Adapt lmail snippet part 1

Adapt lmail snippet part 2For Power Users: Call Scripts Other Than AppleScript

Beside AppleScript, Shell Script and JavaScript, you can actually run any type of script as long as you can call it from the terminal (e.g., Python, Perl, terminal commands, even entire applications). The trick is AppleScript’s ability to carry out terminal commands: do shell script “your command.” For example,

do shell script “ls /Users/$USER/Applications”

will give you a list of all locally installed applications by listing (“ls”) the content of the Application folder.

Next Steps

I showed you a very small number of possibilities. This is just the beginning. You can do so many more things. Here are a couple of ideas you could consider implementing in a script:

  • convert times into other time zones
  • get the current song playing in iTunes
  • get a list of all tasks in your inbox or from any perspective in Omnifocus
  • get the device information for your Mac
  • create a QR code for your URL and add it to your clipboard
  • read data from an existing Numbers spreadsheet
  • make an HTML table with a user-defined size
  • get weather forecast information for tomorrow
  • make sure you have an attachment when you use the word “attachment” in an email (example on Smile Software)
  • get the word count of the text document you’re currently working on

A great resource for TextExpander snippets is the recently initiated Google+ group “TextExpander Tips.”

Now It’s Your Turn

Start simple! Use one of the examples from this blog post. You can download all the snippets I mentioned here. Change the output to your needs. Make a different format.

Then get crazy. Don’t be afraid. If you need help, Smile support is famously amazing, which helped me immensely with putting this post together.

TextExpander is one of those applications, where as soon as you start to use it, you get tons of ideas about how to use it more and better. I hope the examples here will inspire you.

By the way, the best way to learn a script language like AppleScript (or any other programming language) is to work on a problem you have.

Maybe you already have a great script that you use in TextExpander or you have an idea for a script. We’d love to hear about it. Please share it with us in the comments section.

Work smart, be remarkable, and have a great day!

Helmut

If you want more articles and tips like these, let us know where we can send them to:


This is a guest post by Helmut Hauser. He is a Lecturer (Assistant Professor) in Robotics at the University of Bristol (UK), a father of three little boys, and a productivity geek. He shares his tips and tricks on how to work smart and be remarkable from his viewpoint as an academic on his blog and you can find him on Twitter.


You may also Like

Last Updated: March 14, 2023

Read More
Read More
Read More

ABOUT THE AUTHOR

Thanh Pham

Founder of Asian Efficiency where we help people become more productive at work and in life. I've been featured on Forbes, Fast Company, and The Globe & Mail as a productivity thought leader. At AE I'm responsible for leading teams and executing our vision to assist people all over the world live their best life possible.


Leave a Reply


Your email address will not be published. Required fields are marked

  1. Keyboard Maestro does real scripting far more easily. And it does text expansions. All for $36 – no outrageously priced subscription like TextExpander.

  2. Helmut,

    I’ve been using the day-of-the-week scripts (Thanks!) and I just realized today that I could use one for the second weekday from today, or, as they say in Britain, “Monday week”. So I modified your script to do that. Two tiny changes to make this work:

    1. if returnDate’s weekday is nextWeekdayToFind then return (current date) + (14 * days): 14 replaces 7
    2. return returnDate + (7 * days): add 7 days to jump a week

    I use monwk as the abbreviation.

  3. The Safari Applescripts fail if Safari’s running in full screen. Here’s a fix:

    # get the frontmost window – this will fail in full screen
    try
    tell application “Safari” to return name of current tab of window 1
    return
    end try

    # get window 2 – for full screen
    tell application “Safari” to return name of current tab of window 2

  4. I love these snippets; thank you for sharing them.

    I am having trouble with the scrip for snippet “nxmon.” It won’t compile. This is a screenshot of what I put together. Would really appreciate knowing what I’ve done wrong

    Set myWeekDay to getDateOfNextWeekday(Monday) — Monday
    Set myMonth to month of dateOfmyWeekDay
    Set myDay to day of dateOfmyWeekDay
    Set myYear to year of dateOfmyWeekDay
    Set FullOutput to “next ” & myWeekDay & ” (” & myMonth & amp; ” “& myDay & “, ” & myYear & “)”
    Return fullOutput

  5. Excellent resource!

    A tiny suggestion: You might consider using three dashes in the table-generating code as Github seems to regard that as the minimum. Two dashes won’t get you a table, it seems.

    (This is also true in Hoedown’s renderer, used in MacDown, for example.)

  6. These snipets are great, but with my language (italian) they don’t work. Is there someone who can help me?
    Thanks

  7. Great post. I especially find the follow up of the last sent email very useful.

    One small correction: In the first script example the change and change back of the last line for test purposes should be

    return finalOutput

    without the quotation marks.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}