Question:
- Is there a time threshold for smart rules and/or batch processes after which the running AppleScript is stopped?
- Is there some more logging available using the log window? This would be very helpful debugging long running rules/batch processes
What I found out so far
- Running the script - it’s not complete, but uses some outsourced methods - I found that the smart rule and the batch process are stopped after some time without any error/alert- maybe 5-10 min
- The very same script run via Script Debugger ran for about 47 mins without any error or some other trouble - I stopped it manually, as I was about to go to bed.
- Each request to the
Environment
- I run phi4 with MstyStudio.app - best reproducible results for renaming files based on content
- I configured Ollama for DEVONthink
- Each “renaming” request (to the model) per record takes about 7-20 s
Next steps
- Try to test the existing renaming script - I was not happy about the results and needed a reusable solution I can embed into another script
Here’s the script - if required I post the other parts as well. I tried th
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property globalSuccessTags : {"auto-processed-rename-ai"}
property globalAiRequestTimeout : 120
property globalPrompt : "Generate a concise, descriptive filename for the following text.¬
# Rules/Expectations for the filename:¬
- Be 3-6 words maximum, no more than 50 letters¬
- Use only lowercase letters, numbers, and underscores¬
- Capture the main topic or purpose¬
- Be specific enough to identify the content¬
- if a company name is given always use it
- ignore city names in invoices
- Follow the format: topic_subtopic_descriptor (if applicable)¬
- Avoid generic terms at all costs like \"subscription\", \"license\", \"invoice\", \"rechnung\", \"document\", \"file\", or \"text\"¬
# Steps¬
1. Capture the main topic or purpose based on the text¬
2. Translate the captured text into German
2. Extract dates from the text - ignore ones before the date saved in the NOW-variable¬
3. Order dates from newest to oldest¬
4. Use the oldest date in YYYY-MM-DD notation¬
6. Generate the name in German for the file and prefix the name with the date¬
¬
# Expected output - Example¬
¬
```json¬
{¬
\"new_filename\": \"1970-10-01_this-is-the-file-name\"¬
}¬
```¬
# Variables
"
-- code to run within Script Debugger
tell application id "DNtp" to my performSmartRule(selected records)
-- ==========
-- test performSmartRule
property globalIsDev : true
-- copy template for smart rules
on performSmartRule(theRecords)
local libraryHandler
set libraryHandler to script "library-handling"
tell application id "DNtp"
libraryHandler's canRun(current database, theRecords)
end tell
local AiBase
set AiBase to libraryHandler's loadScript("ai-base", globalIsDev)
AiBase's SetupLocalAiRuntime()
local theScriptHandler
set theScriptHandler to libraryHandler's loadScriptSilent("document-rename", globalIsDev)
local thePrompt
set thePrompt to get globalPrompt of theScriptHandler
local aiRequestTimeout
set aiRequestTimeout to globalAiRequestTimeout of theScriptHandler
tell application id "DNtp"
-- phi4
-- display alert "Dokumente werden umbenannt"
theScriptHandler's ForAll(theRecords, globalIsDev, Ollama, "phi4", thePrompt, aiRequestTimeout)
-- display alert "Alle Dokumente wurden erfolgreich umbenannt"
end tell
end performSmartRule
-- Run job for all given records
on ForAll(theRecords, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
local titleProgressBar
set titleProgressBar to "Rename documents"
log "[INFO] Job started"
tell application id "DNtp"
show progress indicator titleProgressBar steps (count of theRecords) with cancel button
-- https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_error_xmpls.html
try
local theRecord
repeat with theRecord in theRecords
if cancelled progress then exit repeat
set theFilename to the filename of theRecord
step progress indicator theFilename
log "[INFO] Work on record " & theFilename
set modifiedRecord to my ForOne(theRecord, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
set tags of modifiedRecord to (get tags of modifiedRecord) & globalSuccessTags
end repeat
on error errStr number errorNumber
-- An unknown error occurred. Resignal, so the caller
-- can handle it, or AppleScript can display the number.
display alert errStr
error errStr number errorNumber
end try
hide progress indicator
end tell
log "[INFO] Job completed"
end ForAll
-- Run job for single record
on ForOne(theRecord, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
local newName
set newName to my GetNewNameLoop(theRecord, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
tell application id "DNtp"
set name of theRecord to newName
end tell
return theRecord
end ForOne
on GetNewNameLoop(theRecord, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
local newName
local repeatCounter
set repeatCounter to 1
repeat
try
set newName to my GetNewName(theRecord, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
exit repeat
on error errStr number errorNumber
-- nothing
end try
if repeatCounter ≥ 3 then
error "Repeat counter ≥ 3 for GetNewName(): " & errStr & ". Exit"
end if
set repeatCounter to repeatCounter + 1
end repeat
return newName
end GetNewNameLoop
on GetNewName(theRecord, localIsDev, theEngine, theModel, thePrompt, localAiRequestTimeout)
local AiBase
set AiBase to script "ai-base"
set now to current date
tell application id "DNtp"
-- Get date components
set currentYear to year of now
set currentMonth to rich texts -2 thru -1 of ("00" & ((month of now) as integer))
set currentDay to rich texts -2 thru -1 of ("00" & (day of now))
end tell
set currentDate to currentYear & "-" & currentMonth & "-" & currentDay as string
set thePrompt to thePrompt & "¬
CURRENT_DATE=" & currentDate
-- log "[DEBUG] Validate chosen AI engine"
-- set theEngine to AiBase's ValidateAndReturnEngine(theEngine)
local theResponse
set theResponse to AiBase's AskModelTimeout(theRecord, theEngine, theModel, thePrompt, "JSON", localAiRequestTimeout)
if theResponse is missing value then
log "[WARN] No (valid) result from AI"
return missing value
end if
return new_filename of theResponse
end GetNewName


