Action

Publish tag to OneDrive

Posted by tja, Last update almost 6 years ago

Shameless modification of “Publish tag to iCloud Drive” to work for OneDrive:

Prompt for use to select a tag. Query for all drafts in the inbox with the selected tag, and write each to a separate file in “/Drafts/Tags/“ folder within OneDrive.

Great for backing up all active drafts with a certain tag, or bulk publishing a series of drafts.

Configuration options can be configured at the top of the script to setup default tags to prompt for, destination folder, and to set Drafts templates to use to name the files and for the content of the files.

Steps

  • script

    // START configuration options
    
    // set to sub folder path for a folder in the Drafts iCloud Drive folder.
    let destinationFolder = "/Drafts/Tags/";
    // The template to use naming the export files
    let fileNameTemplate = "[[safe_title]].md";
    // Temnplate to use for the content of the files
    let contentTemplate = "[[draft]]";
    // Array of tag names to always include in the prompt. Recently used tags will also be included.
    let alwaysIncludeTags = ["personal", "hold", "OneDrive"];
    
    // END configuration options
    
    let f = () => {
    	// merge tags to prompt for...
    	let tags = [...alwaysIncludeTags, ...Draft.recentTags()];
    	if (tags.length == 0) {
    		return false;
    	}	
    
    	// prompt to select a tag to export
    	let p = Prompt.create();
    	p.title = "Select Tag to Publish";
    	p.message = "All inbox drafts with the tag selected will be exported to files in the " + destinationFolder + " folder in OneDrive.";
    
    	for (let tag of tags) {
    		p.addButton(tag);
    	}
    	if (!p.show()) {
    		return false;
    	}
    	let tag = p.buttonPressed;
    
    	// query for inbox drafts with the designated tag
    	let drafts = Draft.query("", "inbox", [tag]);
    	if (drafts.length == 0) {
    		console.log("No drafts matching tag found");
    		return true;
    	}
    
    	// write each draft to OneDrive.
    	let fm = OneDrive.create();
    	for (let d of drafts) {
    		let fileName = d.processTemplate(fileNameTemplate);
    		let path = destinationFolder + tag + "/" + fileName;
    		let content = d.processTemplate(contentTemplate);
    		if (fm.write(path, content)) {
    			console.log("Created file: " + path);
    		}
    		else {
    			console.log("Error creating file: " + path);
    			return false;
    		}
    	}
    
    	alert("Published " + drafts.length + " files to " + destinationFolder) + " in the Drafts OneDrive folder";
    	return true;	
    }
    
    if (!f()) {
    	context.fail();
    }

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.