- The question
- Is there a better way to show a “review window”?
- Does it make sense to ask for a display dialog (AppleScript) where I can
- view the content of the file
- see the new filename, attributes and tags - e.g.
display review "DNtp" with new name "asdf" new tags {"tag1", "tag2"} in theNewLocation buttons {"Abort", "Cancel", "Apply"}
- The Setup
- I’ve got a directory which can contain a lot files - invoices, health records, …
- I organise my files by locations - tags are only used for some specific use cases - find files for tax applications, or find target locations
- Each document is handled by some rules I defined early - e.g. content, kind, tags
- Aim
- Based on the result, I want to get the target location for each document and some more specific tags
- Before I apply the changes, I would like to get an idea what the document is about
- The relevant code
- For now, I open a new window for the record
- Show an “Alert”
- Handle the response
- Close the window
-- [...]
set theWindow to open window for record theRecord
local alertResponse
set alertResponse to display alert "DNtp" message "File: " & theName & "
Targent path: " & theTargetPath & "
Database: " & theDatabase & "
Tags: " & my convertListToString(theResultTags, ", ") buttons {"Abort", "Ignore", "Move"} default button 2 as warning
close theWindow
-- [...]
- The script
Based on other posts in this forum, I made this script not being cached. This is intentional.
set theScriptPath to (system attribute "HOME") & "/Library/Script Libraries/document-invoices-handle.scpt"
set ScriptHandler to load script (POSIX file theScriptPath as alias)
Here comes the “full” script whithout my specific rules.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
-- test performSmartRule
tell application id "DNtp" to my performSmartRule(selected records)
-- test scriptOutput
-- tell application id "DNtp" to my scriptOutput(selected record 1, "")
return
on scriptOutput(theRecord, theInput)
tell application id "DNtp"
local theScriptPath
set theScriptPath to (system attribute "HOME") & "/Library/Script Libraries/document-invoices-handle.scpt"
local ScriptHandler
set ScriptHandler to load script (POSIX file theScriptPath as alias)
ScriptHandler's ForOne(theRecord)
end tell
end scriptOutput
on performSmartRule(theRecords)
tell application id "DNtp"
local theScriptPath
set theScriptPath to (system attribute "HOME") & "/Library/Script Libraries/document-invoices-handle.scpt"
local ScriptHandler
set ScriptHandler to load script (POSIX file theScriptPath as alias)
ScriptHandler's ForAll(theRecords)
end tell
end performSmartRule
-- Run job for all given records
-- Uses default model for given engine
on ForAll(theRecords)
tell application id "DNtp"
if the length of theRecords is less than 1 then error "One or more record must be selected in DEVONthink."
show progress indicator "Run job for record" steps (count of theRecords)
repeat with theRecord in theRecords
set theName to the name of theRecord
step progress indicator theName
my ForOne(theRecord)
end repeat
hide progress indicator
end tell
log "[INFO] Job completed."
end ForAll
-- Run job for single record
-- Uses default model for given engine
on ForOne(theRecord)
tell application id "DNtp"
local thisYear
set thisYear to year of (get creation date of theRecord) as string
local theResult
set theResult to my filterDocument(theRecord, thisYear)
local theResultTags
set theResultTags to tags of theResult
if theResultTags is not missing value then
set tags of theRecord to (tags of theRecord & theResultTags)
end if
if (company of theResult) is not missing value then
add custom meta data company of theResult for "company" to theRecord
end if
if (basePath of theResult) is not missing value and (database of theResult) is not missing value then
local theTargetPath
set theTargetPath to (basePath of theResult & "/" & thisYear)
local theDatabase
set theDatabase to (database of theResult)
local theTarget
set theTarget to get record at theTargetPath in database theDatabase
if theTarget is missing value then
local theParent
set theParent to get record at (basePath of theResult) in database theDatabase
set theTarget to create record with {record type:group, name:thisYear} in theParent
end if
local theName
set theName to name of theRecord
set theWindow to open window for record theRecord
local alertResponse
set alertResponse to display alert "DNtp" message "File: " & theName & "
Targent path: " & theTargetPath & "
Database: " & theDatabase & "
Tags: " & my convertListToString(theResultTags, ", ") buttons {"Abort", "Ignore", "Move"} default button 2 as warning
close theWindow
if get button returned of alertResponse = "Ignore" then
return
end if
if get button returned of alertResponse = "Abort" then
error "Abort handling of documents"
end if
log "[DEBUG] Name of file: " & theName
log "[DEBUG] Target Path: " & theTargetPath
log "[DEBUG] Database: " & theDatabase
log "[DEBUG] Tags: " & my convertListToString(theResultTags, ", ")
move record theRecord to theTarget
end if
end tell
return theRecord
end ForOne
on filterDocument(theRecord, thisYear)
tell application id "DNtp"
local theName
set theName to name of theRecord
local theContent
set theContent to plain text of theRecord
local theKind
set theKind to kind of theRecord
local theTags
set theTags to tags of theRecord
if theContent contains "company XY" and theContent contains "1234567" then
return {basePath:"/Finance/Company XY/kontoauszuege", database:"business", tags:{"finance"}, company:"Company XY"}
end if
-- [ ... ]
if theTags contains "health" then
return {basePath:"/Health", database:"business", tags:missing value, company:missing value}
end if
return {basePath:"/Receipts", database:"business", tags:missing value, company:missing value}
end tell
end filterDocument
on setTags(theRecord, theNewTags)
tell application id "DNtp"
set theTags to tags of theRecord
set tags of theRecord to theTags & theNewTags
end tell
end setTags
on setCompany(theRecord, theName)
tell application id "DNtp"
add custom meta data theName for "company" to theRecord
end tell
end setCompany
on convertListToString(theList, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to ""
return theString
end convertListToString