Short version:
User of DEVONthink 3.
I made a script using Claude. It inserts the filename and the group path inside the RTF document and it can work with multiple selected RTFs at once. It preserves metadata and item links.
I am not sure if it is still good, although it does its job. I would appreciate if someone could take a look at the attached code and if s/he thinks it misses something or alter any functionality of DEVONthink to tell us.
Long story:
I needed a more automated way to insert the filename and the group path inside my rtf notes. I use groups for provenance and tags for topics. I don’t rely on “classify&see also” function (ok. I do like the see also recommendations, but it’s not a big deal if those inserted info mess up a bit with some of the results, it’s not actually that bad), but I do really need to have such info available. I’m an academic, doing research and for my workflow it is very important to be able to tell the source just by looking at the file while being in its topical tag(s). That’s why the group path is important -which is actually mirroring the structure of an archive.
With my nearly non-existing programming capabilities, I started crafting a script to automatically insert the group path and file name in the rtf’s text. Then I realised that this script couldn’t work with multiple selected records. So, I used claude to finalise the script. The first one worked ok, but I noticed it was erasing all the metadata property fields of the rtf files. Then I noticed, that the link I had to another document removed, and as I was trying to write that script using claude, I kept finding new issues and then try and see what works and what not. The last one seems to be fine. It can actually take the filename and group path and insert them on the first two lines. It also preserves the metadata and the links.
My workflow is quite restricted to basics. I don’t -yet- use all the power that DEVONthink has to offer, but I cannot exclude that in the future I won’t. While trying and running all these scripts from claude I saw things that were missing, or went wrong. The final version seems to be ok. But because I don’t have the necessary knowledge to comprehend this script, it would be so much helpful if someone could take a second look on the script, see its structure and if s/he thinks is anything wrong or that it will mess up with some other functionality in DEVONthink, just to point it out.
Thank you so much for your help, I’ve been reading this forum since the 2.xx version of the software!
Here’s the code:
-- Insert File Name and Group Path into RTF Note in DEVONthink 3
-- Works directly on the raw RTF file to preserve metadata, links and annotations
on textToRTF(theText)
set rtfText to ""
repeat with i from 1 to length of theText
set c to character i of theText
set codePoint to id of c
if codePoint is 127 then
set rtfText to rtfText & "\\\\uc0\\\\u" & codePoint & " "
else if c is "\\\\" then
set rtfText to rtfText & "\\\\\\\\"
else if c is "{" then
set rtfText to rtfText & "\\\\{"
else if c is "}" then
set rtfText to rtfText & "\\\\}"
else
set rtfText to rtfText & c
end if
end repeat
return rtfText
end textToRTF
tell application id "DNtp"
set theRecords to selection
if theRecords is {} then
display dialog "Please select at least one RTF record first." buttons {"OK"} default button "OK" with icon caution
return
end if
repeat with theRecord in theRecords
if (type of theRecord) is RTF then
-- Get name, strip extension only if there is one
set docName to name of theRecord
set AppleScript's text item delimiters to "."
set nameParts to text items of docName
set AppleScript's text item delimiters to ""
if (count of nameParts) is 1 then
set AppleScript's text item delimiters to "."
set docName to text items 1 thru -2 of docName as string
set AppleScript's text item delimiters to ""
end if
-- Get group path and file path
set groupPath to location of theRecord
set thePath to path of theRecord
-- Read full RTF file
set fileRef to open for access thePath
set rtfContent to read fileRef
close access fileRef
-- Convert name and path to RTF-safe encoding (handles Greek and special chars)
set rtfName to my textToRTF(docName)
set rtfPath to my textToRTF("Path: " & groupPath)
-- Build RTF insert: name, path, blank line
set rtfInsert to rtfName & "\\\\par " & rtfPath & "\\\\par\\\\par "
-- Insert after partightenfactor0 marker
set AppleScript's text item delimiters to "partightenfactor0"
set rtfParts to text items of rtfContent
set AppleScript's text item delimiters to ""
if (count of rtfParts) is 1 then
set newRTF to (item 1 of rtfParts) & "partightenfactor0" & return & rtfInsert & (items 2 thru -1 of rtfParts as string)
set fileRef to open for access thePath with write permission
set eof fileRef to 0
write newRTF to fileRef
close access fileRef
end if
end if
end repeat
display dialog "Done! File name and group path inserted in all selected RTF records." buttons {"OK"} default button "OK"
end tell



