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ā¦
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ā¦
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.