Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Last active October 27, 2022 15:33
Show Gist options
  • Save jasonsnell/9b95468474d6cc864d4feb6e2a5ca9cf to your computer and use it in GitHub Desktop.
Save jasonsnell/9b95468474d6cc864d4feb6e2a5ca9cf to your computer and use it in GitHub Desktop.
Podcast Noter AppleScript
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
# Sample Keyboard Maestro macros at
# <https://sixcolors.com/wp-content/uploads/2022/01/Podcast-Noter-Macros.kmmacros.zip>
on run argv
try
set theNote to (item 1 of argv)
on error
set theNote to ""
end try
set theSavePath to "/Users/jsnell/Desktop/" -- put your own path here
set theDatematch to (do shell script "date '+%Y%m%d'")
set theLatestMatch to date "Monday, January 1, 2001 at 12:00:00 AM"
try
tell application "Finder"
set theMatches to (every file of (POSIX file theSavePath as alias) whose name contains theDatematch)
set theWinner to item 1 of theMatches
repeat with i from 1 to (count of items in theMatches)
set theMatched to creation date of item i of theMatches
if theMatched > theLatestMatch then
set theLatestMatch to theMatched
set theWinner to item i of theMatches
end if
end repeat
set theStartTime to (creation date of theWinner)
end tell
on error
display notification "No session found."
return
end try
set theOffset to ((current date) - theStartTime)
set theTimeStamp to secondstoHMS from theOffset
if (theNote as text) = "" then
set theStamper to theTimeStamp
else
set theStamper to (theTimeStamp & " - " & (theNote as text))
end if
set theSafeDatematch to (do shell script "date -j -f '%A, %B %e, %Y at %I:%M:%S %p' '" & theStartTime & "' '+%b %d %H%M'")
do shell script ("echo " & quoted form of theStamper & " >> " & quoted form of (theSavePath & "Notes - " & theSafeDatematch & ".txt"))
end run
on secondstoHMS from theSeconds
tell theSeconds to return my pad(it div hours) & ":" & my pad(it mod hours div minutes) & ":" & my pad(it mod minutes)
end secondstoHMS
on pad(v)
return text -2 thru -1 of (v + 100 as text)
end pad
@jasonsnell
Copy link
Author

made variable theSavePath into a written-out path so you can customize it if you want, not hard coded to desktop.

@jasonsnell
Copy link
Author

@RandyWalker
Copy link

I can't seem to get this to work. I've changed line 14 to my desktop and I have a file with today's date (YYYYMMDD format) in the name located there.

Script Editor Replies:

tell application "Script Editor"
	get item 1
end tell
tell current application
	do shell script "date '+%Y%m%d'"
		--> "20220118"
end tell
tell application "Finder"
	get POSIX file "/Users/randy/Desktop/"
		--> error number -1728 from POSIX file "/Users/randy/Desktop/"
	get every file of alias "Macintosh HD:Users:randy:Desktop:" whose name contains "20220118"
		--> {document file "20220118.wav" of folder "Desktop" of folder "randy" of folder "Users" of startup disk}
	get creation date of document file "20220118.wav" of folder "Desktop" of folder "randy" of folder "Users" of startup disk
		--> date "Tuesday, January 18, 2022 at 18:34:22"
	get creation date of document file "20220118.wav" of folder "Desktop" of folder "randy" of folder "Users" of startup disk
		--> date "Tuesday, January 18, 2022 at 18:34:22"
end tell
tell current application
	current date
		--> date "Tuesday, January 18, 2022 at 19:12:56"
Result:
error "The variable theNote is not defined." number -2753 from "theNote"

If I run it from Terminal, I get this:

% osascript podcast-noter.scpt 'swear' 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] (1)MS]] ... 8, 2022 at 18:34:22'' using format ``%A, %B %e, %Y at %I:%M:%S %p''

Any advice? :)

@RandyWalker
Copy link

24-hour time strikes again! Your script is for 12-hour clocks, not 24. Once I switched my system to 12-hour time, it works just fine from Terminal and Stream Deck. (Still gives the same error in Script Editor thought!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment