Script to import files from CSV index?

Please forgive the uselessness of this question. I’ve been using DT for some time now but never tried to get on top of the scripting abilities, and now with custom metadata they have suddenly become potentially very attractive. I suspect there is something out there that is 70-80% of what I need but some basic searching has failed to turn it up so I thought I’d ask.

Suppose I have a CSV list of files, which includes the filename, and various metadata items for each file. I assume that if I knuckle down and learn how all this works I could write an AppleScript to iterate through the list and import the files with the metadata appropriately. But does anyone know of something out there already along those lines that I could tweak, rather than working it out from scratch? (Coding is not my day job, and not even the hobby it used to be …)

thanks!

An example CSV would be helpful.

File - Import - Files and Folders works out of the box to import a CSV file.

What do you wish to do with the data from there?

1 Like

Import files by using the filenames for them that are in the CSV data, if I understand the original post correctly. :slightly_smiling_face:

OK I didn’t read that closely enough the first time

Are these files located randomly, or are they together in a folder somewhere?

WIll this be an ongoing process or just once to set up a database?

If they are located in the same folder or in a small number of folders, you can fairly easily Index those folders and then if desired choose to import the indexed files into the database.

Many thanks for the replies. The files would be in a folder. The catch is that I want to import them and set metadata for each imported flile per other items in the same CSV entry. So for example it might look like this (not replicating CSV coding):

File path | Document name | Custom metadata 1 | Custom metadata 2 | Custom metadata 3
docs/doc1.pdf | “Memo from Fred” | Apple | Guns | 3 January 2016
docs/doc2.pdf | “Directions to mountain” | Banana | Rope | 2 August 1958

etc

  1. The file path is incomplete in the example.
  2. The filename and the Document Name are not the same here. You can’t set the title of a PDF as it’s read only. However, if the PDF has the title in place, it will be part of the import.
  3. You need to define what the custom metadata names are.

Here is my sample CSV in DEVONthink…

Here is a simple teaching edition snippet.

set docsPath to "~/Desktop/"
-- I am using this variable as your example text showed an incomplete path.
-- Using the variable above would import from files on my desktop.
-- Using a full path in the CSV could supersede this method.

tell application id "DNtp"
	set doc to (item 1 of (selection as list)) -- Handling a csv file selected in DEVONthink
	
	-- Get the header names and set variables for the meta data names
	set docHeaders to (columns of doc)
	set md1 to (item 2 of docHeaders) as string
	set md2 to (item 3 of docHeaders) as string
	
	set docContents to (cells of doc) -- Get the contents of the cells in the file
	
	repeat with csvItem in docContents
		set newRecord to import (docsPath & (item 1 of csvItem)) to current group -- Import the file
		
		-- Add the custom metadata
		add custom meta data (item 2 of csvItem) for md1 to newRecord
		add custom meta data (item 3 of csvItem) for md2 to newRecord
	end repeat
end tell
1 Like

That’s fantastically helpful – thank you very much indeed.

1 Like

You’re very welcome :slight_smile:

@Bluefrog I solved the custom keywords to tags issue with a batch process & placeholder! Worked well! I have a new question about the import script: Script to import files from CSV index? - #7 by BLUEFROG.

Each time I use this, the script–unless I am mistaken–creates a new set of custom metadata. I’d like to keep the meta data names the same for each collection (csv file) I am importing. Any suggestions on how I might do that? Does it have to do with the custom meta data settings? Thanks so much!

I solved the custom keywords to tags issue with a batch process & placeholder! Worked well!

Nice! Batch processing gets forgotten under the glaring spotlight people put on smart rules, but it’s very cool tech for one-off or infrequent tasks.

I’d like to keep the meta data names the same for each collection (csv file) I am importing.

What meta data names in what collections ?

PS:

add custom meta data (item 2 of csvItem) for md1 to newRecord

Only creates a custom attribute if it doesn’t exist. So if md1 was Name, it would first create a Name attribute in Preferences > Dataif it didn’t exist. Subsequent runs would just use the existing Name attribute.

Thank you. That’s helpful; I’m (now) using the BetterBibTex plugin (https://github.com/retorquere/zotero-better-bibtex#better-bibtex-for-zotero) to export Zotero collections, importing them into Devonthink. Here’s what the .bib file looks like:

I’ve been exporting and editing that in OpenRefine – but now I see I can move columns in Devonthink. If I keep my process consistent, it sounds as if I will be able to maintain the same Name attributes across the imported Zotero collections. Thanks so much!

You’re welcome :slight_smile: