By Jason Snell
February 7, 2020 10:30 AM PT
Bad AppleScript: Template Gun meets the Internet
Back in 2018 I wrote about Template Gun, my AppleScript app that lets me quickly generate new project folders for the podcasts I’m working on. (I picture the “Template Gun” as more of a hot-dog or t-shirt cannon.)
The premise is that I keep a folder full of zip archives containing the Logic Pro projects and asset files for all of my podcasts. Template Gun launches, providing me a list of all my current templates, and when I pick one it’s automatically unzipped on my Desktop, ready to go to work.
If you regularly find yourself building project folders (of any kind!) from scratch in the Finder, I highly recommend the concept of making yourself a zip archive you can use as a ready-made template. And if you’re got multiple templates to choose from, something like my Template Gun script sure beats leaving them all on your Desktop or fishing them out of a folder.
But one of my favorite features of Template Gun is that, after it unzips the template, it does some helpful renaming of the resulting folder and files in order go get it ready for my use. Back in 2018, I wrote about how Template Gun was taking the current date, subtracting it from the launch date of my podcasts, and then using that to give my podcast files the proper episode number.
This seemed logical to me at the time, but now it seems ridiculous. For two reasons:
- Sometimes I do two podcasts in a week; sometimes I skip weeks. It’s been 493 weeks since I launched The Incomparable, but this weekend I’m posting episode 500. Using week offsets means you have to edit the script every time you don’t release one single episode per week.
The part of my brain that conceives of automating tasks on my Mac is, apparently, still mired in 1995—an era where connectivity to the Internet could not be assumed. When I build Shortcuts on iOS, I constantly avail myself of web services. My scripts on the Mac are meant to be run without any network around. It’s 2020. This is stupid.
Which brings me to my revelation: If there’s an Internet-based source that can tell me the current episode number of my podcast, I can just look it up there and increment by one, creating the template for the next episode. Every podcast has an RSS feed. We can do this.
set theFeed to (do shell script "curl https://www.relay.fm/upgrade/feed")
do shell script is the MVP of scripting on macOS, because it lets me hand off commands to helpful unix tools like
curl, which goes and fetches the entire contents of any URL.
set theFeed to (characters 1 thru 2000 of theFeed) as string
Then I parse the first 2000 characters of the feed — that’s all I need, and podcast RSS feeds are big.
set theEpisode to item 2 of (getmatch(theFeed, "<title>Upgrade ([0-9]+)"))
set theEpisode to ((theEpisode as integer) + 1)
Increment the integer by one, and we’ve got our new episode number.
The CMS we’ve built for The Incomparable generates a semicolon-delimited stats file for every podcast, so for my Incomparable podcasts I have to parse slightly differently:
set theResult to (do shell script "curl https://www.theincomparable.com/vulcanhello/stats.txt") set theStart to (characters 1 thru 6 of theResult) as string set theEpisode to (characters 1 thru ((offset of ";" in theStart) - 1) of theStart) as string set episodeCount to ((theEpisode as integer) + 1)
And for podcasts that don’t have episode numbers, I fall back on dates:
if mychoice contains "Six Colors" then set todaysDay to day of (current date) as string set theMonth to month of (current date) as string set theYear to (year of (current date)) as string set theNewName to ("sixcolors-" & theMonth & "-" & todaysDay & "-" & theYear) tell application "System Events" set name of file (theDesktop & "Six Colors:sixcolors.logicx") to (theNewName & ".logicx") set name of folder (theDesktop & "Six Colors") to theNewName end tell end if
It’s a nice touch that reduces the amount of work I need to do to prep my workspace and begin the real work at hand. If you could see yourself using something like this to get your job done, feel free to look at my AppleScript source code. And don’t forget: the Internet is a wonderful resource that your scripts can use to save you effort.
- Seriously, if you write AppleScript scripts, you need these subroutines. Having grep available in my scripts has been transformative. ↩
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.