Here’s a TextExpander trick for automating dynamic portions of snippets. It’s a way to allow the power of shell scripting inside of regular text snippets, avoiding extra fill-ins or running two snippets. First, a short backstory.
When I set up the Sponsor posts for my site, I always schedule them for 6am on the next Thursday, but I rarely write them on the same day of the week. Thus, I can’t just use TextExpander’s date math tools. I have a TextExpander snippet that fills in a skeleton sponsor post with all of the necessary headers, tags, categories and other specifics. However, if I run that snippet, I also have to run my “Jekyll Make-a-date” snippet in addition to get the right date in for scheduling1.
In TextExpander, you can embed snippets inside of other snippets, but it doesn’t work with shell snippets that require input, like the Make-A-Date snippets. Shell snippets that run on their own, though, work perfectly. So, I hardcoded a date string into a trimmed down version of the older, PHP-based Make-A-Date snippet and now my sponsor snippet always includes the date for next Thursday.
Creating a date math snippet
This idea can be modified to create snippets for whatever date you need, and the output can be changed to match your requirements. I’ll show you how.
- First, create a new snippet and give it a label. In my case, it’s just called “Next Thursday.”
- Set the content type at the top of the edit field to “Shell Script.”
- Modify the script below (instructions follow it) and paste it into the field.
- Assign an abbreviation. This is how you’ll reference it in other snippets.
Here’s the code:
Line 4 ($input) is the string to convert. “next thursday 6am” will be interpreted as the next available Thursday. If it’s Wednesday, that’s going to be interpreted as “tomorrow,” but if it’s Thursday or later in the week (or earlier next), it will look forward. You can experiment with various date strings to find one that suits your needs. PHP is not the best natural language processor, but we don’t need too much flexibility in this case.
Line 5 ($dateformat) specifies the output of your date. It uses the PHP date string format, which you can find in the PHP documentation. The one in the code above creates “2014-02-20 06:00”, which is what I need for my blog headers. You can make it more verbose and customize it just by changing the characters in the string based on the PHP documentation.
Using it in other snippets
To use the snippet inside of another snippet, just reference it like this:
You can fit that anywhere in the text of your main snippet and it will automatically execute and insert the date. This works even if you have fill-ins in the main snippet.
You can use this technique to harness the powers of Ruby, Python, AppleScript and more. As long as you don’t need input to run the script, you can automate dynamic portions of other snippets easily. In fact, I just made one that gets the date for last Thursday, this Wednesday and finds all my posts in between to create a recap post each week. You’ll see that one soon.
Be sure to check out my TextExpander Tools collection for more snippet fun.
Sure, that only takes an extra 10 seconds, but 10 seconds every week adds up to almost 9 minutes a year.↩