Action

Craft Note Linked in Daily Note

Posted by FlohGro, Last update 12 months ago

created by @FlohGro / more on my Blog

Craft Note Linked in Daily Note

This action creates a new Document in Craft in a configurable space from the content of the current draft.
After creating the document a link to it will be appended to the todays daily note.

ATTENTION: This action needs a compatible shortcut to be installed on your devices. Download the shortcut Append to Crat Daily Note before using this action!

[Configuration]

When you first run the action it will ask you to store the space id of your Craft space. This is a one time action and you don’t need to do it for any other of my Craft actions you install.

To retrieve your spaceId just copy the deeplink of any document 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 and paste it into the prompt of this action.

[Usage]


If you find this useful and want to support me you can donate or buy me a coffe

Buy Me A Coffee

Steps

  • script

    // create Craft note & add link to template tag "docUrl"
    
    // optional configuration of the spaceIdCredentialName - only necessary if you want to use the same action for different Craft Spaces
    const spaceIdCredentialName = "CraftDocumentSpace"
    
    
    let spaceID = getCraftSpaceIdToUse();
    
    if(!spaceID){
    	context.fail()
    } else {
    let title = draft.displayTitle
    
    let content = draft.processTemplate("[[body]]")
    
    content = encodeURIComponent(content)
    //let spaceID = "883e9e8f-3427-8904-416c-e0a216c53af8"
    
    const baseURL = "craftdocs://x-callback-url/createdocument?"
    
    var cb = CallbackURL.create()
    cb.baseURL = baseURL
    cb.addParameter("spaceId", spaceID)
    cb.addParameter("title", title)
    cb.addParameter("content", content)
    cb.addParameter("folderId", "")
    cb.waitForResponse = true
    
    let result = cb.open()
    
    if (cb.status == "success") {
        console.log("Craft note successfully created")
        //retrieve the link to the new note from the cb response
        const docLink = cb.callbackResponse.link
        const mdDocLink = "["+ title +"]" + "("+ docLink+")"
        draft.setTemplateTag("docUrl",mdDocLink)
    } else {
        console.log("Craft note result:" + cb.status + " " + cb.callbackResponse)
        if (cb.status == "cancelled") {
            context.cancel()
        } else {
            context.fail()
        }
    }
    }
    
    function getCraftSpaceIdToUse() {
        let credential = Credential.create(spaceIdCredentialName, "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;
        }
    }
  • runShortcut

    shortcutName
    Append to Craft Daily Note
    waitForResponse
    false

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.