Recently heard David and Rose talk about mixing automation tools and was working on some Applescript so I decided to try it.
I’d been getting a formatted date/time using shell commands from within Applescript. I was finding that this would randomly misbehave (stall and timeout). So I created a simple Shortcut to replace the handler that calls the shell script (link to shortcut attached). Seemed to work OK over the weekend when I built it. Yesterday and today I seem to be running into the same situation I had with the script.
Specifically, the script I have built that calls log_event from Hazel would not run. So I have been attempting to debug using ScriptDebugger. I found that when I would step into the handler that getFormattedCurrentDateTime is stalling. I then went to the Shortcuts app and ran the shortcut and it worked just fine. Then magically the Applescript started to work again.
Perhaps there is something about my scripts that causes these issues. Perhaps something about my Mac. I am using a 2021 16" M1 MacBookPro and I installed MacOS 12.5 before I built getFormattedCurrentDateTime.
Obviously automations that are not reliable are really troublesome. I would think that using Apple’s own tools would be the lowest risk. But I have had problems like on & off for years.
Here is my Shortcut
https://www.icloud.com/shortcuts/84959e0ab7414a538aa3bae2464933db
The handler that calls the shortcut is getFormattedCurrentDateTime
on getFormattedCurrentDateTime()
tell application "Shortcuts"
local currentDateTime, currentDateTrimmed
set currentDateTime to run shortcut "Get formatted current date-time"
-- We can do this because we know we are only getting a single date-time
set currentDateTime to contents of currentDateTime as string
set currentDateTimeTrimmed to my trimWhiteSpaceFromEnds(currentDateTime)
return currentDateTimeTrimmed
end tell
end getFormattedCurrentDateTime
getFormattedCurrentDateTime is being called by the handler log_event, I use it to write out log files from my Applescripts. The first try block starts with a call to getFormattedCurrentDateTime and this is where things hang up, sometimes.
type or paste code here
on log_event(GeneralLogFilePosixPath, theMessage)
-- It is assumed that the scripts calling this function determine if/when/where to write out a log.
-- The logfiles include the date and the the POSIX path provided into this function. The log
-- entry includes the date/time of the log entry and the message passed into the function.
local theDateTimeString, theLogString, theLogFilePath, fileExists, positionString, theLogFile
try
set theDateTimeString to my getFormattedCurrentDateTime()
set theLogString to theDateTimeString & " " & theMessage & "\n"
set theLogFilePath to my getLogFilePath(GeneralLogFilePosixPath)
set fileExists to my doesFileExist(theLogFilePath)
if fileExists then
set positionString to "EOF"
else
set positionString to "TOP"
end if
set wasWritten to my writeToFile(theLogFilePath, positionString, theLogString)
if not wasWritten then
set errMsg to "Logging:log_event: The log file " & theLogFilePath & " did not get written with " & theLogString
my throwError(errMsg)
end if
return wasWritten
on error theError
try
set theDateTimeString to my getFormattedCurrentDateTime()
set theNewMessage to "CTlib:log_event: Error (" & theError & ") writing a log entry out to" & theLogFilePath & "the entry was " & theLogString
set theLogFilePath to my getLogFilePath(GeneralLogPOSIXPath)
set theLogFile to my writeToFile(theLogFilePath, "EOF", theNewMessage)
on error theNewError
set errorMessage to "UNKNOWN ERROR IN CTlib:log_event"
display alert theNewError message errorMessage
end try
return false
end try
end log_event