New From Template Re-Naming Of Groups And Files

I feel bad about asking for this because I’m certain this has come up before (probably several times) and I’ve not been able to find a reference to it on the board.

I have a multi-group structure I call “Project,” and I’ve saved this as a template. Included in the template are a few empty files, such as a blank notebook. When I start a new project and use “new from template” I would like to run a script that requests a name, and then uses that name to rename certain groups and files (ideally by pre-pending the supplied project name to the name stored in the template e.g. “Bill Of Material” becomes “Project X Bill of Material”). I’d like that to happen for both (some) files and (some) groups within the structure defined by the template. Realizing the “some” criteria in the previous sentence represents script modifications I will likely have to make myself, is there an existing script floating around that does the basics of this (or close)? I’m sure this has been done, I’m probably not using the right terms to search for it.

Thanks,

Gary

Your best bet is to look inside the Project.templatescriptd template in Templates.noindex. This template is a package, so use the Finder contextual menu to view package contents. In the Scripts folder of the package is the controlling script, which prompts for user name for the project hierarchy when it the template is activated. If you copy that template and modify its name, internal hierarchy, and script you can start building the result you’re looking for.

You can also incorporate placeholders in your logic. Look at the script inside the Phone Note.templatescriptd template for the placeholder techniques.

Thanks, it’s gonna rain all weekend. Perfect weather for scripting. :slight_smile:

If you come up with a solution you like and it can be shared with others please consider posting it here as an attachment. The community gets better when it sees new work.

This was surprisingly easy. I need to play with it a bit (It’s still raining) but I modified just one line of the script to get this the way I wanted it. If it looks good tomorrow I’ll write up some instructions on what I did.

Gary
Screen Shot 2016-09-30 at 9.32.59 PM.png

Great!

I’m a few days late with this, but here is what I did to get this working.

In Devonthink, open the scripts folder, and open Templates.noindex. Do a Show package contents for Project.templatescriptd.

Inside, open Contents, then Resources. Locate the folder matching your localization (en.lproj in my case).

In this folder there is a folder called New Project. This is the top node of the New From Template > Project folder tree that ships with Devonthink. Move (not copy) the New Project folder to a safe place if you think you may want to restore it at some point. Otherwise, delete it.

Create a new folder called New Project (this is what the existing script expects and it will be replaced anyway). In New Project, create the folders (groups) you would like the template to create for you. Important part When naming the folders, any folder you want to use the project name, substitute %project% for the name you will supply when you run the script. For example, for a project called Manhattan, a desired group name of Manhattan Project Documents would be named %project% Project Documents.

If you want to populate your groups with template files, copy the files into the appropriate folders. As with the folder/group names, replace the project name with %project%. So, Manhattan Project Notebook.pages is saved as %project% Project Notebook.pages.

If you like changing the icons of files and groups in Devonthink (as I do) change the ones you want in your folder tree using the standard Finder method. I use 1024x1024 PNG files for this.

Back up to the en.lproj level and open the folder called Scripts. Inside, there is a single file called Main.scpt. Open this in the script editor. I wanted my top level group in DT to read Project: Manhattan (in this example). To do that, go to the line just above the last End Tell (just before the error handler) and change it to read:

set the name of theRecord to "Project: " & theProjectName

This step is not required if you’re OK with the top group being named simply Manhattan.

When you’re done, you can select New From Template > Project from within DT, it will ask for a project name, and will build your group structure substituting the name you gave it for %project% where ever you used it.

Now, two issues.

#1 - Don’t make the folder tree a copy of an existing structure you made in DT using File > Export > as Template. If you do that, DT puts a file called DEVONtech_storage in each folder that contains another folder or a file. The existence if the DEVONtech_storage file appears to break the %project% name substitution. I don’t know why, but you can fix it by deleting those files (or by creating your structure manually external to DT as I described).

#2 - Any icon I used for the top level folder/group of my project structure (only the top level, all others are fine) seemed to be shrunk by DT. This is a bad thing since the DT group icons are too small anyway. I found no solution to this other than going back to the default DT project icon (a box with a check mark)

I hope this helps someone.

Happy Computing,

Gary

Gary, thanks.

You might want to copy Project.templatescriptd to your own template file – like MyProject.templatescriptd. A future update of DEVONthink could come along and replace your work with the standard template. Updates won’t touch custom files that are not part of DEVONthink’s install package.

Great writeup and very helpful. I learned a few things I didnt know.

Thanks Gary for taking the time to do it.

Frederiko

Thank you Frederiko and Korm

Korm - A very timely suggestion (and one I’m glad I did immediately) as I see DT was just updated for Sierra.

Regards,

Gary

This is almost exactly what I need, but missing one small addition.

I would like to have the name of the currently selected Group available to add to certain folder names, and some file names.

i.e. I add projects as sub-folders under a client master folder. So if I have a new project about “Exhausts” at BMW, I select “BMW” top folder, then add the project under that, and give it the name “Exhaust”.

I would like that to automatically add the master client folder name (BMW) to the front of the project name for the folder (and certain files). e.g using the same kind of placeholder structure;

%client% : %project%

Then inside each top level project folder I add a formatted note file;
%client% : %project% - Notes

However, I cant get a way to store the Client folder name in a variable. I have tried adding properties, then calling them, but the closest I could get was using script which stores the group name in the clipboard function, but could not get it to work.

I think it would be a simple addition, but cant see how.

Any help would be much appreciated.
Screen Shot 2017-07-25 at 12.36.24.png

Sorry, forgot to add the script I have been playing with;

-- Smart template adding a localized project template to the current group
-- Written by Eric Böhnisch-Volkmann, modified by Christian Grunenberg
-- © 2009–2016 DEVONtechnologies, LLC


-- Default non-localized project name, also used to identify the resources

property pClient : "Client"


-- We're later working in DEVONthink, we need to cache localized strings while still in our realm
set theProjectName to localized string pTemplateName

try
	
	tell application id "DNtp"
		set theItem to (the first item of (the selection as list))
		set pClient to "" & name of theItem & ""
		
		set theProjectName to display name editor my helperLibrary's localizedString(pTemplateName) default answer theProjectName info (my helperLibrary's localizedString("Please enter a name for this project") & ":") as string
		
		set thePlaceholders to {|%client%|:pClient, |%project%|:theProjectName}
		
		-- Import the predefined structure
		set theTemplateFiles to helperLibrary's pathToLocalizedResources() & my helperLibrary's localizedString(pTemplateName)
		set theRecord to import theTemplateFiles placeholders thePlaceholders to current group
		set the name of theRecord to "Project: " & theProjectName 	-- Rename to user-defined project name
		
	end tell
	
on error errMsg number errNum
	if errNum ≠ -128 then display alert (localized string "An error occured when creating the new project structure") message errMsg as warning
end try

It requires a bit more tweaking than just adding a variable. I have attached the modified script you can replace the existing one with (hopefully in a copy of the original Project template). However, this is the change you’ll have to make to the directory structure inside the Template.
Screen Shot 2017-07-25 at 12.03.03 PM.png

There only three lines slightly modified in the script.
main.scpt.zip (8.69 KB)

Thank you Bluefrog - it is exactly what I needed. Your help is very much appreciated. :smiley:

When I compare the scripts it also slowly teaches me a bit more about scripting.

You’re welcome.

This is exactly the kind of thing I hope to hear! Good on ya! :smiley: