Previous Week Number Using TextExpander

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. Click on the Date menu and choose “Unicode Date Format”
  1. Enter ww and click OK
  1. Set the abbreviation to ;weeknumber
  2. Click “New Snippet”
  3. Set the Content to: JavaScript
  4. Paste in the following content:
var lastWeek = %snippet:;weeknumber%-1;
if (lastWeek == 0) {
	lastWeek = 52;
"WK" + lastWeek;
  1. 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:
set lastWeek to %date:ww%-1
if lastWeek is equal to 0 then
	lastWeek = 52
end if
return "WK" & lastWeek

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.