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: