I would like to share a script for a smart rule in DEVONthink Pro. Perhaps someone else has similar requirements and can benefit from it.
Broader Applicability:
This script can be generally useful for any scenario where external data analysis (i.e., not in DEVONthink Pro) involves a large number of PDF files, and it is helpful to reference the original file in DEVONthink Pro from a table of analyzed data.
For example, it could be used for scientific research where PDF files are analyzed externally (text-based analysis only) and the original PDF files need to be referenced later, or for legal case management where documents are reviewed and annotated externally, and the original documents in DEVONthink need to be accessed.
Ideally, I would prefer not to need the workaround described below with Google’s new “NotebookLM”. If AI functions are indeed coming to DEVONthink Pro, I hope for the ability to apply a prompt to selected PDFs and have the analysis table automatically output in DEVONthink’s table function (.tsv). This would likely eliminate the need for external data analysis tools or AI tools in many cases.
My Example Process:
I upload invoices and bank statements to Google’s “NotebookLM,” which analyzes them and, with a detailed prompt, provides me with a detailed table for my personal accounting in seconds, exactly how I need it. Since “NotebookLM” creates closed AI notebooks, it only uses the provided data. If the source data does not yield any results during the analysis, the AI simply outputs no result, and the source data would need to be completed first (if desired) or the prompt must be adjusted accordingly to handle missing data in the results table.
Thus, there is no annoying information noise or hallucinations like with ChatGPT. The provided sources behave like in a small, closed ecosystem. The table generated from the analysis is available in Markdown format, so I can retrieve and process it from “NotebookLM” in seconds.
My Previous Problem:
Since “NotebookLM” converts PDFs to text for analysis, I could not easily access the original invoice file later. It doesn’t help to have the callback URL in the file metadata or Finder comments. As a result, I had to manually find the original invoice file in DEVONthink based on the table data if I wanted to look up something in the original PDF.
Losing time at this stage, even though I could now handle my previously manual process in seconds with NotebookLM, was something I wanted to avoid.
Solution:
I described my problem to an AI and received this AppleScript, which I integrated into a smart rule. This rule has only one condition, “Kind is group,” and is triggered manually when I select one or more groups. The smart rule then starts this AppleScript.
Shortly after, I have a folder on my desktop with the PDFs of all previously selected groups. Each of these helper PDFs for “NotebookLM” has an additional page with the callback URL for the original file in DEVONthink, thanks to the AppleScript. “NotebookLM” then creates an additional column in my table for the link to my original file in DEVONthink.
Since the script is an AI result, it may be optimized further to achieve the same outcome. I haven’t checked it in that regard as it meets my requirements.
For those concerned about data privacy:
“NotebookLM” states that it does not use uploaded data for AI training purposes, only potentially for program optimizations. This makes sense to me, as only data that you upload yourself can be analyzed. The AI does not refer to other sources in its analysis.
But of course, I would feel more secure if I could eventually map this data analysis process entirely within my favorite program, DEVONthink Pro…
on performSmartRule(theRecords)
tell application id "DNtp"
-- Wählen Sie die aktuell ausgewählten Gruppen
set theSelection to the selection
if (count of theSelection) is less than 1 then
display dialog "Bitte wählen Sie mindestens eine Gruppe aus." buttons {"OK"} default button "OK"
return
end if
-- ĂśberprĂĽfen Sie, ob die Auswahl Gruppen sind
repeat with theGroup in theSelection
if (type of theGroup) is not group then
display dialog "Bitte wählen Sie nur Gruppen aus." buttons {"OK"} default button "OK"
return
end if
end repeat
-- Fragen Sie nach dem Namen des zu kopierenden Ordners
set exportGroupName to text returned of (display dialog "Name des zu kopierenden Ordners:" default answer "Unbenannte Gruppe")
-- Fragen Sie nach dem Speicherort im Finder (Standard: Desktop)
set defaultPath to path to desktop folder as string
set targetFolder to choose folder with prompt "Wählen Sie den Speicherort für den Ordner:" default location alias defaultPath
-- Erstellen Sie den neuen Ordner im Finder
set targetPath to (POSIX path of targetFolder) & "/" & exportGroupName
do shell script "mkdir -p " & quoted form of targetPath
-- Schleife durch alle ausgewählten Gruppen
repeat with theGroup in theSelection
-- Holen Sie alle Dateien in der Gruppe
set theRecords to children of theGroup
repeat with theRecord in theRecords
if (type of theRecord) is PDF document then
-- Holen Sie die UUID des PDF-Dokuments
set theUUID to uuid of theRecord
set callbackURL to "x-devonthink-item://" & theUUID
-- Erstellen Sie ein neues HTML-Dokument mit der Callback-URL als Link
set htmlContent to "<html><body><p>Click the following link to open in DEVONthink:</p><p><a href=\"" & callbackURL & "\" style=\"color:blue;text-decoration:underline;\">" & callbackURL & "</a></p><p>If clicking the link does not work, right-click (or control-click) the link and select 'Open Link'.</p></body></html>"
set newHTMLRecord to create record with {name:"Callback URL", type:html, source:htmlContent} in (parent 1 of theRecord)
-- Konvertieren Sie das HTML-Dokument in ein PDF
set newPDFRecord to convert record newHTMLRecord to PDF document
-- FĂĽgen Sie das neue PDF am Ende des Original-PDFs an
set mergedPDF to merge records {theRecord, newPDFRecord}
-- Benennen Sie das neue PDF um
set originalName to name of theRecord
set name of mergedPDF to originalName & "_UUID"
-- Exportieren Sie das neue PDF in den Zielordner
set exportPath to targetPath & "/" & (name of mergedPDF)
export record mergedPDF to POSIX path of targetPath
-- Löschen Sie das temporäre HTML-Dokument und das konvertierte PDF
delete record newHTMLRecord
delete record newPDFRecord
-- Löschen Sie das zusammengeführte PDF in DEVONthink
delete record mergedPDF
end if
end repeat
end repeat
-- Löschen der DEVONtech_storage Datei (falls vorhanden)
do shell script "rm -f " & quoted form of (targetPath & "/DEVONtech_storage")
display dialog "Die Gruppe wurde erfolgreich kopiert." buttons {"OK"} default button "OK"
end tell
end performSmartRule