Six Colors
Six Colors

by Jason Snell & Dan Moren

This Week's Sponsor

End users aren't your enemy! Kolide gets users to fix their own device compliance problems–and unsecure devices can't log in. Click here to learn how.

By Jason Snell

Bad AppleScript: Load the Template Gun!

Note: This story has not been updated since 2020.

For a couple of years now I’ve been using Zip files as templates for my podcast editing work, leaving them on my Desktop for quick access. But lately I’ve acquired enough template files that they were cluttering up my desktop. I decided I wanted to write a script that would work like a hot dog cannon, but for templates. Somewhere else on my Mac, I’d have a folder full of template files. Upon launching this script, I’d pick which template I want to be dispensed, and it will be unzipped and placed on my Desktop.

I’m sure there’s a way to do this with a four-line shell script, but I don’t know how to shell script and I do know how to use AppleScript, so that’s the path I chose to take.

set templateFolder to (alias "Macintosh HD:Users:jsnell:Dropbox:Podcast Templates:")    
tell application "Finder" to set theNames to the name of items in folder templateFolder

First thing’s first – I grab the contents of my Podcast Templates folder, both their full file references and their names.

 set mychoice to (choose from list theNames with prompt 
   "Choose your Template" default items "None" 
   OK button name {"Choose"} 
   cancel button name {"Cancel"}) as string

This is the command1 that lets me choose which template I want from out of the items in the folder.

tell application "Finder"
    duplicate item ((templateFolder as text) & mychoice) as alias to (path to desktop folder)
    open item ((path to desktop folder as string) & mychoice) as alias
    delay 2
    delete item ((path to desktop folder as string) & mychoice) as alias
end tell

This next block copies the template file out of the folder, unzips it, waits two seconds (because sometimes it takes Finder a second or two to update), and then deletes the original archive. I considered unzipping a copy of the folder first, then moving that folder, but the name of the resulting unzipped folder could be anything—and I don’t want to do the work to guess. I know the name of the Zip archive, so that’s what I deal with.

For the Download podcast, which I only edit on the day of release, I’ve added a final bonus feature to the script that properly labels the folder and the Logic project inside with the right episode number.

if mychoice contains "Download" then
    set theDesktop as (path to desktop folder) as string        
    set unixDate to (do shell script "date +'%s'") as number
    set premiere to "1493316000" as number
    set newdate to unixDate - premiere — seconds since premiere
    set newdate to newdate / 86400 — days since premiere
    set weekcount to newdate / 7 —weeks since premiere
    set weekcount to weekcount as integer
    set weekcount to (weekcount - 1) —adjust for extra episodes
    set theNewName to ("Download " & (weekcount as string))
    tell application "System Events"
        set name of file (theDesktop & "Download Template:download.logicx") to (("download" & weekcount as string) & ".logicx")
        set name of folder (theDesktop & "Download Template") to theNewName
    end tell
end if

This is a more brittle fragment, because if I rename the Download template to something different it wouldn’t work. But that’s unlikely to happen. It uses the unix time format to figure out how many weeks it’s been since the first episode of Download, applies a fudge factor (for skipped weeks), and then renames the folder and template to the current episode number. It works!

Is my Template Gun script—which I saved as an app and dropped in my Applications folder, so I can launch it with LaunchBar—anything special? It most certainly is not. But it saves me a little bit of time and a little bit of Desktop clutter, and I’m glad I did it.

  1. Actually all one line, but broken up here for readability. 

If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.

Search Six Colors