How to create a list of folders based on a list of foldernames in a text file?

Let’s say I have a text file that looks something like this:

Folder 1
Folder 3
Folder 54

Let’s say that the list has 800+ lines to it.

Is there an easy way for me to automate the creation of folders in DevonThink (one folder per line name)? Or do I have to do this manually?

You mean “groups”, I guess. And yes, you can create them automatically with AppleScript or JavaScript.
But are you sure that 800+ groups in DT are what you want?

1 Like

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property theWorks : {"Folder 1", "Folder 2", "Folder 3", "etc"}

tell application id "DNtp"
	
	repeat with theWork in theWorks

		set theWork to (localized string theWork)
		create record with {type:group, name:theWork} in current group

	end repeat

end tell

2 Likes

Thank you. Yeah I guess I can script them. I wish there was a scripting guide for DevonThink. I haven’t the slightest idea how to begin with JavaScript scripting.

Thank you. I guess this presumes that I have them all listed in that format within the curly brackets?

I tried to make it plain to see where the folder names should go, so I am not sure if I understand your question.

1 Like

I guess I wanted it to get the names for the folders from the txt file, with one line of the txt file being a folder name.

With your script, if I understand it right, I have to manually format them into that format between curly braces?

You can do it in a matter of seconds using RegEx. If you don’t have any text editor, download one and play around with it (e.g. BBEdit, Sublime Text, Visual Studio Code). It should be a simple matter of replacing all ^(.+)\n with "\1",

3 Likes

AppleScript can get paragraphs of, no regex needed.

2 Likes

Seconding @chrillek’s question: Do you actually want to create 800 groups?

Sure. I’m tracking 800 different entities. Need to keep all the material for each different entity together.

And what do you expect the groups to be named?

tags? Groups are kind of intrusive (for lack of a better wording). Navigating 800+ groups seems like a self-inflicted nightmare to me. As would be 800+ folders in the Finder.
But it’s your decision, of course.

1 Like

You might be surprised how many groups some users have in their databases :slight_smile:

1 Like

The names of the various entities. Each group would have a sub-folder structure to capture various aspects relating to that entity.

In this case create location might be better than create record with as you could create groups and sub groups in one go. Make sure that slashes in your sub group names are escaped! Select the record containing the group names, run the script and you’re done.

-- Create groups based on selected text record

property theSubGroup_Locations : "1
1/1.1
1/1.1/1.1.1
2
2/2.1
This group name contains a \\/ and \"quotes\"
"
tell application id "DNtp"
	try
		set theSelection to selection of viewer window 1
		if theSelection = {} or ((count theSelection) > 1) then error "Select one record."
		set theRecord to item 1 of theSelection
		set theText to plain text of theRecord
		if theText = "" then error "Select a record with text."
		set theGroupNames to paragraphs of theText
		
		set theGroup to display group selector "Create groups in:"
		set theGroup_Location to (location of theGroup & name of theGroup) as string
		
		set theSubGroup_Locations to paragraphs of theSubGroup_Locations
		
		repeat with thisGroupName in theGroupNames
			if thisGroupName contains "/" then set thisGroupName to my replace_String(thisGroupName, "/", "\\/ ")
			set thisGroup_Location to (theGroup_Location & "/" & thisGroupName) as string
			set thisGroup to create location thisGroup_Location
			repeat with thisSubGroup_Location in theSubGroup_Locations
				create location (thisGroup_Location & "/" & thisSubGroup_Location) as string
			end repeat
		end repeat
		
		display notification "Groups created"
		
	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

on replace_String(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replace_String

You can go to https://helpdesk.devontechnologies.com and start a support ticket to discuss this in more detail, if you’d like.

Brett Terpstra has a utility script called “planter” which may do the trick

It will certainly work outside of DT.

This script just saved me couple hours. Thank you. How hard would it be to convert this to do the same thing in Outlook :)?

Thanks

Have you given it a shot yourself?

1 Like