Action

Text Modifier…

Posted by @nahumck, Last update 5 months ago

Transform selected text based on a variety of prompted styles.

Steps

  • script

    // Text Modifiers
    
    /* --- functions used --- */
    // Remove or comment out (//) the ones you don't want to use
    var mods = [
    "Encode", 
    "Decode",
    "Hyphenate",
    "Small Caps",
    "Reverse Small Caps", 
    "Superscript",
    "Subscript",
    "lower case",
    "UPPER CASE",
    "Title Case",
    "aLtErNaTiNg CaSe",
    "Encircle",
    "NATO",
    "Flip",
    "Strikeout"
    ];
    
    /* Scripts taken from Drafts 4 and ported to Drafts 5 */
    /* Action created by Tim Nahumck */
  • script

    /* ------Functions------ */
    
    // Title Case Function
    /* --------------------- */
    /* To Title Case 2.1 – http://individed.com/code/to-title-case/ */
    /* Copyright © 2008–2013 David Gouch. Licensed under the MIT License. */
    
    String.prototype.toTitleCase = function(){
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
     
      return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        }
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        }
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    
    //Small Caps Function
    /* --------------------- */
    // uses Unicode characters
    function smallCapitalize(input) {
      var out = '';
      for( var i = 0; i <= input.length - 1; i++ ) {
        var ch = input.charAt( i );
        if( ch == 'a' || ch == 'A' ) { out += '\u1D00' }
        else if( ch == 'b' || ch == 'B' ) { out += '\u0299' }
        else if( ch == 'c' || ch == 'C' ) { out += '\u1D04' }
        else if( ch == 'd' || ch == 'D' ) { out += '\u1D05' }
        else if( ch == 'e' || ch == 'E' ) { out += '\u1D07' }
        else if( ch == 'f' || ch == 'F' ) { out += '\u0493' }
        else if( ch == 'g' || ch == 'G' ) { out += '\u0262' } 
        else if( ch == 'h' || ch == 'H' ) { out += '\u029C' }
        else if( ch == 'i' || ch == 'I' ) { out += '\u026A' }
        else if( ch == 'j' || ch == 'J' ) { out += '\u1D0A' }
        else if( ch == 'k' || ch == 'K' ) { out += '\u1D0B' }
        else if( ch == 'l' || ch == 'L' ) { out += '\u029F' }
        else if( ch == 'm' || ch == 'M' ) { out += '\u1D0D' }
        else if( ch == 'n' || ch == 'N' ) { out += '\u0274' }
        else if( ch == 'o' || ch == 'O' ) { out += '\u1D0F' }
        else if( ch == 'p' || ch == 'P' ) { out += '\u1D18' }
        else if( ch == 'q' || ch == 'Q' ) { out += '\u01EB' }
        else if( ch == 'r' || ch == 'R' ) { out += '\u0280' }
        else if( ch == 's' || ch == 'S' ) { out += 's' }
        else if( ch == 't' || ch == 'T' ) { out += '\u1D1B' }
        else if( ch == 'u' || ch == 'U' ) { out += '\u1D1C' }
        else if( ch == 'v' || ch == 'V' ) { out += '\u1D20' }
        else if( ch == 'w' || ch == 'W' ) { out += '\u1D21' }
        else if( ch == 'x' || ch == 'X' ) { out += 'x' }
        else if( ch == 'y' || ch == 'Y' ) { out += '\u028F' }
        else if( ch == 'z' || ch == 'Z' ) { out += '\u1D22' }
        else { out += ch }
        }
      return out;
    }
    
    //Small Caps Function
    /* --------------------- */
    // uses Unicode characters
    function reverseSmallCapitalize(input) {
    var out = '';
    for( var i = 0; i <= input.length - 1; i++ ) {
    var ch = input.charAt( i );
    if( ch == '\u1D00' ) { out += 'a' }
    else if( ch == '\u0299' ) { out += 'b' }
    else if( ch == '\u1D04' ) { out += 'c' }
    else if( ch == '\u1D05' ) { out += 'd' }
    else if( ch == '\u1D07' ) { out += 'e' }
    else if( ch == '\u0493' ) { out += 'f' }
    else if( ch == '\u0262' ) { out += 'g' } 
    else if( ch == '\u029C' ) { out += 'h' }
    else if( ch == '\u026A' ) { out += 'i' }
    else if( ch == '\u1D0A' ) { out += 'j' }
    else if( ch == '\u1D0B' ) { out += 'k' }
    else if( ch == '\u029F' ) { out += 'l' }
    else if( ch == '\u1D0D' ) { out += 'm' }
    else if( ch == '\u0274' ) { out += 'n' }
    else if( ch == '\u1D0F' ) { out += 'o' }
    else if( ch == '\u1D18' ) { out += 'p' }
    else if( ch == '\u01EB' ) { out += 'q' }
    else if( ch == '\u0280' ) { out += 'r' }
    else if( ch == 's' ) { out += 's' }
    else if( ch == '\u1D1B' ) { out += 't' }
    else if( ch == '\u1D1C' ) { out += 'u' }
    else if( ch == '\u1D20' ) { out += 'v' }
    else if( ch == '\u1D21' ) { out += 'w' }
    else if( ch == 'x' ) { out += 'x' }
    else if( ch == '\u028F' ) { out += 'y' }
    else if( ch == '\u1D22' ) { out += 'z' }
    else { out += ch }
    }
    return out;
    }
    
    //Superscript Function
    /* --------------------- */
    // uses Unicode characters
    function superscript(s) {
      var pchars = "0123456789n"
        , cchars = "⁰¹²³⁴⁵⁶⁷⁸⁹ⁿ"
        , count  = pchars.length
        , regex  = new RegExp('[' + pchars + ']', 'g')
        , trans  = {}
        , lookup = function(c) { return trans[c] || c; };
      
      for (var i=0; i<count; i++) {
        trans[pchars[i]] = cchars[i];
      }
      return s.replace(regex, lookup);
    }
    
    //Subscript Function
    /* --------------------- */
    // uses Unicode characters
    function subscript(s) {
      var pchars = "0123456789"
        , cchars = "₀₁₂₃₄₅₆₇₈₉"
        , count  = pchars.length
        , regex  = new RegExp('[' + pchars + ']', 'g')
        , trans  = {}
        , lookup = function(c) { return trans[c] || c; };
      
      for (var i=0; i<count; i++) {
        trans[pchars[i]] = cchars[i];
      }
      return s.replace(regex, lookup);
    }
    
    //Hyphenate Function
    /* --------------------- */
    function hyphenate(s) {
      var f       = ' '
        , r       = '-'
        , re      = new RegExp(f, 'g')
        , matches = s.match(re);
      if (matches) {
        return s.replace(re,r);
      }
      else {
        app.displayWarningMessage("No spaces in selection")
        return s
       
      }  
    }
    
    //Encircle Function
    /* --------------------- */
    function encircle(s) {
      var pchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        , cchars = "ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏ⓪①②③④⑤⑥⑦⑧⑨"
        , count  = pchars.length
        , regex  = new RegExp('.', 'g')
        , trans  = {}
        , lookup = function(c) { return trans[c] || c; };
      for (var i=0; i<count; i++) {
        trans[pchars[i]] = cchars[i];
      }
      
      return s.replace(regex, lookup);
    }
    
    //NATO Function
    /* --------------------- */
    // uses Unicode characters
    function NATO(input) {
      var out = '';
      for( var i = 0; i <= input.length - 1; i++ ) {
        var ch = input.charAt( i );
        if( ch == 'a' || ch == 'A' ) { out += 'Alfa ' }
        else if( ch == 'b' || ch == 'B' ) { out += 'Bravo ' }
        else if( ch == 'c' || ch == 'C' ) { out += 'Charlie ' }
        else if( ch == 'd' || ch == 'D' ) { out += 'Delta ' }
        else if( ch == 'e' || ch == 'E' ) { out += 'Echo ' }
        else if( ch == 'f' || ch == 'F' ) { out += 'Foxtrot ' }
        else if( ch == 'g' || ch == 'G' ) { out += 'Golf ' } 
        else if( ch == 'h' || ch == 'H' ) { out += 'Hotel ' }
        else if( ch == 'i' || ch == 'I' ) { out += 'India ' }
        else if( ch == 'j' || ch == 'J' ) { out += 'Juliett ' }
        else if( ch == 'k' || ch == 'K' ) { out += 'Kilo ' }
        else if( ch == 'l' || ch == 'L' ) { out += 'Lima ' }
        else if( ch == 'm' || ch == 'M' ) { out += 'Mike ' }
        else if( ch == 'n' || ch == 'N' ) { out += 'November ' }
        else if( ch == 'o' || ch == 'O' ) { out += 'Oscar ' }
        else if( ch == 'p' || ch == 'P' ) { out += 'Papa ' }
        else if( ch == 'q' || ch == 'Q' ) { out += 'Quebec ' }
        else if( ch == 'r' || ch == 'R' ) { out += 'Romeo ' }
        else if( ch == 's' || ch == 'S' ) { out += 'Sierra ' }
        else if( ch == 't' || ch == 'T' ) { out += 'Tango ' }
        else if( ch == 'u' || ch == 'U' ) { out += 'Uniform ' }
        else if( ch == 'v' || ch == 'V' ) { out += 'Victor ' }
        else if( ch == 'w' || ch == 'W' ) { out += 'Whiskey ' }
        else if( ch == 'x' || ch == 'X' ) { out += 'X-ray ' }
        else if( ch == 'y' || ch == 'Y' ) { out += 'Yankee ' }
        else if( ch == 'z' || ch == 'Z' ) { out += 'Zulu ' }
        else if( ch == '0' || ch == 'Z' ) { out += 'Zero ' }
        else if( ch == '1' || ch == 'Z' ) { out += 'One ' }
        else if( ch == '2' || ch == 'Z' ) { out += 'Two ' }
        else if( ch == '3' || ch == 'Z' ) { out += 'Three ' }
        else if( ch == '4' || ch == 'Z' ) { out += 'Four ' }
        else if( ch == '5' || ch == 'Z' ) { out += 'Five ' }
        else if( ch == '6' || ch == 'Z' ) { out += 'Six ' }
        else if( ch == '7' || ch == 'Z' ) { out += 'Seven ' }
        else if( ch == '8' || ch == 'Z' ) { out += 'Eight ' }
        else if( ch == '9' || ch == 'Z' ) { out += 'Niner ' }
        else { out += ch }
        }
      return out;
    }
    
    //aLtErNaTiNg CaSe Function
    /* --------------------- */
    function alternateCase(s) {
    	var chars = s.toLowerCase().split("");
    	for (var i = 0; i < chars.length; i += 2) {
    		chars[i] = chars[i].toUpperCase();
    	}
    	return chars.join("");
    };
    
    //Strikeout Function
    /* --------------------- */
    function strikeout(unstruck) {
      var s = String.fromCharCode(824)
        , a = unstruck.split('');
      if (a.length > 0) {
        return a.join(s) + s;
      }
      else {
        return '';
      }
    }
    
    //Flip Text Function
    /* --------------------- */
    function flip(s) {
      var pchars = "abcdefghijklmnopqrstuvwxyz,.?!'(){}[]"
        , fchars = "ɐqɔpǝɟƃɥıɾʞlɯuodbɹsʇnʌʍxʎz'˙¿¡,)(}{]["
        , count  = pchars.length
        , regex  = new RegExp('.', 'g')
        , trans  = {}
        , t      = s.toLowerCase()
        , lookup = function(c) { return trans[c] || c; };
      for (var i=0; i<count; i++) {
        trans[pchars[i]] = fchars[i];
      }
      var a = t.split("");
      a.reverse();
      return a.join("").replace(regex, lookup);
    }
  • script

    /* ------ Prompt ------ */
    
    var p = Prompt.create();
    p.title = "Text Modifier";
    for (var mod of mods) {
      p.addButton(mod);
    }
    var con = p.show();
  • script

    // Actions if not cancelled
    if (con) {
      var s = editor.getSelectedText();
      var selRange = editor.getSelectedRange();
      if (p.buttonPressed == "lower case") {
        var text = s.toLowerCase();
      }
      if (p.buttonPressed == "UPPER CASE") {
        var text = s.toUpperCase();
      }
      if (p.buttonPressed == "Title Case") {
        var text = s.toTitleCase();
      }
      if (p.buttonPressed == "Small Caps") {
        var text = smallCapitalize(s);
      }
      if (p.buttonPressed == "Reverse Small Caps") {
        var text = reverseSmallCapitalize(s);
      }
      if (p.buttonPressed == "Superscript") {
        var text = superscript(s);
      }
      if (p.buttonPressed == "Subscript") {
        var text = subscript(s);
      }
      if (p.buttonPressed == "Encode") {
        var text = encodeURIComponent(s);
      }
      if (p.buttonPressed == "Decode") {
        var text = decodeURIComponent(s);
      }
      if (p.buttonPressed == "Hyphenate") {
        var text = hyphenate(s);
      }
      if (p.buttonPressed == "Encircle") {
        var text = encircle(s);
      }
      if (p.buttonPressed == "NATO") {
        var text = NATO(s);
      }
      if (p.buttonPressed == "aLtErNaTiNg CaSe") {
        var text = alternateCase(s);
      }
      if (p.buttonPressed == "Strikeout") {
        var text = strikeout(s);
      }
      if (p.buttonPressed == "Flip") {
        var text = flip(s);
      }
      editor.setSelectedText(text);
      editor.setSelectedRange(selRange[0]+text.length,0);
      editor.activate();
    }
    else {
      context.cancel();
    }

Options

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