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.
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
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.
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}
I’m not saying this is what you specifically are looking to do. Just showing to you (and the rest of the class ) ways this can be extended quite simply.
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 -->
… 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?
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…
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.
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.