Action

Craft Note From Template

Posted by @FlohGro, Last update about 2 months ago

UPDATES

about 2 months ago

updated description for existing templates in Craft

craft create note from template - Action Description

created by @FlohGro

Craft Create Note from Template

  • the main task for this action is to create notes in Craft from templates stored in Drafts
  • you can setup as many templates as you want, they are identified by the action by configurable tags you apply to the Template Drafts
    • the action also makes it possible to easily create new templates with the configured tags
  • the action will query for all drafts with the configured tags (optionally you can also configure tags which should be omitted) and display a prompt where you can select the template you want to use
  • after selecting a template the action will create and open the new document in Craft

Configuration

  • the action needs a bit of configuration before you can use it the first time
  • template tags: you have to configure the tags which your templates should include (the action will only display these drafts in the menu)
    • therefore you have to edit the script step of the action
    • in line 4 you will see the constant „templateTags“
      • this constant defines the tags which identify your craft template drafts. Please add them as comma-separated-springs into the array.
      • feel free to delete the standard configuration of “3 template”, “3 sw craft”
  • template omit tags: optinally you can configure tags which should not be included in the drafts found by this action
    • e.g. you have configured your templateTags to “3 template”, “3 sw craft“, but you have other drafts with the tag „todo“ which should be ignored by this action
    • add these tags into the empty array of the constant „templateOmitTags“ in line 7 and the corresponding drafts will ignored by this action
  • when you run the action the first time you will se a "Credential Prompt” which will ask you to insert your spaceId of Craft (this is neiden to create or open documents in your space).
    • to retrieve your spaceId just copy the deeplink of andy documenta in that space (refer to the Craft Support Page when you don’t know how to do that)
    • paste the copied id into a draft and you will se a link similar to this: “craftdocs://open?blockId=[the block id]&spaceId=[the spaceId]” - find the character combination “[the spaceId]” after the “spaceId=” and copy it
    • the run the action the first time and paste your spaceId into the Prompt.
    • this will store the spaceId in a Credential in Drafts and sync to your other connected devices - it is a one time action

Setting up templates

  • when you finished the configuration you can start setting up your templates
  • if you already have drafts with the configured tags they will directly show up in the prompt of the action
  • if you don’t have any draft with the configured tags, the prompt will just display a button “[Create New Template]” when you select that button, Drafts will create a new Draft with the configured tags and a default content.
  • replace the title with the name of your template (which will be the title of the created Craft Documents) and just insert the content you want to have in that template in markdown format
  • you can setup as many templates as you want
  • if you already have templates for notes stored in Craft, you can simply export them as markdown to Drafts and apply the tags you want to use with this action

Creating Documents from Templates

  • when you configured the action and set up one or more templates the action can be used easily
  • the prompt will display the titles of all drafts with the configured tags, just select the template you want to use and the action will create and directly open that note in Craft.

Demo

  • I made a short screen recording of the action in use you can watch it here

Troubleshooting and Questions

  • if you have any questions you can reach out to me in the Drafts forums (@FlohGro) or on Twitter (find me by searching for FlohGro or using @FloHyO)
  • if you find any confusing things in the doc please message me and i will update it accordingly

If you find this useful you can Buy Me A Coffee

Steps

  • script

    // craft create note from template - created by @FlohGro
    
    // this constant defines the tags which identify your craft template drafts. Please add them as comma-separated-springs into the array to let the action find the correcht drafts
    const templateTags = ["3 template", "3 sw craft"];
    
    // this constant can be configured optionally. If you have drafts with the same tags in the templateTags array which should not be used as template in this action, please add the extra tags which identify the drafts which shouldn't be used as templates here
    const templateOmitTags = [];
    
    // ----------------------------------------------------------------
    // END OF CONFIGURATION - CHANGING ANYTHING BELOW WILL PROBABLY BREAK THE FUNCTIONALITY OF THE ACTION
    // ----------------------------------------------------------------
    
    const spaceId = getCraftSpaceIdToUse();
    
    if (spaceId) {
        let templateDrafts = Draft.query("", "all", templateTags, templateOmitTags, "accessed", false, false)
    
        let selectedDraft = selectTemplateDraftFromPrompt(templateDrafts);
    
        if (selectedDraft) {
    
            let createdDocumentBlockId = createCraftNoteFromDraft(selectedDraft);
    
            if (createdDocumentBlockId) {
                let blockId = createdDocumentBlockId;
                openCraftDocumentWithBlockId(blockId)
            }
    
        }
    
    } else {
        // error notification already fires in function
    }
    
    // -----------------------------------------------------------------
    // function definitions
    // -----------------------------------------------------------------
    
    function getCraftSpaceIdToUse() {
        let credential = Credential.create("CraftDocumentSpace", "Credential to store the spaceId of the space you want to use in Drafts.\ninsert your spaceId into the TextField below. \n\nNOTES: \n- this is a one time action, you don't need to do it everytime\n- Multiple spaces are currently not supported with this spaceId credential helper");
        credential.addTextField("spaceId", "spaceId");
        if (credential.authorize()) {
            return credential.getValue("spaceId");
        } else {
            let errorStr = "failed storing / retrieving space Id with credential"
            app.displayErrorMessage(errorStr);
            context.fail(errorStr);
            console.log(errorStr);
            return false;
        }
    }
    
    
    
    function selectTemplateDraftFromPrompt(templateDrafts) {
    
        let p = Prompt.create();
        p.title = "select template";
    
        p.addButton("[Create New Template]");
        for (templateDraft of templateDrafts) {
            p.addButton(templateDraft.displayTitle, templateDraft)
        }
    
        if (p.show()) {
            if (p.buttonPressed != "[Create New Template]") {
                return p.buttonPressed;
            } else {
                let newTemplateDraft = new Draft()
                for (tag of templateTags) {
                    newTemplateDraft.addTag(tag);
                }
                newTemplateDraft.content = "# New Template Draft\n"
                newTemplateDraft.update();
                alert("created new template draft and will load it now.")
                editor.load(newTemplateDraft);
                return false;
    
            }
        } else {
            context.cancel();
            return false;
        }
    
    
    }
    
    function createCraftNoteFromDraft(inputDraft) {
    
    
        let title = inputDraft.displayTitle
    
        let content = inputDraft.processTemplate("[[body]]")
    
        content = encodeURIComponent(content)
    
        const baseURL = "craftdocs://x-callback-url/createdocument?"
    
        let cbCreate = CallbackURL.create()
        cbCreate.baseURL = baseURL
        cbCreate.addParameter("spaceId", spaceId)
        cbCreate.addParameter("title", title)
        cbCreate.addParameter("content", content)
        cbCreate.addParameter("folderId", "")
        cbCreate.waitForResponse = true
    
        let result = cbCreate.open()
    
        if (result == true) {
            console.log("Craft note successfully created")
            // parse result:
            let craftCreateResult = cbCreate.callbackResponse
            var blockId = craftCreateResult.blockId
    
            return blockId;
        } else {
            console.log("Craft note result:" + cb.status + " " + cb.callbackResponse)
            if (cb.status == "cancelled") {
                context.cancel()
            } else {
                context.fail()
            }
            return false;
        }
    
    }
    
    
    function openCraftDocumentWithBlockId(blockId) {
        const baseURL = "craftdocs://open?"
        let cbOpen = CallbackURL.create()
        cbOpen.baseURL = baseURL
        cbOpen.addParameter("spaceId", spaceId)
        cbOpen.addParameter("blockId", blockId)
        cbOpen.waitForResponse = false;
        cbOpen.open();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.