Action

Append to List

Posted by RosemaryOrchard, Last update over 3 years ago

Allows you to append to any Markdown file in a specific folder in Dropbox

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    let path = "/Obsidian/RosemaryOrchard/Lists/";
    let content = draft.content;
    let replacementWords = ['watch', 'read'];
    replacementWords.forEach(function (word) {
    	content = content.replace(new RegExp(word, "gmi"), "");
    });
    draft.content = content.trim();
    draft.update();
    
    // create Dropbox object and vars
    let db = Dropbox.create();
    let endpoint = "https://api.dropboxapi.com/2/files/list_folder";
    let args = {
        "path": path,
        "recursive": false,
        "include_media_info": false,
        "include_deleted": false,
        "include_has_explicit_shared_members": false,
        "include_mounted_folders": true
    };
    
    // make API request
    let response = db.rpcRequest({
    	"url": endpoint,
    	"method": "POST",
    	"data": args
    });
    if (response.statusCode != 200) {
    	console.log("Dropbox Error: " + response.statusCode + ", " + response.error + ' ' + db.lastError);
    	context.fail();
    } else {
    	let p = Prompt.create();
    	p.title = "Pick a file";
    	let fileList = response.responseData.entries;
    	Object.keys(fileList).forEach(function(key) {
    		if (fileList[key].name.endsWith('.md')) {
    			p.addButton(fileList[key].name.replace(".md", ""), fileList[key].name);
    		}
    	});
    	let didSelect = p.show();
    	if (didSelect) {
    		draft.setTemplateTag("append_path", path)
    		draft.setTemplateTag("append_file",p.buttonPressed);
    	} else {
    		context.cancel();
    	}
    }
    
    
    
    
  • dropbox

    fileNameTemplate
    [[append_file]]
    folderTemplate
    [[append_path]]
    template
    [[draft]]
    writeType
    append

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.