Logging of running scripts - in smart rules, scripts menu

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 the log message command in your scripts

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.

Can’t you write that into the script itself?

Thanks, but I’m already using the indicators and logging.

The problem

  1. I applied a rule

  2. The script showed a dialog (Dialog Toolkit Plus via DT) and I decided to abort the execution of the script with a handled exception

  3. 	if theButtonPressed = "Abort" then
    		-- no: close theWindow to make review and handling easier
    		error "Abort handling of documents" number 1000
    	end if
    
  4. The indicator was hidden, but as seen in the screenshot, there’s no “Apply rule” and starting other script was not available.

    Somehow the script didn’t stopped.

The solution

Anyway, I fixed it by adding

  1. 		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

  2. 	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.

  1. property globalIsDev : true
    
  2. 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
    

Narg… Still a problem… Scripts are all greyed out.

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.

1 Like

Smart rules requiring user interaction are highly discouraged

Thanks for pointing that out.

@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

From the logs I can see, that the script finishes

But the script menu is not activated again.

I will try the same with 4.1. and 4.0: 4.0. and 4.1. show the same behaviour as 4.1.1

Not in this case but the smart rule isn’t really necessary, you could just use a smart group and a script processing selected items.

Is it still disabled after waiting more than 5 minutes? That’s the timeout DEVONthink uses for frozen smart rule scripts.

Is it still disabled after waiting more than 5 minutes?

No. After 5 minutes the menu is accessible again.

That’s the timeout DEVONthink uses for frozen smart rule scripts.

Any idea why it is frozen?

The log message is generated by this code. I would expect the script to be finished. Am I wrong? Any non closed handles?

I use

and

Can this be a problem?

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.

Mmmh… My code reaches the “end” of the “performSmartRule”-function.

theScriptHandler's globalLog("INFO", "Script finished", "script=" & quoted form of scriptName)

is run just before

end performSmartRule

I see this in the logs:

image

So, the method runs completely, but somehow DT thinks the Smart Rule is not finished.

Does it work without using the DialogToolkit? Switching to a regular menu script as suggested would probably solve all these issues.

When

	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.

Good morning, I recommend against doing that.

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. :man_shrugging:

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.