Action

Publish tag to iCloud Drive

Posted by agiletortoise, Last update over 4 years ago

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 “/Tags/“ sub folder of the Drafts iCloud Drive folder.

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 = "/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"];
    
    // END configuration options
    
  • script

    
    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 iCloud Drive.";
    
    	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 iCloud Drive.
    	let fm = FileManager.createCloud();
    	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 iCloud Drive 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.