Action

crosslink Things task & Craft document

Posted by FlohGro, Last update 9 months ago

UPDATES

10 months ago

  • name changed
  • action no uses credential store for the Craft space id
  • description update
show all updates...

10 months ago

  • name changed
  • action no uses credential store for the Craft space id
  • description update

over 1 year ago

fixed wrong variable name in error case

created by @FlohGro / more on my Blog

crosslink Things task & Craft document

This action will create a cross-linked Task between Things and a Craft document.

A new Craft document will be created from the content of the current draft with the first line as title for the document.

After the document is created the action will create a new task in the inbox of your Things account with the title of the Craft document as task name. The task will contain a link to the Craft document in the notes field of the task

When the task was created the action will prepend a link to it into the created Craft document.

[Configuration]

If you don’t want to use the (any of my) action for different Craft spaces there is no configuration needed. When you first run any of my Craft actions 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 oof my Craft actions you install.

To use these actions with different spaces you need to duplicate the action for each space you want to use it. I recommend to e.g. add a suffix to the action name to describe the space for which you configure it.

The action uses Drafts possibility to store credentials to distinguish different spaces. When you duplicate the action for another space you have to change the name of the credential. Therefore you need edit this line const spaceIdCredentialName = "CraftDocumentSpace" in the script step of the action and change the CraftDocumentSpace to something different (e.g. describe the space in a suffix like „CraftDocumentSpacePersonal" or „CraftDocumentSpaceWork“. If you use several of my Craft actions you should use the same credential name in all of them.

[Usage]

Use this action to create a cross-linked task / document between your task manager and Craft. If you e.g. took some notes during a meeting or while reading a book / blog in Drafts which you need to review or complete later - just run this action. It will help you to quickly navigate between the task and the document without seeing distracting other content.


If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    // cross linked craft note and things task
    
    // 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();
    
    let title = draft.displayTitle
    
    
    let encodedTitle = encodeURIComponent(title)
    
    let content = draft.content
    
    content = encodeURIComponent(content)
    
    var proceed = true;
    
    const craftCreateBaseURL = "craftdocs://x-callback-url/createdocument?"
    const craftCreateBlockBaseURL = "craftdocs://createblock?"
    const thingsAddBaseURL = "things://x-callback-url/add?"
    
    var cbCreateCraftNote = CallbackURL.create()
    cbCreateCraftNote.baseURL = craftCreateBaseURL
    cbCreateCraftNote.addParameter("spaceId", spaceID)
    cbCreateCraftNote.addParameter("title", title)
    cbCreateCraftNote.addParameter("content", content)
    cbCreateCraftNote.addParameter("folderId", "")
    cbCreateCraftNote.waitForResponse = true
    
    let craftCreateReturn = cbCreateCraftNote.open()
    
    if (craftCreateReturn == true) {
        console.log("Craft note successfully created")
    } else {
        proceed = false
        console.log("Craft note result:" + cb.status + " " + cb.callbackResponse)
        if (cb.status == "cancelled") {
            context.cancel()
        } else {
            context.fail()
        }
    }
    
    // craft note is created, now create the things task
    if (proceed) {
    
        // retrieve info from craftCreateResult
        let craftCreateResult = cbCreateCraftNote.callbackResponse
        var blockId = craftCreateResult.blockId
        var craftNoteLink = craftCreateResult.link
    
        let taskNoteStr = "Craft Note: " + craftNoteLink
    
    
        var cbAddThingsTask = CallbackURL.create()
        cbAddThingsTask.baseURL = thingsAddBaseURL
        cbAddThingsTask.addParameter("title", title)
        cbAddThingsTask.addParameter("notes", taskNoteStr)
        cbAddThingsTask.waitForResponse = true
    
        let thingsAddReturn = cbAddThingsTask.open()
    
        if (thingsAddReturn == true) {
            console.log("Things Task successfully created")
        } else {
            proceed = false
            console.log("Add Things task result:" + cbAddThingsTask.status + " " + cbAddThingsTask.callbackResponse)
            if (cbAddThingsTask.status == "cancelled") {
                context.cancel()
            } else {
                context.fail()
            }
        }
    }
    
    // things task added now prepend the link to the previously created craft note
    if (proceed) {
    
        // retrieve info from craftCreateResult
        let thingsAddResult = cbAddThingsTask.callbackResponse
    
        let showTaskUrl = "things:///show?id=" + thingsAddResult["x-things-id"]
    
        let openTaskStr = "> Task in Things: [" + title + "](" + showTaskUrl + ")"
    
        let encodedBlockContent = encodeURIComponent(openTaskStr)
    
        var cbCreateCraftBlock = CallbackURL.create()
        cbCreateCraftBlock.baseURL = craftCreateBlockBaseURL
        cbCreateCraftBlock.addParameter("spaceId", spaceID)
        cbCreateCraftBlock.addParameter("parentBlockId", blockId)
        cbCreateCraftBlock.addParameter("index", 0)
        cbCreateCraftBlock.addParameter("content", encodedBlockContent)
        cbCreateCraftBlock.waitForResponse = false
    
        cbCreateCraftBlock.open()
    }
    
    
    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;
        }
    }

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.