Action

REMCO - People Drafts to Bear

Posted by sylumer, Last update over 2 years ago - Unlisted

v1.0

Steps

  • includeAction

    name
    TAD
  • script

    // SETTINGS
    
    //Prefix on Drafts tags tagged with someone's name.
    const PREFIX = "with";
    
    // Prefix on Bear note title of someone's name, and tagged with that name.
    const TITLE_PREFIX = "Talk to ";
    
    // Set this to true to move processed drafts to the archive.
    const ARCHIVE_ENABLED = true;
  • script

    // ADDITIONAL FUNCTIONS
    
    // Create an array of tags that begin with the passed in prefix.
    // Match is case insensitive
    function findTagPrefix(p_strPrefix)
    {
    	let strPrefix = p_strPrefix.toLowerCase();
    	let astrAllTags = draft.TA_tagFetchAll()
    	let astrPrefixedTags = [];
    	astrAllTags.map(strTag => {
    		if(strTag.toLowerCase().startsWith(strPrefix)) astrPrefixedTags.push(strTag);
    	});
    	return astrPrefixedTags;
    }
  • script

    // PROCESSING
    
    // Get all of the tags with the prefix
    let astrMatchingTags = findTagPrefix(PREFIX);
    // Initialise counters to display results at the end
    let intBearNotesCreated = 0;
    let intBearNotesUpdated = 0;
    
    // For every tag with the specified prefix, do some processing
    astrMatchingTags.map(strTag => {
    	// Build ann array with a single element of our tag
    	let astrTagged = [];
    	astrTagged.push(strTag);
    
    	// Find drafts in the inbox with the specified prefixed tag
    	let adraftsMatched = Draft.query("", "inbox", astrTagged, [], "created", true, false);
    	// Process every draft found
    	adraftsMatched.map(draftMatched => {
    		//Make use of the TADpoLe Bear class for working with the Bear app
    		let tb = new TadBear();
    
    		// Build our unprefixed tag
    		let strBearTag = strTag.slice(PREFIX.length);
    		// Build a note title based on the tag and the constant from the start
    		let strBearTitle = TITLE_PREFIX + strBearTag;
    		// Build a CSV list of drafts tags to use as Bear tags, swapping out the Bear tag for the prefixed tag
    		let strBearTags = draftMatched.tags.join(",").replaceAll(strTag, strBearTag);
    
    		// Look for existing bera notes with the correct tag and title
    		let abnmdResults = tb.TA_search(strBearTitle, strBearTag);
    		
    		// We have to check for an existing Bear note, and if one does exist, use that, otherwise create a new one.
    		if (abnmdResults == undefined || abnmdResults.length == 0)
    		{
    			// No matching bear note found, create a new one
    			tb.TA_create(strBearTitle, draftMatched.content, false, strBearTags, false, false, false, false, false, false, false);
    			// Increment counter
    			intBearNotesCreated = intBearNotesCreated + 1;
    		}
    		else
    		{
    			// Found at least one match, so let's take the first match ¯\_(ツ)_/¯
    			// and use that to append the draft content to.
    			tb.TA_addText(abnmdResults[0].identifier, "", false, draftMatched.content, false, "", "append", true, strBearTags, true, false, false, false, false, false)
    			// Increment counter
    			intBearNotesUpdated = intBearNotesUpdated + 1;
    		}
    
    		// Archive the draft if necessary
    		if (ARCHIVE_ENABLED)
    		{
    			draftMatched.isArchived = true;
    			draftMatched.update();
    		}
    	})
    });
    // Display an information message.
    app.displayInfoMessage(`BEAR > Created: ${intBearNotesCreated} | Updates: ${intBearNotesUpdated}`);

Options

  • After Success Default
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.