I didn’t try to reproduce anything with new databases, but I realized that I also have records that include the file extension in their name.
(A rough estimate is 10-20%. I have “Show filename extensions” enabled in Settings > Appearance and have kept it like that for a long time.)
Since I never noticed, I guess this hasn’t been much of an issue for me. While it does in theory affect all searches, I imagine it mostly becomes a problem for name ends with queries? Or maybe we use search in different ways.
Setting a record’s name to it’s name without extension using AppleScript does indeed seem to fix it. Much preferable to my “alternative search” script above.
Your example looks fine to me! It’s kinda crazy how scriptable DEVONthink is, so I encourage you to explore.
If you’re new to AppleScript, one of the things you’ll want to learn about is the whose clause – an extremely efficient way to get application objects matching a certain condition. (Or multiple, you can make pretty complex queries if you want to.)
The offical AppleScript Langauge Guide defines it as the “filter reference form” of constructing an “object specifier”.
Using whose, we can quickly find all records affected by this issue:
tell application id "DNtp"
set badNames to (contents of every database whose name ≠ name without extension)
end tell
And fix them like this:
tell application id "DNtp"
repeat with theDatabase in databases
set badNames to (contents of theDatabase whose name ≠ name without extension)
repeat with theRecord in badNames
set theRecord's name to (theRecord's name without extension)
end repeat
end repeat
end tell
Now, adding some log messages:
-- Fix record names that include their file extension in all open databases
tell application id "DNtp"
repeat with theDatabase in databases
set badNames to (contents of theDatabase whose name ≠ name without extension)
set badCount to (count of badNames)
if badCount = 0 then
log message ¬
"0 record names that include their file extension." info (name of theDatabase)
else
repeat with theRecord in badNames
set theRecord's name to (theRecord's name without extension)
end repeat
log message "Fixed " & badCount & ¬
" record names that included their file extension." info (name of theDatabase)
end if
end repeat
end tell
PS: You can also use JavaScript to work with DEVONthink, by way of Apple’s JavaScript for Automation (JXA). It interfaces with the same underlying scripting architecture, though the implementation is not 1:1 with AppleScript… and it has some bugs. The two have different strengths and weaknesses. You might prefer JS if you already know it or have other programming experience.
I took a little while to get back to this thread because I tried to write the above in JXA and couldn’t make it work. Must be a bug. The JXA implementation of whose doesn’t seem to work for comparing two properties of an object.