Action

Inbox Sweeper

Posted by agiletortoise, Last update over 5 years ago

The Inbox Sweeper action can move any drafts which have not been modified since a selected date from the inbox to the archive, optionally assigning a tag to those drafts which were archived.

The action will prompt for the cut off date (defaulting to one month ago), and will prompt to confirm the number of drafts which will be affected before continuing.

Steps

  • script

    let initialDate = Date.today().addMonths(-1);
    let initialTagSuggestion = "";
    
    let f = () => {
       let p = Prompt.create();
       p.title = "Inbox Sweeper";
       p.message = "Archive drafts from the inbox which have not be modified since before the selected date. Optionally assign a tag to those drafts when archiving.";
       
       p.addDatePicker("date", "Date", initialDate, {
           "mode": "date",
           "maximumDate": Date.today()
       });
       p.addTextField("tag", "Assign tag", initialTagSuggestion, {
           "placeholder": "optional tag",
           "autocapitalization": "none"
       });
       p.addButton("Continue");
       
       if (!p.show()) {
           return 1; // cancel
       }
       
       let cutoffDate = p.fieldValues["date"];
       let assignTag = p.fieldValues["tag"];
       let queriedDrafts = Draft.query("", "inbox", [], [], "modified", true);
       let filteredDrafts = queriedDrafts.filter(d => d.modifiedAt < cutoffDate);
       
       if (filteredDrafts.length == 0) {
           alert("No drafts older than the selected date found, no changes made.");
           return 1;
       }
    
       let confirm = Prompt.create();
       confirm.title = "Archive Drafts";
       var msg = `Found ${filteredDrafts.length} draft(s). Archive these drafts?`;
       if (assignTag.length > 0) {
           msg += `\n\nArchived drafts will be assigned the tag "${assignTag}"`;
       }
       confirm.message = msg;
       confirm.addButton("Archive Drafts");
       
       if (!confirm.show()) {
           return 1; // cancelled
       }
       
       for (let d of filteredDrafts) {
           d.isArchived = true;
           if (assignTag.length > 0) {
               d.addTag(assignTag);
           }
           d.update();
       }
       
       console.log(`Archived ${filteredDrafts.length} drafts(s).`); 
       return 0;
    };
    
    let result = f();
    switch (result) {
       case -1: context.fail(); break;
       case 1: context.cancel(); break;
    }

Options

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