Article View: adobe.scripting.indesign
Article #1221Re: Using Grep to find text pattern, get found text, and get page number found on
From: robin521
Date: Mon, 16 Feb 2009 03:33
Date: Mon, 16 Feb 2009 03:33
19 lines
2924 bytes
2924 bytes
Hi Peter,
Thank you,
<pre>function myReadPMTags(myStory){
var myName, myString, myStyle, myStyleName;
var myDocument = app.documents.item(0);
//Reset the findGrepPreferences to ensure that previous settings
//do not affect the search.
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
//Find the tags (since this is a JavaScript string,
//the backslashes must be escaped).
app.findGrepPreferences.findWhat = "[^A-Za-z0-9/.-]+";
var myFoundItems = myStory.findGrep();
if(myFoundItems.length != 0){
var myFoundTags = new Array;
for(var myCounter = 0; myCounter<myFoundItems.length; myCounter++){ myFoundTags.push(myFoundItems[myCounter].contents); } myFoundTags = myRemoveDuplicates(myFoundTags); //At this point, we have a list of tags to search for. for(myCounter = 0; myCounter < myFoundTags.length; myCounter++){ myString = myFoundTags[myCounter]; //Find the tag using findWhat. app.findTextPreferences.findWhat = myString; //Extract the style name from the tag. myStyleName = myString.substring(1, myString.length-1); //Create the style if it does not already exist. try{ myStyle = myDocument.paragraphStyles.item(myStyleName); myName = myStyle.name; } catch (myError){ myStyle = myDocument.paragraphStyles.add({name:myStyleName}); } //Apply the style to each instance of the tag. app.changeTextPreferences.appliedParagraphStyle = myStyle; myStory.changeText(); //Reset the changeTextPreferences. app.changeTextPreferences = NothingEnum.nothing; //Set the changeTo to an empty string. app.changeTextPreferences.changeTo = ""; //Search to remove the tags. myStory.changeText(); //Reset the find/change preferences again. app.changeTextPreferences = NothingEnum.nothing; } } //Reset the findGrepPreferences. app.findGrepPreferences = NothingEnum.nothing; } 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; } </pre>
Technical Details
Message-ID:
<59b7e2c7.8@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newshub.sdsu.edu!webcrossingReferences:
<59b7e2c7.-1@webcrossing.la2eafNXanI>Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,c068c4e91c9ba325 X-Google-Attributes: gidf939a,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: robin521@adobeforums.com Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newshub.sdsu.edu!webcrossing Newsgroups: adobe.scripting.indesign Subject: Re: Using Grep to find text pattern, get found text, and get page number found on Message-ID: <59b7e2c7.8@webcrossing.la2eafNXanI> Date: Sun, 15 Feb 2009 19:33:36 -0800 References: <59b7e2c7.-1@webcrossing.la2eafNXanI> Lines: 42 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit