Today, I encountered a situation where I was unable to run scripts because the menu was greyed out. From other discussions, I know this happens when another script is already running. Unfortunately, I couldn’t determine which script or automation was currently active.
It would be helpful to have a logging option that tracks:
Smart rule executions
Script runtime duration and results
External requests to AI models (in addition to Chat.log)
Other automation processes that prevent scripts from running
You can use a progress indicator to show that a script is running. In DT3 the commands are show progress indicator, step progress indicator and hide progress indicator. I don’t know if they changed in DT4, check the scripting dictionary.
if errNum = 1000 then
my globalLog("WARN", quoted form of "Job execution aborted by user", "error=1000 count_of_records=" & len)
return
end if
to
on error errMsg number errNum partial result partialError
tell application id "DNtp"
hide progress indicator
end tell
if errNum = 1000 then
my globalLog("WARN", quoted form of "Job execution aborted by user", "error=1000 count_of_records=" & len)
return
end if
set AppleScript's text item delimiters to {return}
-- An unknown error occurred. Resignal, so the caller
-- can handle it, or AppleScript can display the number
display alert errMsg & ("Error number: ") & errNum & return & (partialError as text)
error errMsg & ("Error number: ") & errNum & return & (partialError as text)
end try
I’m not sure, where the script execution got stuck.
Side note
Logging is done with a convenience method handling DEBUG messages as well.
property globalIsDev : true
on globalLog(level, msg, msgInfo)
if msgInfo is missing value then
set logMsg to "msg=" & msg
else
set logMsg to "msg=" & msg & " " & msgInfo
end if
if level is not "DEBUG" then
tell application id "DNtp"
log message "level=INFO " & logMsg
end tell
return
end if
if globalIsDev is true then
tell application id "DNtp"
log message "level=DEBUG " & logMsg
end tell
end if
end globalLog
Smart rules requiring user interaction are highly discouraged as smart rules are supposed to run without user attention automatically and quickly. And a smart rule waiting for user input blocks all other rules in the meantime.
@cgrunenberg Even for “On Demand” smart rules? I’ve got a fixed subset of documents I want the the script to work on. A “workaround” would be using a smart group + a normal script from the contextual menu.
BTW: Running the same code as “Script” works fine. I can start the script again. If I add the same startup code to an “On Demand” smart rule, the script sections is still greyed out after the script finishes. Any idea?
-- test performSmartRule
property globalIsDev : false
-- copy template for smart rules
on performSmartRule(theRecords)
local libraryHandler
set libraryHandler to script "library-handling"
tell application id "DNtp"
set currentDb to current database
end tell
libraryHandler's canRun(currentDb, theRecords)
local scriptName
set scriptName to "document-invoices-handle"
local theScriptHandler
set theScriptHandler to libraryHandler's loadScript(scriptName, globalIsDev)
theScriptHandler's globalLog("INFO", "Run script", "script=" & quoted form of scriptName)
theScriptHandler's ForAll(theRecords, globalIsDev)
theScriptHandler's globalLog("INFO", "Script finished", "script=" & quoted form of scriptName)
end performSmartRule
Impossible to tell without your setup and databases. The easiest way to figure this out is probably to add extensive logging after each important step or command.
set {buttonPressed, controlsResults} to display enhanced window "Set metadata for file" acc view width accViewWidth acc view height theTop acc view controls allControls buttons theButtons initial position {} giving up after 0 with align
is run, the smart rule never ends. I rebuild my setup based on your suggestions.
Not sure if this would really make a difference but you could also try to add explicit tell application id "DNtp" ... end tell blocks enclosing the usage of DialogToolkit.
I had some weird issues in the last 2 weeks: Dialog Toolkit Plus failed to initialise from time to time when it ran from within DT (smart rule) with an error complaining about not being the first thread. I fixed this by NOT wrapping it in tell application id "DNtp" ... end tell. Running the same code from Script Debugger never failed.
This was the first line of Dialog Toolkit Plus in my code. At this line the error occured.
set {theButtons, minWidth} to create buttons {"Delete", "AI Rename", "Redo", "Abort", "Ignore", "Move"} default button "Ignore" without equal widths
This is most likely caused by the fact that smart rule scripts are usually executed in the background and are not supposed to require user input. Versions 4 uses a separate process to ensure that buggy scripts can’t freeze or crash the app or cause other sideeffects (e.g. after internal exceptions) anymore.