Export file names where tag takes a specific value

Is it possible to export the names of files (documents) that have a specific tag and have them as a list, with each file name on a new line.
e.g. All file names with tag:collapse, to text file “output”
I tried the script Export listing, but did not get what I wanted. I got far too much other stuff. I just want the name of the document.

Thanks for your help
Don Spady

You could delete the columns you don’t need.

If you just need a list of filenames it’s very easy. No need for scripts unless you want to automate this.

When you Copy & Paste a selection from the item list into a plain text file, you get exactly what you want: Just a list of item names, one per line. It doesn’t matter what columns you have displayed.

So just do a search for the tag (or create a smart group), select all, copy/paste. Voila! (This includes any selected items, also groups. So use kind:document if you only want files)

It works the same with a selection of files in Finder, by the way.

PS: You can also use Tools > Create Table of Contents on your selection. This gives you a numbered list of the items (markdown or RTF) – but formatted with item links. Pretty handy.

2 Likes

Thanks for the idea, but after dropping columns, then what? Take a screenshot, use ocr to make it text?
If there are 1000 documents, that is a lot of pictures to take and link.
Must be something more elegant. Also some file names are long and take more than 1 line.

I would guess whatever text editor you view the file in has line wrapping enabled?

Edit: Ah, maybe you mean some filenames are too long to display fully in the item list.


You can also use Tools > Create Metadata Overview. This gives you a sheet, where it might be easier to delete columns. Then convert to plain text if you need.

If you’re ok with using a script; it’s simple code to generate a list of names
Let’s take a look at the script code and what you want changed

I believe @troejgaard’s idea is the simplest and most effective.

  1. Go to the search results or the tag you’re filtering on.
  2. Click on one item.
  3. Press Command-A to select all, then Command-C to copy.
  4. Create or go to a plain text file and click in it.
  5. Command-V and you have a list of the filenames, nothing else.
1 Like

Thanks for the idea.
I am not fluent, (about Grade 1) when it comes to writing script.
Here is my pseudocode
create file “output.txt” in folder Desktop
In Devonthink, in Database ‘X’
search tag:“xxx xxx” (this leads to ~100 - 150 files) I want to copy their names to “output.txt”
if true then copy (append) document name to “output.txt”
If false, then go to the next document.
end when no more files to parse.

I tried to create a rule to do this, but there was no obvious command to copy a name, or to put the result anywhere.

Here is my attempt at a rule

Search in Database
All (of the following are true)
Tag is “Xxx xxx”
checkmark Perform the followint actions ON Demand
???Add to reading list (but that doesn’t do the job. I want an output I can use with a text editor.

Thanks for the help. When I do that, in the sequence you provided, and do the Cmd-V into textedit, I get the whole of every file (several hundred megabytes) as opposed to maybe 50K,
I click on the name of one file in the top window (the one with the file names, size, etc)

Don

You must be using a plain text file. TextEdit defaults to rich text, which you don’t want.
Create a new document in DEVONthink with Data > New > Plain Text and paste into it per my previous instructions.

Alternatively, create a new document in TextEdit, then choose Format > Make Plain Text or press Shift-Command-T. Then paste in to that.

Like BLUEFROG says, it must be plain text. You can also change TextEdits default to plain text in the preferences, which I personally did. (Though I basically never use it anymore)

That works just the way I want. Thanks very much to all of you for your help.
Don

Out of interest, why does an rtf file give all the contents of a file but a text file just gives the file name. It is not intuitive.

1 Like

Out of interest, why does an rtf file give all the contents of a file but a text file just gives the file name. It is not intuitive.

Because rich text supports attachments, creating an RTFD file. This is essentially a special type of folder that looks like a singular file (similar to what a DEVONthink database is and called a “package file”). You were adding copies of the dropped files into that “folder”.

Plain text is just that: text. No formatting, no colors, no frills, no sparkles or sprinkles, and no attachments :wink:

2 Likes

I tried the Tools > Generate Metadata Overview. I selected one file, did a Cmd-A to select all of the eligible files, went to Tools, found Create (NO Generate command) Metadata Overview, waited a bit and it worked a charm.
Thanks for the advice.
Now I know 2 ways to get what I want.

Don

Ah, you’re right. Not sure where I got “Generate” from. Just corrected it for potential later readers.

You can create a toolbar search or smart group to list the items with your desired tag(s).

Then highlight/select all the files and run this script.

There are lots of ways to run the script; I use it in TextExpander as Javascript. Then by just typing the TextExpander abbreviation I can execute the script within Word or Apple Notes or web email etc.

(()=> {
  const app=Application("DEVONthink 3");
  const names = app.selectedRecords.name();
  return names.join("\n");
})()

Having a little fun, here’s a bit terse but succinct approach to getting the filename of files with a specific tag and put them on the clipboard…

set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, linefeed}
tell application id "DNtp" to set the clipboard to (get filename of (contents of (current database) whose (tags contains "iCloud")) as string)
set AppleScript's text item delimiters to od

Extending this a little further, this lets you add a tag to look for and generates a plain text file with the results…

set {od, AppleScript's text item delimiters, searchTerm} to {AppleScript's text item delimiters, linefeed, text returned of (display dialog "Enter case-sensitive tag to find:" default answer "")}
tell application id "DNtp" to create record with {name:searchTerm & " Files", type:text, content:(get filename of (contents of (current database) whose (tags contains searchTerm)) as string)} in current group
set AppleScript's text item delimiters to od

While these are fun, bear in mind, they’re a stacked deck to some degree. That doesn’t mean they’re unsafe or pointless. It just means they aren’t error-trapped, so the second one will generate an empty text file if there are no matches.

To address that and make it a little more readable for learning, with a bit of error-trapping…

tell application id "DNtp"
	set od to AppleScript's text item delimiters
	set searchTerm to ""
	repeat while searchTerm is ""
		set searchTerm to text returned of (display dialog "Enter case-sensitive tag to find:" default answer "")
	end repeat
	set matchedFiles to get filename of (contents of (current database) whose (tags contains searchTerm))
	if matchedFiles is not {} then
		set AppleScript's text item delimiters to linefeed
		create record with {name:searchTerm & " Files", type:text, content:matchedFiles as string} in current group
		set AppleScript's text item delimiters to od
	else
		log message "No files with the tag " & searchTerm & " were found in this database."
	end if
end tell

And here’s a different – and error-trapped – approach using a tags: search…

tell application id "DNtp"
	set od to AppleScript's text item delimiters
	set searchTerm to ""
	repeat while searchTerm is ""
		set searchTerm to text returned of (display dialog "Enter case-sensitive tags to find. Separate them with semicolons, e.g, one;two" default answer "")
	end repeat
	set matchedFiles to search "tags:" & searchTerm in (root of (current database))
	if matchedFiles is not {} then
		set fileList to {}
		set AppleScript's text item delimiters to linefeed
		repeat with theFile in matchedFiles
			copy name of theFile to end of fileList
		end repeat
		create record with {name:searchTerm & " Files", type:text, content:fileList as string} in current group
		set AppleScript's text item delimiters to od
	else
		log message "No files with the tag(s) " & searchTerm & " were found in this database."
	end if
end tell

… when nothing is found …

Epilogue

In case anyone is curious, this post also shows the iterative process often found in scripting. The scripts started with my first three-liner, just to push the limits of brevity for fun. It evolved from there adding some flexibility, then expanding it out for teaching, then showing a secondary approach to finding the files.

In many cases, the evolution is the opposite direction, moving from verbose and extra cautious, with singleton variables (only defined and used once) and complex, to cleaner and more concise code at the end. In other cases, it expands and contracts, from simple and fragile, to overly-complicated, then back to poetry :heart: :slight_smile:

4 Likes