I’d like to fasten the filing of records by looking at tags of other records for file naming and tagging. I want for DTPO to ask me for the appropriate database for filling the record and then compare the record to similar records of the chosen database.
I’m struggling to compare a record in the Global Inbox to a chosen database using:
set theCompareList to compare record theRecord to theDatabase
The problem is that theDatabase seems to be ignored, only records of the database where theRecord is located are found. I say this since I’ve tried to move theRecord to another database and the comparison then uses that database instead.
Using the handler to select theDatabase results in:
A future release will support See Also/Classify across databases but right now it’s limited to the record’s database. But content can be compared to any database, therefore a workaround could look like this:
set theCompareList to compare content (plain text of theRecord) to theDatabase
thank you for the quick response. Sorry to say, the result is the same using
set theCompareList to compare content (plain text of theRecord) to theDatabase
or
set theCompareList to compare record theRecord to theDatabase
I get the same records, which are located in the Global Inbox, using both variants. The version of DTPO is 2.10.2, installed on MacOS High Sierra, 10.13.6.
Thank you for the interest and quick response, I do appreciate it!
Below, I display the name of the database sent to the handler “compareRecordForTags(theRecord, theDatabase)” so this seems to be correct. I also list the names of the found records, as you can see. I’ve tried with files located in the Global inbox as well as other inboxes of open databases. However, I always end up with the names of records in the database of the record used for comparison.
This is my humble code, sorry if it’s not that good. N.B.: not all handlers are included, but I think it’s not important for this case, please tell me otherwise and I can upload all of it):
tell application "DEVONthink Pro"
-- Get selected record for comparison
set theRecord to item 1 of (get the selection)
set allDatabases to every database
set theDatabase to selectDatabase(allDatabases) of me
set theTagsList to compareRecordForTags(theRecord, theDatabase) of me
end tell
(* THESE ARE THE USED HANDLERS *)
-- Handler to compare the record to get suggestion for file naming and tags. Save the records to theCompareList
on compareRecordForTags(theRecord, theDatabase)
tell application "DEVONthink Pro"
display dialog name of theDatabase as string
-- set theCompareList to compare record theRecord to theDatabase -- unable to compare the record to a given database. "A future release will support See Also/Classify across databases but right now it's limited to the record's database. But content can be compared to any database, therefore a workaround could look like this:"
set theCompareList to compare content (plain text of theRecord) to theDatabase
repeat with i from 1 to length of theCompareList
display dialog name of item i of theCompareList as string
end repeat
set theTagsList to {}
-- Collect tags of all compared files
repeat with i from 1 to length of theCompareList
set theRecordTags to tags of item i of theCompareList
repeat with j from 1 to length of theRecordTags
set the end of theTagsList to item j of theRecordTags
end repeat
end repeat
end tell
-- Get list sorted in decreasing order of frequency (of tags)
set theTagsList to item 1 of sortListByFrequency(theTagsList)
return theTagsList
end compareRecordForTags
-- Handler for selecting one (1) database from list of databases. Returns choosen database in listOfDatabases
on selectDatabase(listOfDatabases)
tell application "DEVONthink Pro"
-- Initiate list of database names
set theDatabaseNameList to {}
-- Parameter for keeping the index of the Databases
set theIndex to 0
-- Extract the names of the databases and save to list theDatabaseNameList
repeat with i from 1 to length of listOfDatabases
set the end of theDatabaseNameList to name of item i of listOfDatabases as string
end repeat
-- Make user choose from the list of databases
choose from list theDatabaseNameList with title "Select the database for comparison" with prompt "Please select the for comparison." --with multiple selections allowed
set chosenDatabaseName to the result as string
-- Search for index in theDatabaseNameList
repeat with i from 1 to length of listOfDatabases
if name of item i of listOfDatabases is chosenDatabaseName then
set theIndex to i
end if
end repeat
end tell
return item theIndex of listOfDatabases
end selectDatabase
I’m glad you’re taking pains to learn and understand AppleScript. Here is my trimmed down version of what you posted…
tell application id "DNtp"
log (id of current database as integer) -- Get the ID of the current database
set theRecord to item 1 of (get the selection)
choose from list (name of databases as list) with title "Select the database for comparison"
set chosenDatabaseName to the result as string
log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
compare content (plain text of theRecord) to database chosenDatabaseName
-- Check the returns to see what DB the results came from.
end tell
Yes, I know. That handler, inte turn, calls a handful of other handlers for sorting tags found in order of frequency using bubble sort and stuff like that. What I really aim for is using iOS to set filenames and tags using a nomenclature I use. As of now, I need to be on my Mac in order to do it. If I could use iOS in combination with Siri Shortcuts, iCloud Sync and DTTG, I’d be a happy man.
But, anyhow, I get the same result as you indicate using this code (comparison not made to wanted database). Any idea of how to proceed (except for rationalising the script?) :
tell application id "DNtp"
log (id of current database as integer) -- Get the ID of the current database
set theRecord to item 1 of (get the selection)
choose from list (name of databases as list) with title "Select the database for comparison"
set chosenDatabaseName to the result as string
log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
set theRecords to compare content (plain text of theRecord) to database chosenDatabaseName
set theNames to {}
repeat with i from 1 to length of theRecords
set the end of theNames to name of database of item i of theRecords
end repeat
get theNames
-- Check the returns to see what DB the results came from.
end tell
I know iOS isn’t as capable. I havent Figured out the details just yet. But I’m thinking of something in line with this:
My Always on Mac mini would do the ocr for incoming files to an iCloud hot folder and add to thefile to the global inbox. Then use the iOS device or the Mac to select what database to associate the record with. The mini would then do the comparison of the record and save a tag list containing candidates for author, kind of document (receipt, application, article, …), other tags, creation date (will likely be using Hazel before adding the DTPO. Save this data in a formatted way to the records comments. Use Siri Shortcuts to look into the comments and choose stuff from above and rename the record automatically and accordingly.
Something like that. I think it’s definitely doable. Wouldn’t you agree?
I tried to compare text to a database but only get results from the current database. Is comparing to a given database possible?
Edit: This workaround does what I’m looking for
tell application id "DNtp"
try
-- choose from list ...
--> Database_Name
open window for record root of database Database_Name
activate
set theResults to compare content theText
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
This doesn’t work over here. Without my workaround (first setting the current database by opening a new root window) the current database is used. Perhaps I’m missing something? It seems @bjorn and @BLUEFROG got this result too, see this old post
tell application id "DNtp"
log (id of current database as integer) -- Get the ID of the current database
set theRecord to item 1 of (get the selection)
choose from list (name of databases as list) with title "Select the database for comparison"
set chosenDatabaseName to the result as string
log (id of database chosenDatabaseName as integer) -- Get the ID of the chosen database.
compare content (plain text of theRecord) to database chosenDatabaseName
-- Check the returns to see what DB the results came from.
end tell
tell application id "DNtp"
try
set theDatabase to database "_temp"
set theResults to compare content "test" to theDatabase
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell