TextExpander snippet for web archives

If I think a web page is likely to be deleted or changed, and I want to be sure I have a copy of it, I use Safari’s Web Archive feature to store a local copy of the page with all its images.

Saving a page as a web archive

By default, Safari chooses the page’s title—the text that appears in the title bar—as the name of the saved archive. This is fine, but I prefer to have the name start with the date on which I saved it. This isn’t, strictly speaking, necessary; I can always get the archive’s creation date by doing a Get Info on it, but it’s faster to just have it in the file name.

Since I’ve long had a TextExpander snippet that outputs the current date in yyyymmdd format, it’s easy to add the date by

  1. Pressing the left arrow key to put the cursor at the front of the title.
  2. Typing ;ssds,1 the abbreviation for the snippet (mnemonic: “super short date string”).
  3. Typing a space-hyphen-space to separate the date from the page title.

I have, however, some mental defect that often leads me to start typing the snippet before I hit the arrow key. Because the Save sheet comes up with the proposed file name already selected, the page title gets deleted as soon as I touch the semicolon key. To save myself from this habit, I recently came up with this AppleScript snippet:

tell application "Safari"
  set title to name of current tab of front window
  set today to (do shell script "date '+%Y%m%d'")
  get today & " - " & title
end tell

This gives me, in a single step that I can’t mess up, the archive name I would have gotten by going through the three steps above. I set its abbreviation to ;wa (mnemonic: “web archive”) and can type it as soon as the Save sheet appears.

Two final comments:

  1. I used the date command to get the formatted date because it’s much easier than doing it in AppleScript.
  2. There’s no syntax highlighting because Highlight.js doesn’t have a language definition file for AppleScript. I may look into changing that.

  1. If you’re wondering why my TextExpander abbreviations start with a semicolon, you can read this post to see the method to my madness. ↩︎