A Script to Append Work Activity to a Log File
I used to have an AppleScript that would prompt me for some text and then append that to a file with a date and time stamp. The purpose of this script was to allow me to quickly log stuff that I was working on throughout the day. I launched the script with a keyboard mapping using FastScripts. I think I had it set to ⇧ + ⌘ + L.
At some point over the years and a couple of new computers later, I lost the script I found a copy of the script and recently became interested in adding this back into my workflow. Before I found it though, I used Kagi’s Quick Answer feature to cobble together a new version of the script.
set filePath to "Macintosh HD:Users:kevin:Documents:log.txt"
set isoDate to do shell script "date +'%Y-%m-%d %I:%M %p'"
try
-- Try to open the file for access
set fileRef to open for access file filePath with write permission
-- If successful, do something with the file
set userInput to ""
repeat until userInput is not ""
set userInput to text returned of (display dialog "What did you do?" buttons {"Cancel", "OK"} default button 2 default answer "")
end repeat
write return & return & "--- " & return & isoDate & return & return & (ASCII character 9) & userInput to fileRef starting at eof
on error errMsg number errNum
if errNum = -49 then
-- If the error is that the file is already open
close access file filePath -- Close the existing access
-- Now reopen the file
set fileRef to open for access file filePath with write permission
set userInput to ""
repeat until userInput is not ""
set userInput to text returned of (display dialog "What did you do?" buttons {"Cancel", "OK"} default button 2 default answer "")
write return & return & "--- " & return & isoDate & return & return & (ASCII character 9) & userInput to fileRef starting at eof
end repeat
else
-- Handle other errors
display dialog "An error occurred: " & errMsg
end if
end try
-- Close the file when done
close access fileRef
This is an improvement over my original script as it has better error handling and checks if the file is already open. I’m using Raycast as my launcher now and I’ve set it up to run this script by pressing the Hyper Key1 + L. I’m hoping to start using this regularly so I can have a good record of my work activites.
I still have the original log file that has entries going back 14 years, and it’s interesting to see what I was working on back then.
-
I mapped mine to the Caps Lock key. ↩︎