Simple smart template. No localization?

I would like to create a simple smart template that creates a group with a user defined name.
That group should also contain a file with that same name.

Once I get this working, perhaps I can learn AppleScript. Heh.

Screen Shot 2020-02-10 at 13.28.12

I tried to copy the project smart template but what I created failed. I first tried with only English localizations. Perhaps I could get this working first without localizations then later add the complexity?

Is there an example of a smart template that does not use localization?

Here is my attempt without localizations.

property pTemplateName : "Document"

-- Import helper library
tell application "Finder" to set pathToAdditions to ((path to application id "DNtp" as string) & 
"Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions

try
    tell application id "DNtp"
	    activate
	    set theName to display name editor default answer pTemplateName info "please enter a name for this document:"
	
	    set theTemplateFile to (path to me as string) & "Contents:Resources:" & theName & ".md"
	
	    set thePlaceholders to {|%groupName%|:theName}
	    set theRecord to import theTemplateFile placeholders thePlaceholders to current group
	    set the name of theRecord to theName
    end tell

 on error errMsg number errNum
   if errNum ā‰  -128 then display alert "An error occured when creating the new document" message errMsg as warning
end try

It produces this error.

A good attempt :slight_smile:

set theTemplateFile to (path to me as string) & "Contents:Resources:" & theName & ".md"

This line will not work since there is no such file as whatever I typed.md. There is a folder and a file named %groupName%. You just need to import the folder and the imported Markdown file will also take the name specified in the display name editor dialog.

Note since this placeholder already exists, I would use a different placeholder name, like %myGroup%. Just to lessen possible confusion.

Here is the simple structureā€¦
Simple TemplateScriptD

Simple Group with File.templatescriptd.zip (7.2 KB)

Ohhhh. I see. Thank you!

And for a bit more fun, to show how this can be extended, I added a little subroutine to get an abbreviation of whatever was typed in and name the Markdown file with that abbreviation.

set abbrev to {}
		repeat with currentWord in (words of theName)
			set currentWord to currentWord as string
			if (count characters) of currentWord > 3 then
				copy (characters 1 thru 3 of currentWord as string) to end of abbrev
			else
				copy currentWord to end of abbrev
			end if
		end repeat
		
		set thePlaceholders to {|%myGroup%|:theName, |%myGroupAbbrev%|:abbrev as string}

Hereā€™s the foder structureā€¦
Screen Shot 2020-02-10 at 4.00.26 PM

Running the scriptā€¦

And the resultā€¦

Notice I also put the placeholders in the Markdown file in the template, both the custom placeholder and one of our standard onesā€¦

Iā€™m not saying this is what you specifically are looking to do. Just showing to you (and the rest of the class :stuck_out_tongue: ) ways this can be extended quite simply.

Cheers!
(And youā€™re welcome) :slight_smile:

1 Like

This is really helpful, thanks!

A quick query: the content of the markdown document is clearly designed to do various searches when clicked in the Preview, but Iā€™m unsure exactly how. I think Iā€™ve worked out the first two (search for the name of the new Group and document in your selected documentsā€™ name or content) and they work.

But what are the last two doing? e.g. Whatā€™s the function of <Dropbox in the third example? (It just beeps at me, and there is a document starting with ā€˜Markdown Thingyā€™ (the name of my newly created group) in the Inbox.)

Why doesnā€™t the fourth search work with that error message?

Thanks!

[Content Search in Selection](x-devonthink://search?query=text:markdown%20scope:selection)

[Name Search in Selection](x-devonthink://search?query=name:markdown%20scope:selection)

[Name Begins With Search in Inboxes](x-devonthink://search?query=name:<Dropbox%20scope:Inboxes)

[Added within 4 Days in All](x-devonthink://search?query=additionDate:#4days)  
<!-- Can't specify a scope of All Databases. No scope uses the last used scope -->

haha! :stuck_out_tongue: Youā€™re not supposed to have that file. Thatā€™s my oversight.

I was testing some things in Markdown with our URL scheme.

But what are the last two doing? e.g. Whatā€™s the function of <Dropbox in the third example?

Itā€™s just a percent-encoded search similar to the first. It has nothing to do with Dropbox.

And the last doesnā€™t work because thereā€™s no scope of All Databases in the toolbar search.

Fair enough! Interesting, thoughā€¦

Thanks again.

No worries!
Maybe it will inspire something for you. :slight_smile:

Oh, it inspired meā€¦

ā€¦ but unfortunately not with the competence to adapt it the way I wanted ;-(

Essentially, what I want to do is create a new markdown file with various placeholders filled in. The first stage was just to create the note with the name I entered, and this works.

	tell application id "DNtp"
		activate
		set theName to display name editor default answer pTemplateName info "Please enter a name for this document:"
		set theTemplateFile to (path to me as string) & "Contents:Resources:%myDoc%.md"
		set thePlaceholders to {|%myDoc%|:theName}
		set theRecord to import theTemplateFile placeholders thePlaceholders to current group
	end tell

What I canā€™t work out how to do is to prepend this with a calculated date (say %shortDate%) so the final note title is ā€œ19/02/2020 The bit I entered in the Prompt just nowā€.

Sorry for being dense, but what to I have to do to get this to work?

Thanksā€¦

Are you referring to the title, meaning the filename ?

PS: I would not use forward slashes in a filename, for best compatibility.

Thanks for the quick reply.

I only chose shortdate as an example for simplicity of typing this post (I normally use 20200219 etc) ā€“ but I see that was causing one of the problems. When I rename the file to ā€œ%year%.md%ā€, and make the same changes inside the script, then the new file is given the filename ā€œ2020.mdā€ and the note title ā€œ2020ā€.

But then the prompt is ignored, of courseā€¦ how do I put both parts together so I end up with the filename ā€œ2020 This is the bit I entered.mdā€ and the note title ā€œ2020 This is the bit I enteredā€? (I have the setting not to show extensions in DT, of course).

Thanks ā€“ I should know this, but Iā€™m not doing well rememberingā€¦

No worries!

Are you wanting todayā€™s date?
If so, check this post outā€¦

In that script you can structure the titleDateString in the way you want to.
And using the line from your code, you could putā€¦

set thePlaceholders to {|%myDoc%|:(titleDateString & " " & theName)}

Yes ā€“ basically the old reversed timestamp with no separators: %year%%month%%day%%hour%%minute%

Thanks!

That was it! ā€“ it was the way of concatenating the two placeholder elements I was having difficulty with. Many thanks for your quick replies, as everā€¦

Youā€™re very welcome :slight_smile:

Jim,

Iā€™m very sorry for the mithering, but I just canā€™t get this to work. All I want to do is create a markdown file in the current group, with the filename and note title populated from the current date and ā€œName I entered in the promptā€. Your script works perfectly for a new group with a file within in ā€” I just want the file itself.

I know itā€™s not the translation of the date into a string ā€“ that works (as the dialogs show). Itā€™s that I simply canā€™t find a way to create a record at all with this script, no matter what I try, even though AFAICS it just follows your example, which does workā€¦ Iā€™m clearly missing something obvious, but have no idea whatā€¦ [The actual markdown template is called ā€œ%myDoc%.mdā€ and itā€™s under Resources in the template folder.]

property pTemplateName : "Document"

try
tell application id "DNtp"
	activate
	
	set titleDateString to do shell script "date +%F"

	set theName to (display name editor default answer pTemplateName info "Please enter a name for this document:")
            set theName to titleDateString & " " & theName
	display dialog theName

	set theTemplateFile to (path to me as string) & "Contents:Resources:%myDoc%.md"
	
            display dialog theTemplateFile
	
	set thePlaceholders to {|%myDoc%|:theName}
	
	set theRecord to import theTemplateFile placeholders thePlaceholders to the current group
end tell

on error errMsg number errNum
if errNum ā‰  -128 then display alert "An error occured when creating the new document" message errMsg as warning.
end try

The dialog boxes show everything they should and thenā€¦ nothing. No new record created (anywhere, never mind in the current group), and no error message. Nothing.

So what am I missing please.

Thanks again for your patience!

I suggest removing the try block or changing the if errNum line toā€¦

display alert "" & errNum

I rarely add try blocks to a script unless Iā€™m using them for specific purposes. That way I can see when errors are thrown, especially when Iā€™m initially working on the script.

PS: I generated a new template and used your code (though I removed the tryā€¦ block so i could watch for errors). Itā€™s working as expected here.

brookter.templatescriptd.zip (4.5 KB)

Thanks Jim!

I have no idea why, but my version wasnā€™t working and the one youā€™ve just sent me does, so Iā€™ll cut my losses and just use that!

I really appreciate your help ā€“ thank you.

Youā€™re very welcome :slight_smile: