SHARE
TWEET

Illustrator Title Case Script

Cronos Jul 1st, 2012 141 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Title Caps
  3.  * Ported to Illustrator By Carlos Canto - 6/30/12
  4.  * Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008
  5.  * Original by John Gruber - http://daringfireball.net/ - 10 May 2008
  6.  * License: http://www.opensource.org/licenses/mit-license.php
  7.  */
  8.  
  9. // UPDATE 7/12/12, increased the exclusion list of prepositions, articles and conjuntions.
  10. //                                 - added an option to run the script on a selection of text, along with a selection of single or multiple text frames.
  11.  
  12. #target Illustrator
  13.  
  14. function titleCase(str){
  15.         //var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
  16.         var small = "(a|abaft|aboard|about|above|absent|across|afore|after|against|along| alonside|amid|amidst|among|amongst|an|and|apopos|around|as|aside|astri de|at|athwart|atop|barring|before|behind|below|beneath|beside|besides| between|betwixt|beyond|but|by|circa|concerning|despite|down|during|exc ept|excluding|failing|following|for|from|given|in|including|inside|int o|lest|like|mid|midst|minus|modula|near|next|nor|notwithstanding|of|of f|on|onto|oppostie|or|out|outside|over|pace|per|plus|pro|qua|regarding |round|sans|save|than|that|the|through|throughout|till|times|to|toward |towards|under|underneath|unlike|until|unto|up|upon|versus|via|vice|wi th|within|without|worth|v[.]?|via|vs[.]?)";
  17.         var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
  18.  
  19.         var casedChanged = titleCaps (str);
  20.        
  21.         function titleCaps (title){
  22.                 var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
  23.                
  24.                 while (true) {
  25.                         var m = split.exec(title);
  26.  
  27.                         parts.push( title.substring(index, m ? m.index : title.length)
  28.                                 .replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
  29.                                         return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
  30.                                 })
  31.                                 .replace(RegExp("\\b" + small + "\\b", "ig"), lower)
  32.                                 .replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
  33.                                         return punct + upper(word);
  34.                                 })
  35.                                 .replace(RegExp("\\b" + small + punct + "$", "ig"), upper));
  36.                        
  37.                         index = split.lastIndex;
  38.                        
  39.                         if ( m ) parts.push( m[0] );
  40.                         else break;
  41.                 }
  42.                
  43.                 return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
  44.                         .replace(/(['Õ])S\b/ig, "$1s")
  45.                         .replace(/\b(AT&T|Q&A)\b/ig, function(all){
  46.                                 return all.toUpperCase();
  47.                         });
  48.         };
  49.    
  50.         function lower(word){
  51.                 return word.toLowerCase();
  52.         }
  53.    
  54.         function upper(word){
  55.           return word.substr(0,1).toUpperCase() + word.substr(1);
  56.         }
  57.  
  58.         return casedChanged;
  59. };
  60.  
  61. var idoc = app.activeDocument;
  62. var sel = idoc.selection;
  63.  
  64. if (sel.typename == "TextRange") {
  65.     sel.changeCaseTo(CaseChangeType.TITLECASE);
  66.     sel.contents = titleCase(sel.contents);
  67. }
  68.  
  69. else if (sel.length>0) {
  70.         for (i=0; i<sel.length; i++) {
  71.                 if(sel[i].typename == "TextFrame") {
  72.                         var itext = sel[i];
  73.                         itext.textRange.changeCaseTo(CaseChangeType.TITLECASE);
  74.                         itext.contents = titleCase(itext.contents);
  75.                 }
  76.         }
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top