I’m trying to create a script that duplicates selected records to a new location, filling custom metadata to link the new item and the duplicated one.
The selected records are sometimes indexed files and other times they’re imported files. The target destination is normally an imported group, in a separate database.
However I found out that duplicating an indexed record to an imported location creates a duplicate record, but doesn’t duplicate the underlying file. The new record instead points to the same indexed file, and is an indexed record itself.
How can I change the script so that the new duplicated record is attached to a new duplicated file and is imported, if created on an imported group?
-- Duplicate record(s) and make original canonical
tell application id "DNtp"
try
set windowClass to class of window 1
if windowClass = viewer window then
set theRecords to selection of window 1
else if windowClass = document window then
set theRecords to {content record of window 1}
end if
if theRecords = {} then error "Nothing selected."
set theGroup to display group selector "Duplicate in:"
set theGroupsDatabase to database of theGroup
repeat with thisRecord in theRecords
set originalItemURL to reference URL of thisRecord
set originalTitle to name of thisRecord
set originalReference to ("[" & originalTitle & "](" & originalItemURL as text) & ")"
set newRecord to duplicate record thisRecord to theGroup
add custom meta data originalReference for "doc_original" to newRecord
add custom meta data "" for "docs_juntado_em" to newRecord
set newRecordURL to reference URL of newRecord
set newRecordTitle to name of newRecord
set newRecordReference to ("[" & newRecordTitle & "](" & newRecordURL as text) & ")"
set originalMetadata to (custom meta data of thisRecord)
-- Se tiver MD de Juntado em, adiciona novo record à lista
try
set originalDocsJuntadoEm to mddocs_juntado_em of originalMetadata
set newJuntadosEm to originalDocsJuntadoEm & return & newRecordReference
-- Se não tiver MD de Juntado em (ie on error), inclui novo record
on error
set newJuntadosEm to newRecordReference
end try
add custom meta data newJuntadosEm for "docs_juntado_em" to thisRecord
try
set originalCanonico to mddoc_canonico of originalMetadata
set userChoice to button returned of (display alert "Record already has canonical doc." message "Are you sure you want to update the canonical file to the duplicated record?" buttons {"Cancel", "Yes"} default button "Cancel")
if userChoice is "Yes" then
-- Perform delete action
set docCanonico to originalReference
else
set docCanonico to originalCanonico
end if
on error
set docCanonico to originalReference
end try
add custom meta data docCanonico for "doc_canonico" to thisRecord
add custom meta data docCanonico for "doc_canonico" to newRecord
end repeat
display notification "Duplicated"
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