Previous Week Number Using TextExpander

Greg Scown Blog, TextExpander, TextExpander Tips

A user wrote in asking if TextExpander’s date math could be used to determine the previous week number. He clarified that he wanted to get the week number of last week, not necessarily 7 days ago. It’s easy to subtract seven days using date math, but this isn’t a common request, so it’s not part of our standard date math. It’s a great case for why we support AppleScript, JavaScript and shell scripting in TextExpander.

Given that JavaScript works on OS X, iOS, and will soon work on TextExpander for Windows, let’s do this one in JavaScript. We’ll let TextExpander give us the current week number, then we’ll subtract one. If we wind up with zero, then we know it’s week 52 because we’re wrapping around to the last week of the previous year.

  1. Click “New Snippet”
  2. Set the Content: to JavaScript
  3. Enter the following content:
  4. var lastWeek = %date:ww%-1;
    if (lastWeek == 0) {
    	lastWeek = 52;
    }
    "WK" + lastWeek;
    
  5. Set your abbreviation, and you should be ready

At this point, I noticed that the user reported his app version as 3.4.2 (released in March 2012), which does not include JavaScript support. I adapted the script to AppleScript for users of versions of TextExpander prior to 5.0, where we introduced JavaScript support:

  1. Click “New Snippet”
  2. Set the Content: to AppleScript
  3. Enter the following content:
  4. set lastWeek to %date:ww%-1
    if lastWeek is equal to 0 then
    	lastWeek = 52
    end if
    return "WK" & lastWeek
    
  5. Set your abbreviation, and you should be done

If you do have an older version of TextExpander and use the AppleScript version of this, please consider upgrading. It’s through paid upgrades that we can continue to improve TextExpander, such as adding JavaScript support for example. It also lets us provide fast and friendly support as well as post handy tips on the blog.