I would like to use AppleScript to duplicate a record into a group.
I try the following code, but the “duplicate” command does not work.
tell application id "DNtp"
tell database "Practical"
set r1 to content "Model invoice"
set r2 to parent "Invoices 2022"
duplicate record r1 to r2
end tell
end tell
“Model invoice” is a Pages document.
“Invoices 2022” is a DEVONthink group.
The class of r1 is “record” whereas the class of r2 is “parent”. I assume that both r1 and r2 should be records for the “duplicate” command to work. Unfortunately, I can’t find the way to refer to a group as a record.
Any help would be most welcome. Thanks in advance. W.
You didn’t get the record and the group correctly. In Script Debugger this …
tell application id "DNtp"
tell database "_temp"
set r1 to content "Model invoice"
end tell
end tell
… returns “Invalide Object Reference” (and the same for your script’s next line).
Try this
-- Duplicate record "Model invoice" to group "Invoices 2022"
tell application id "DNtp"
try
set theRecord_NameWithoutExtension to "Model invoice"
set theGroup_Name to "Invoices 2022"
set theDatabase to database "_temp"
set theRecords to records of theDatabase whose name without extension = theRecord_NameWithoutExtension
if theRecords ≠ {} then
set theRecord to item 1 of theRecords
set theGroups to parents of theDatabase whose name = theGroup_Name
if theGroups ≠ {} then
set theGroup to item 1 of theGroups
duplicate record theRecord to theGroup
else
error "Couldn't find group \"" & theGroup_Name & "\""
end if
else
error "Couldn't find record \"" & theRecord_NameWithoutExtension & "\""
end if
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
return
end try
end tell
I was obviously out of my depth here, thinking in OO terms about AppleScript- that does not work. Apparently, the nested tells work as a hierarchy: if a property is not found in the inner tell block, it’s searched for in the outer one. Or something like that.
In fact, I was thinking about the duplicate call (message? – whatever). It looked to me as if it were called at the database, while it should’ve been called at the application. In JavaScript, that would be database.duplicate vs app.duplicate. AppleScript apparently looks from the innermost tell outwards until it finds an object that supports the duplicate method. Does that seem about right?
Not sure I understand. You’re probably mixing things up (command and property).
There’s only one way to use a command correctly, I think. There are no different notations, i.e. you can’t do something like
tell theRecord
duplicate record it to theGroup
end tell
because the command needs the application (tell application id "DNtp").
A property can be get and set in different ways. What notation one uses (of theRecord or theRecord's or tell theRecord) doesn’t make a difference. If the hierachy is right to get (or set) a property then it works. If the hierachy is messed up it fails.
We’re on the same page. Your „command“ is my „method“. I was derailed by the tell Application … tell record … duplicate … end tell … end tell sequence. To me, it looked as if the OP we’re trying to use the command/method duplicate with record. Hence my earlier remark.
You can’t tell the database to duplicate. The application controls this command. So you can talk to the database to get the information, but you need to do the duplication in the application tell block.
tell application id "DNtp"
tell database "29 - Renamed"
set r1 to content "0002.md"
set r2 to parent "New Group"
end tell
duplicate record r1 to r2
end tell
Nice and simple
The only issue I see with your script is the potential clash if you have more than one document or group with the same name in the database. However, if you’re controlling that, it should be no issue.