Update tags w/ predef tags stored in iCloud

Posted by sorashima, Last update about 1 month ago

Steps

  • script

    const FILEPATH = "/predefinedTagsCloud/predefinedTags.json";
    
    // read predefined tags from JSON file in iCloud
    let fmCloud = FileManager.createCloud();
    let content = fmCloud.readString(FILEPATH);
    if (content.length == 0) {
      content = JSON.stringify([]);
      fmCloud.writeString(FILEPATH, content);
    }
    let baseTags = JSON.parse(content);
    
    
    let currentTags = draft.tags;
    let allTags = baseTags.concat(currentTags).concat(Draft.recentTags());
    let selectableTags = Array.from(new Set(allTags));
    
    
    let nextAction= "assign";
    do {
      switch (nextAction) {
        case "assign":
          nextAction = assignTags();
          break;
        case "predef":
          nextAction = managePredefTags();
          break;
      }
    } while (nextAction != "Finish");
    
    
    
    function assignTags() {
      let p = Prompt.create();
      p.title = "Assign tags to draft";
      p.addSelect("tags","", selectableTags, currentTags, true);
      for (let i = 0; i < 5; i++) p.addTextField("additionalTag" + i, "Other", "", {"placeholder": "one tag"});
      p.addButton("Update", "update");
      p.addButton("→Manage predefined tag mode", "predef");
    
      let nextAction = "Finish";
      let cancelled = !p.show();
      switch (true) {
        case (p.buttonPressed == "update") : {
          let selectedTags = p.fieldValues["tags"];
    
          for (let tag of currentTags) {
            draft.removeTag(tag);
          }
          for (let tag of selectedTags) {
            draft.addTag(tag);
          }
    
          for (let i = 0; i < 5; i++) if (p.fieldValues["additionalTag" + i] != "") draft.addTag(p.fieldValues["additionalTag" + i]);
    
          draft.update();
        
          break;
        }      
        case (p.buttonPressed == "predef") : {
          nextAction = "predef";
          break;
        }
      }
      
      return nextAction;
    }
    
    
    
    function managePredefTags() {
      let p = Prompt.create();
      p.title = "Manage predefined tags";
      selectableTags.forEach(tag => p.addSwitch(tag, tag, baseTags.includes(tag)));
      for (let i = 0; i < 5; i++) p.addTextField("additionalTag" + i, "Other", "", {"placeholder": "one tag"});
      p.addButton("Save", "save");
      p.addButton("→Assign tags to draft mode", "assign");
     
      let nextAction = "Finish";
      let cancelled = !p.show();
      switch (true) {
        case (p.buttonPressed == "save") : {
          let selectedTags = selectableTags.filter(tag => p.fieldValues[tag]);
    
         let tagsToSave = selectedTags;
         for (let i = 0; i < 5; i++) if (p.fieldValues["additionalTag" + i] != "") tagsToSave.push(p.fieldValues["additionalTag" + i]);
         let lowertagsToSave = tagsToSave.map(tag => tag.toLowerCase());
         content = Array.from(new Set(lowertagsToSave));
    
         // write predefined tags to JSON file in iCloud
         fmCloud.writeString(FILEPATH, JSON.stringify(content));
    
          break;
        }
        case (p.buttonPressed == "assign") : {
          nextAction = "assign";
          break;
        }
      }
       
      return nextAction;
    }
    
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.