Bad AppleScript: Template Gun meets the Internet
Back in 2018 I wrote about Template Gun, my AppleScript app that lets me shortly generate new venture folders for the podcasts I’m engaged on. (I image the “Template Gun” as extra of a hot-dog or t-shirt cannon.)
The premise is that I hold a folder stuffed with zip archives containing the Logic Pro initiatives and asset information for all of my podcasts. Template Gun launches, offering me an inventory of all my present templates, and once I decide one it’s routinely unzipped on my Desktop, able to go to work.
If you frequently end up constructing venture folders (of any form!) from scratch in the Finder, I extremely advocate the idea of creating your self a zipper archive you should utilize as a ready-made template. And if you happen to’re obtained a number of templates to select from, one thing like my Template Gun script certain beats leaving all of them in your Desktop or fishing them out of a folder.
But certainly one of my favourite options of Template Gun is that, after it unzips the template, it does some useful renaming of the ensuing folder and information so as go get it prepared for my use. Back in 2018, I wrote about how Template Gun was taking the present date, subtracting it from the launch date of my podcasts, after which utilizing that to present my podcast information the correct episode quantity.
This appeared logical to me at the time, however now it appears ridiculous. For two causes:
Sometimes I do two podcasts in every week; generally I skip weeks. It’s been 493 weeks since I launched The Incomparable, however this weekend I’m posting episode 500. Using week offsets means you must edit the script each time you don’t launch one single episode per week.
The a part of my mind that conceives of automating duties on my Mac is, apparently, nonetheless mired in 1995—an period the place connectivity to the Internet couldn’t be assumed. When I construct Shortcuts on iOS, I continuously avail myself of internet companies. My scripts on the Mac are supposed to be run with none community round. It’s 2020. This is silly.
Which brings me to my revelation: If there’s an Internet-based supply that may inform me the present episode variety of my podcast, I can simply look it up there and increment by one, creating the template for the subsequent episode. Every podcast has an RSS feed. We can do that.
set theFeed to (do shell script "curl https://www.relay.fm/upgrade/feed")
Once once more,
do shell script is the MVP of scripting on macOS, as a result of it lets me hand off instructions to useful unix instruments like
curl, which works and fetches the whole contents of any URL.
set theFeed to (characters 1 via 2000 of theFeed) as string
Then I parse the first 2000 characters of the feed — that’s all I want, and podcast RSS feeds are massive.
set theEpisode to merchandise 2 of (getmatch(theFeed, "<title>Upgrade ([0-9]+)"))
Now I’m utilizing the useful
getmatch subroutine to do a regular-expression search to seize the first episode quantity to look in the feed.
set theEpisode to ((theEpisode as integer) + 1)
Increment the integer by one, and we’ve obtained our new episode quantity.
The CMS we’ve constructed for The Incomparable generates a semicolon-delimited stats file for each podcast, so for my Incomparable podcasts I’ve to parse barely in a different way:
set theResult to (do shell script "curl https://www.theincomparable.com/vulcanhello/stats.txt") set theStart to (characters 1 via 6 of theResult) as string set theEpisode to (characters 1 via ((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 again on dates:
if mychoice accommodates "Six Colors" then set todaysDay to day of (present date) as string set theMonth to month of (present date) as string set theYear to (yr of (present date)) as string set theNewName to ("sixcolors-" & theMonth & "-" & todaysDay & "-" & theYear) inform utility "System Events" set identify of file (theDesktop & "Six Colors:sixcolors.logicx") to (theNewName & ".logicx") set identify of folder (theDesktop & "Six Colors") to theNewName finish inform finish if
It’s a pleasant contact that reduces the quantity of labor I have to do to prep my workspace and start the actual work at hand. If you can see your self utilizing one thing like this to get your job accomplished, be happy to have a look at my AppleScript supply code. And don’t neglect: the Internet is a superb useful resource that your scripts can use to save lots of you effort.changing into a Six Colors subscriber. Subscribers get entry to an unique e-newsletter, podcast, and group.]