Action

Insert Hashtags

Posted by sorashima, Last update almost 3 years ago

Select from the hashtag candidates saved in iCloud Drive and insert the hashtags at the cursor position.
Just like - Update tags w/ predef tags stored in iCloud | Drafts Directory

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 allTags = baseTags
    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 = "Insert hashtags into draft";
      p.addSelect("tags","", selectableTags, [], true);
      p.addButton("Insert", "insert");
      p.addButton("→Manage predefined tag mode", "predef");
    
      let nextAction = "Finish";
      let cancelled = !p.show();
      switch (true) {
        case (p.buttonPressed == "insert") : {
          let selectedTags = p.fieldValues["tags"];
          editor.setSelectedText(selectedTags.map(t => "#" + t).join(" "))
          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, true));
      for (let i = 0; i < 5; i++) p.addTextField("additionalTag" + i, "Other", "", {"placeholder": "one tag"});
      p.addButton("Save", "save");
      p.addButton("→Insert hashtags into 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 Info
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.