🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #8125

Re: textCleanUp and graphics

#8125
From: Olav_Kvern
Date: Fri, 03 Nov 2006 20:23
163 lines
8238 bytes
Hi Ben,

Here's a JavaScript version that might help. In this version, the tag used for graphics is in the form <graphic: filePath>--you can change the regular expression to search for other tagging schemes. You'll probably also want to change the sizing/fitting in the function myPlaceGraphic.



//ReplaceTextWithGraphics_CS2.jsx  
//An InDesign CS2 JavaScript  
//  
//Finds a string in text and places a graphic based on the  
//contents of the string. This script employs a hybrid approach to  
//finding and changing text, using both JavaScript regular expressions  
//and InDesign�s search method.  
//  
main();  
function main(){  
   if(app.documents.length != 0){  
      if(app.documents.item(0).stories.length != 0){  
         //Construct regular expression to find hyperlink sources. 
         //The following RegExp should find all URLs in text. 
         var myRegExp = /<graphic:\s*.*?>/gi; 
         mySearchStories(myRegExp); 
      }  
      else{  
         alert("The current document does not contain any text.");  
      }  
   }  
   else{  
      alert("Please open a document and try again.");  
   } 
   alert("Done!'); 
}  
function mySearchStories(myRegExp){  
   var myStory, myStoryText, myFoundItems, myFoundItem, myFoundStrings, myGraphicPath, myFootnoteCounter; 
   var myStart, myEnd, myFootnote, myStringCounter, myFoundItem, myTableCounter, myCellCounter, myTable, myCell;  
   var myFoundStrings = new Array;  
   var myDocument = app.documents.item(0);  
   var myOldXUnits = myDocument.viewPreferences.horizontalMeasurementUnits; 
   var myOldYUnits = myDocument.viewPreferences.verticalMeasurementUnits;    
   myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; 
   myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; 
   myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;    
   for(var myCounter = 0; myCounter < myDocument.stories.length; myCounter ++){  
      //Get the text of the story as a string.  
      myStory = myDocument.stories.item(myCounter); 
      if(myStory.characters.length > 1){ 
         myStoryText = myStory.contents; 
         //If the story contains only a special character (such as an auto page number marker), 
         //the following will fail, so use try...catch to avoid the problem. 
         try{ 
            if(myStoryText.match(myRegExp)!=null){ 
               myFoundStrings = myFoundStrings.concat(myStoryText.match(myRegExp)); 
            } 
         } 
         catch (myError){} 
      } 
      //The text of tables in the story will not appear in myStory.contents, so 
      //we need to search the text of each story. There�s always the possibility that 
      //the cells contain tables of their own, but this script won�t check for that possibility. 
      if(myStory.tables.length != 0){ 
         for(myTableCounter = 0; myTableCounter < myStory.tables.length; myTableCounter++){ 
            myTable = myStory.tables.item(myTableCounter); 
            for(myCellCounter = 0; myCellCounter < myTable.cells.length; myCellCounter++){ 
               myCell = myTable.cells.item(myCellCounter); 
               //Does the cell contain any text? If it does, the length of the 
               //insertion points collection will be greater than 1. 
               if(myCell.insertionPoints.length > 1){ 
                  myCellText = myCell.texts.item(0).contents; 
                  if(myCellText.match(myRegExp)!= null){ 
                     myFoundStrings = myFoundStrings.concat(myCell.texts.item(0).contents.match(myRegExp)); 
                  } 
               } 
            } 
         } 
      } 
   } 
   if(myFoundStrings.length > 0){ 
      myFoundStrings = myRemoveDuplicates(myFoundStrings);  
      //Search through the sorted strings in reverse order. 
      for(myStringCounter = myFoundStrings.length-1; myStringCounter >= 0; myStringCounter--){  
         myClearFindChange();  
         myFoundItems = myDocument.search(myFoundStrings[myStringCounter]);  
         if(myFoundItems.length != 0){  
            for(myTextCounter = myFoundItems.length-1; myTextCounter >= 0; myTextCounter--){  
               myFoundItem = myFoundItems[myTextCounter]; 
               myPlaceGraphic(myFoundItem, myFoundStrings[myStringCounter]); 
            }  
         }  
         myClearFindChange();  
      } 
   } 
   myFoundString = new Array; 
   //Handle the footnotes here. 
   //Iterate through the stories again. 
   for(var myCounter = 0; myCounter < myDocument.stories.length; myCounter ++){  
      myStory = myDocument.stories.item(myCounter); 
      if(myStory.footnotes.length > 0){ 
         for(myFootnoteCounter = 0; myFootnoteCounter < myStory.footnotes.length; myFootnoteCounter++){ 
            myFootnote = myStory.footnotes.item(myFootnoteCounter); 
            myStoryText = myFootnote.texts.item(0).contents; 
            try{ 
               if(myStoryText.match(myRegExp)!=null){ 
                  myFoundStrings = myStoryText.match(myRegExp); 
                  //Found at least one matching string in the footnote, so use 
                  //itemByRange to construct a text object, then use that object to 
                  //create a hyperlink text source. 
                  for(myStringCounter = 0; myStringCounter < myFoundStrings.length; myStringCounter++){ 
                     myFoundString = myFoundStrings[myStringCounter]; 
                     myStart = myStoryText.indexOf(myFoundString); 
                     myEnd = myStart + myFoundString.length-1; 
                     myFoundItem = myFootnote.texts.itemByRange(myFootnote.characters.item(myStart), myFootnote.characters.item(myEnd)); 
                     myPlaceGraphic(myFoundItem, myFoundString); 
                  } 
               } 
            } 
            catch (myError){} 
         } 
      } 
   } 
   myDocument.viewPreferences.horizontalMeasurementUnits = myOldXUnits; 
   myDocument.viewPreferences.verticalMeasurementUnits = myOldYUnits; 
} 
function myPlaceGraphic(myText, myFilePath){ 
   var myDocument = app.documents.item(0); 
   var myRegExp = /<graphic:\s*(.*?)>/gi; 
   var myFilePath = myFilePath.replace(myRegExp, "$1"); 
   try{ 
      var myX1 = myText.characters.item(0).horizontalOffset; 
      var myY1 = myText.characters.item(0).baseline; 
      var myX2 = myX1 + 72; 
      var myY2 = myY1 + 72; 
      var myInlineGraphic = myText.place(File(myFilePath)); 
      myInlineGraphic.parent.parent.parentStory.recompose(); 
      myInlineGraphic.parent.geometricBounds = [myY1, myX1, myY2, myX2]; 
      myInlineGraphic.parent.fit(FitOptions.proportionally); 
      myInlineGraphic.parent.fit(FitOptions.frameToContent); 
   } 
   catch(myError){} 
} 
function myRemoveDuplicates(myArray){  
   //Semi-clever method of removing duplicate array items; much faster  
   //than comparing every item to every other item!  
   var myNewArray = new Array;  
   myArray = myArray.sort();  
   myNewArray.push(myArray[0]);  
   if(myArray.length > 1){  
      for(var myCounter = 1; myCounter < myArray.length; myCounter ++){  
         if(myArray[myCounter] != myNewArray[myNewArray.length -1]){  
            myNewArray.push(myArray[myCounter]);  
         }  
      }  
   }  
   return myNewArray;  
}  
function myClearFindChange(){  
   app.findPreferences = NothingEnum.nothing;  
   app.findPreferences = NothingEnum.nothing;     
} 


Thanks,

Ole
Technical Details
Message-ID: <3bc1fbdd.8@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!news2.volia.net!news.musoftware.de!news.albasani.net!news.isoc.lu!webcrossing
References: <3bc1fbdd.-1@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,627e7574b8c670c8
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII
From: Olav_Kvern@adobeforums.com
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!news2.volia.net!news.musoftware.de!news.albasani.net!news.isoc.lu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: Re: textCleanUp and graphics
Message-ID: <3bc1fbdd.8@webcrossing.la2eafNXanI>
Date: Fri, 3 Nov 2006 12:23:55 -0800
References: <3bc1fbdd.-1@webcrossing.la2eafNXanI>
Lines: 268
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit