Action

crosslink to Craft and Things

Posted by @FlohGro, Last update 5 months ago

UPDATES

5 months ago

fixed wrong variable name in error case

crosslink to Craft and Things

  • this action will create a new Craft Document from the content of the current draft
  • the first line of the draft will be used as title of the Craft Document
  • after creating the Craft Document, the action will add a new taks to Things with a link to the craft document in the notes field of the task
  • after the task in things is created, the action will prepend a link to the things task into the previously created craft document

Configuration

  • to make this action work, you need to configure the spaceId.
  • You can get it by copying the deepplink of a document in this space. Paste the link into a new draft and extract the combination like “2159f404-e3ed-4d8a-fbe1-6c36499c8f3” after spaceId= in the link.
  • replace “unconfigured” with this id in the spaceID variable of the script (attention: only replace the unconfigured in line 5!).

if you find this useful you can Buy Me A Coffee

Steps

  • script

    // cross linked craft note and things task
    
    // insert your preferred space ID for a document here. You can get it by copying the deepplink of a document in this space. Paste the link into a new draft and extract the combination like "2159f404-e3ed-4d8a-fbe1-6c361399c8f3" after spaceId= in the link. replace "unconfigured" with this id in the spaceID variable.
    
    const spaceID = "unconfigured"
    
    if(spaceID == "unconfigured"){
    
    let errorMsg = "the spaceID variable was not configured in the script"
    alert(errorMsg)
    context.fail(errorMsg)
    
    } else {
    
    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()
    }
    }

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.