Action

JSONIfyDRAFT

Posted by corwinslack, Last update over 5 years ago

The logic is by @agiletortoise but extended by corwinslack who is responsible for mistakes.

This creates a JSON that contains most of the template tags. Run this as step 1 in an action and then run a shortcut step with the template [[json]].

The first step of the Shortcut would be

get dictionary from input.

The keys for the dictionary are the template tag names.

This will even pass [[shortcut_result]] if a shortcut was executed as step 1 of an action and this is step 2.

Steps

  • script

    // this creates a JSON that contains most template tags
    // typically this is step 1 of a script
    // step to should be Run shortcut with the template [[json]]
    // the first step of the shortcut should be 
    // get dictionary from input
    
    // http://getdrafts.com/scripting
    
    // by @agiletortoise but extended by corwinslack
    // create the values you want to send
    
    //let   = draft.processTemplate("[[]]"); 
    
    
    let title = draft.processTemplate("[[title]]");
    let drafts = draft.processTemplate("[[draft]]");
    let body = draft.processTemplate("[[body]]");
    let selection = draft.processTemplate("[[selection]]");
    let uuid = draft.processTemplate("[[uuid]]");
    let safe_title = draft.processTemplate("[[safe_title]]");
    let selection_only = draft.processTemplate("[[selection_only]]");
    let selection_start  = draft.processTemplate("[[selection_start]]");
    let selection_length = draft.processTemplate("[[selection_length]]");
    let created = draft.processTemplate("[[created|%F %T]]");
    
    let modified = draft.processTemplate("[[modified|%F %T]]");
    let shortcut_result = draft.processTemplate("[[shortcut_result]]");
    let draft_open_url = draft.processTemplate("[[draft_open_url]]");
    let tags = draft.processTemplate("[[tags]]");
    let latitude_created = draft.processTemplate("[[latitude_created]]");
    let longitude_created = draft.processTemplate("[[longitude_created]]");
    let  latitude_modified = 
    draft.processTemplate("[[latitude_modified]]");
    let longitude_modified = draft.processTemplate("[[longitude_modified]]");
    // convert body to new "tag" that is in HTML format
    let mdbody = draft.processTemplate("%%[[body]]%%"); 
    
    
    
    // make a JS object holding those keys-values
    let obj = {
        "draft": drafts,
        "title": title,
        "body": body,
        "mdbody": mdbody,
        "selection": selection,
        "safe_title": safe_title,
        "selection_only": selection_only,
        "uuid": uuid,
        "selection_start": selection_start,
        "selection_length": selection_length,
        "created": created,
        "modified": modified,
        "draft_open_url": draft_open_url,
        "tags": tags,
        "latitude_created": latitude_created,
        "longitude_created": longitude_created,
        "latitude_modified": latitude_modified,
        "longitude_modified": longitude_modified,    
        "shortcut_result": shortcut_result
    }
    
    // create a template tag for use in later action steps in this action
    // JSON.stringify ensures proper conversion to JSON
    draft.setTemplateTag("json", JSON.stringify(obj)); 
    
    // in later action steps, like a Run Shortcut step,
    // use new [[json]] tag

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.