Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Last active May 27, 2023 06:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsnell/ace81f30fed763a93499d41c45342a1b to your computer and use it in GitHub Desktop.
Save jasonsnell/ace81f30fed763a93499d41c45342a1b to your computer and use it in GitHub Desktop.
Template Gun
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set nameArray to {}
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
set mychoice to (choose from list theNames with prompt "Choose your Template" with title "Template Gun" default items "None" OK button name {"Choose"} cancel button name {"Cancel"}) as string
if mychoice contains "false" then
quit
end if
tell application "Finder"
set theDesktop to (path to desktop folder as string)
duplicate item ((templateFolder as text) & mychoice) as alias to (path to desktop folder)
open item (theDesktop & mychoice) as alias
delay 2
delete item (theDesktop & mychoice) as alias
end tell
(*
if mychoice contains "TVTM" then
set episodeCount to getepisode("tvtm","incomparable")
set theNewName to ("TVTM " & (episodeCount as string))
tell application "System Events"
set name of file (theDesktop & "TVTM Template:TVTM Template.logicx") to (("tvtm" & episodeCount as string) & ".logicx")
set name of folder (theDesktop & "TVTM Template") to theNewName
end tell
end if
*)
if mychoice contains "Vulcan Hello" then
set episodeCount to getepisode("vulcanhello", "incomparable")
set theNewName to ("Vulcan Hello " & (episodeCount as string))
tell application "System Events"
set name of file (theDesktop & "vulcanhello:vulcanhello.logicx") to (("vulcanhello" & episodeCount as string) & ".logicx")
set name of folder (theDesktop & "vulcanhello") to theNewName
end tell
end if
if mychoice contains "Magnum" then
set episodeCount to getepisode("magnum", "incomparable")
set theNewName to ("Magnum " & (episodeCount as string))
tell application "System Events"
set name of file (theDesktop & "magnum:magnum.logicx") to (("magnum" & episodeCount as string) & ".logicx")
set name of folder (theDesktop & "magnum") to theNewName
end tell
end if
if mychoice contains "Doctor Who" then
set episodeCount to getepisode("teevee", "incomparable")
set theNewName to ("teevee " & (episodeCount as string))
tell application "System Events"
set name of file (theDesktop & "teevee-doctorwho:teevee-doctorwho.logicx") to (("teevee" & episodeCount as string) & ".logicx")
set name of folder (theDesktop & "teevee-doctorwho") to ("doctorwho-" & theNewName)
end tell
end if
(*
if mychoice contains "Download" then
set theEpisode to getepisode("download","relay")
set theNewName to ("Download " & (theEpisode as string))
tell application "System Events"
set name of file (theDesktop & "Download Template:download.logicx") to (("download" & theEpisode as string) & ".logicx")
set name of folder (theDesktop & "Download Template") to theNewName
end tell
end if
*)
if mychoice contains "Six Colors" then
set theNewName to ("sixcolors-" & getdate())
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
on getepisode(slug, network)
if network is "relay" then
set theFeed to (do shell script "curl https://www.relay.fm/" & slug & "/feed")
set theFeed to (characters 1 thru 2500 of theFeed) as string
set theEpisode to item 2 of (getmatch(theFeed, "<itunes:episode>([0-9]+)</itunes:episode>"))
set episodeCount to ((theEpisode as integer) + 1)
else
set theResult to (do shell script "curl https://www.theincomparable.com/" & slug & "/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)
end if
return (episodeCount as string)
end getepisode
on getdate()
set todaysDay to day of (current date) as string
if (count of characters of todaysDay) is 1 then
set todaysDay to ("0" & todaysDay)
end if
set theMonth to month of (current date) as number
if (count of characters of (theMonth as string)) is 1 then
set theMonth to ("0" & theMonth)
end if
set theYear to (year of (current date)) as string
return (theYear & "-" & (theMonth as string) & "-" & todaysDay)
end getdate
on doesmatch(s, regex)
-- Subroutines by Michael Klement <https://github.com/mklement0>
# doesMatch(text, regexString) -> Boolean
# DESCRIPTION
# Matches string s against regular expression (string) regex using bash's extended regular expression language *including*
# support for shortcut classes such as `\d`, and assertions such as `\b`, and *returns a Boolean* to indicate if
# there is a match or not.
# - AppleScript's case sensitivity setting is respected; i.e., matching is case-INsensitive by default, unless inside
# a 'considering case' block.
# - The current user's locale is respected.
# EXAMPLE
# my doesMatch("127.0.0.1", "^(\\d{1,3}\\.){3}\\d{1,3}$") # -> true
local ignoreCase, extraGrepOption
set ignoreCase to "a" is "A"
if ignoreCase then
set extraGrepOption to "i"
else
set extraGrepOption to ""
end if
# Note: So that classes such as \w work with different locales, we need to set the shell's locale explicitly to the current user's.
# Rather than let the shell command fail we return the exit code and test for "0" to avoid having to deal with exception handling in AppleScript.
tell me to return "0" = (do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; egrep -q" & extraGrepOption & " " & quoted form of regex & " <<< " & quoted form of s & "; printf $?")
end doesmatch
on getmatch(s, regex)
-- Subroutines by Michael Klement <https://github.com/mklement0>
# SYNOPSIS
# getMatch(text, regexString) -> { overallMatch[, captureGroup1Match ...] } or {}
# DESCRIPTION
# Matches string s against regular expression (string) regex using bash's extended regular expression language and
# *returns the matching string and substrings matching capture groups, if any.*
#
# - AppleScript's case sensitivity setting is respected; i.e., matching is case-INsensitive by default, unless this subroutine is called inside
# a 'considering case' block.
# - The current user's locale is respected.
#
# IMPORTANT:
#
# Unlike doesMatch(), this subroutine does NOT support shortcut character classes such as \d.
# Instead, use one of the following POSIX classes (see `man re_format`):
# [[:alpha:]] [[:word:]] [[:lower:]] [[:upper:]] [[:ascii:]]
# [[:alnum:]] [[:digit:]] [[:xdigit:]]
# [[:blank:]] [[:space:]] [[:punct:]] [[:cntrl:]]
# [[:graph:]] [[:print:]]
#
# Also, `\b`, '\B', '\<', and '\>' are not supported; you can use `[[:<:]]` for '\<' and `[[:>:]]` for `\>`
#
# Always returns a *list*:
# - an empty list, if no match is found
# - otherwise, the first list element contains the matching string
# - if regex contains capture groups, additional elements return the strings captured by the capture groups; note that *named* capture groups are NOT supported.
# EXAMPLE
# my getMatch("127.0.0.1", "^([[:digit:]]{1,3})\\.([[:digit:]]{1,3})\\.([[:digit:]]{1,3})\\.([[:digit:]]{1,3})$") # -> { "127.0.0.1", "127", "0", "0", "1" }
local ignoreCase, extraCommand
set ignoreCase to "a" is "A"
if ignoreCase then
set extraCommand to "shopt -s nocasematch; "
else
set extraCommand to ""
end if
# Note:
# So that classes such as [[:alpha:]] work with different locales, we need to set the shell's locale explicitly to the current user's.
# Since `quoted form of` encloses its argument in single quotes, we must set compatibility option `shopt -s compat31` for the =~ operator to work.
# Rather than let the shell command fail we return '' in case of non-match to avoid having to deal with exception handling in AppleScript.
tell me to do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; shopt -s compat31; " & extraCommand & "[[ " & quoted form of s & " =~ " & quoted form of regex & " ]] && printf '%s\\n' \"${BASH_REMATCH[@]}\" || printf ''"
return paragraphs of result
end getmatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment