home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Automation / Scripts / Break Apart Words.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  4.9 KB  |  175 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2002 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12.  
  13. /***************************************************************
  14. Author: Henry Lee 
  15. Revised by: Ken Villines
  16. ***************************************************************/
  17.  
  18. /***************************************************************
  19.  
  20. This is a simple function that takes an LMTextObject (point or
  21. box text) and converts the paragraph into words.  The words are
  22. created from converting the text object into individual characters
  23. then regrouped into words.
  24.  
  25. Functions:
  26.     breakApartWords(target)
  27.  
  28. Arguments:
  29.     <target> LMTextObject - The text object to be broken apart
  30.  
  31. ***************************************************************/
  32.  
  33. //comp = application.currentComposition;
  34. //objs = comp.selection;
  35.  
  36. /***************************************************************
  37. To change the behavior of this script, 
  38. make your changes below
  39. ***************************************************************/
  40. Console.hide();
  41. Console.clear();
  42.  
  43. if(application.compositions.length >= 1){
  44.     
  45.     comp = application.currentComposition;
  46.    
  47.    if(comp.selection.length < 1){
  48.    Console.show();
  49.     Console.clear();
  50.     Console.write("ERROR: Nothing is selected");
  51.     }else{
  52.        objs = comp.selection;
  53.     
  54.             if(objs.length > 1){
  55.                 Console.show();
  56.                  Console.clear();
  57.                  Console.write("ERROR: More then one object is selected");
  58.             }else{
  59.                 returnKey = /\r/;
  60.                 newLine = /\n/;
  61.                 formFeed = /\f/;
  62.      
  63.                 for (var i=0;i<objs.length;i++) {
  64.                       if(objs[i].toString() == "[object LMPointTextObject]" || 
  65.                           objs[i].toString() == "[object LMBoxTextObject]"){
  66.                               if(objs[i].text.search(returnKey) == (-1) &&
  67.                                       objs[i].text.search(newLine) == (-1) &&
  68.                                       objs[i].text.search(formFeed) == (-1)){
  69.                                   breakApartWords(comp.selection[i]);
  70.                             }else{
  71.                                   Console.show();
  72.                                 Console.clear();
  73.                                 Console.write("ERROR: A return character was found");
  74.                               }
  75.                    }else{
  76.                        Console.show();
  77.                        Console.clear();
  78.                        Console.write("ERROR: A text object was not Selected");
  79.                    }
  80.                  }
  81.              }
  82.          }    
  83. }else{
  84. Console.show();
  85. Console.clear();
  86. Console.write("ERROR: A Composition does not exist");
  87. }
  88.  
  89. /***************************************************************
  90. DO NOT EDIT BELOW THIS LINE
  91. ***************************************************************/
  92.  
  93.  
  94. // class definition
  95. function Word(x,y) {
  96.   this.startIndex = x;
  97.   this.endIndex = y;
  98. }
  99.  
  100. function breakApartWords(target) {
  101.  
  102. // regular expression: any word: [a-zA-Z]+
  103.  
  104.   var origText = target.text;
  105.   var results;
  106.   var pattern;
  107.   var startIndex = new Array;
  108.   var endIndex = new Array;
  109.   var temp;
  110.   var inWord = false;
  111.   var j=0;
  112.   var tempWord;
  113.   var tempWordArray = new Array;
  114.   var k=0;
  115.   var linenum=0;
  116.   var words = new Array;
  117.   
  118.   characterArray = target.convertIntoObjects();
  119.   characterGroup = comp.group(characterArray);
  120.   characterGroup.name = "Group of Words";
  121.   characters = characterGroup.objects;
  122.  
  123.   pattern = /\S/;
  124.   
  125.   for (var i=0; i<origText.length; i++) {
  126.     temp = origText.charAt(i);
  127.     if (temp.match(pattern)) { //not whitespace
  128.       if (!inWord) {
  129.         inWord = true;
  130.         words[j] = new Word(i-(2*linenum),0);
  131.       }
  132.     }
  133.     else { //white space
  134.       if (inWord) {
  135.         inWord = false;
  136.         words[j].endIndex = i-1-(2*linenum);
  137.         j++;
  138.       }
  139.     }
  140.     if (i==origText.length-1) { //there is no white space at the end of a text block
  141.       if (inWord) {
  142.         inWord = false;
  143.         words[j].endIndex = i-(2*linenum);
  144.         j++;    
  145.       }
  146.     }
  147.     //when text is broken up, the newline & return character codes are lost
  148.     //this creates a difference in the character indices between the original
  149.     //text block and the character array.
  150.     if ((origText.charCodeAt(i)==13)&&(origText.charCodeAt(i+1)==10)) {
  151.       linenum++;
  152.     }
  153.   }
  154.   
  155.   for (j=0; j<words.length;j++) {
  156.         k=0;
  157.         var tempName = "";
  158.         tempWordArray=new Array();
  159.         for (i=words[j].startIndex;i<=words[j].endIndex;i++) { 
  160.               tempWordArray[k]=characters[i];
  161.               tempName += characters[i].text;
  162.               k++;
  163.               var whiteSpace = characters[i+1];
  164.         }
  165.            
  166.            if(i+1 <= characters.length){ 
  167.             whiteSpace.deleteThisObject();
  168.            }
  169.            
  170.            var tempWord = comp.group(tempWordArray);
  171.         tempWord.name = tempName;
  172.   }
  173.   
  174.   return linenum;
  175. }
  176.