Script - Duplicate command - Duplicating indexed item to non-indexed location does not duplicate the underlying file

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
  1. Why are you processing the window class?
  2. Why are you using try blocks are this point in development?
  3. Duplicating an indexed file into a separate location doesn’t generate a new document. Duplicating into the same indexed location does.

These changes accomplish what you’re suggesting…

	set originalItemURL to reference URL of thisRecord
			set originalTitle to name of thisRecord
			set originalLocation to (location group of thisRecord)
			set originalReference to ("[" & originalTitle & "](" & originalItemURL as rich text) & ")"
			
			
			set newRecord to duplicate record thisRecord to originalLocation -- Duplicate in place to create a copy
			set name of newRecord to originalTitle -- Change the name to the original's name
			move into database record newRecord -- Consolidate the indexed file, importing it
			move record newRecord to theGroup -- Move to the selected location

If the script is invoked in a document window (not a navigation window).

I was having trouble with the script when the custom metadata value was blank. Looked around in the forums and it seems that trying to access empty metadata values errors. Ergo the try blocks to handle cases where the metadata is or is not filled.

Don’t know why it didn’t ocur to me. Thanks