Thread View: adobe.scripting.indesign
11 messages
11 total messages
Started by Ric_Miller
Wed, 11 Feb 2009 20:14
Using Grep to find text pattern, get found text, and get page number found on
Author: Ric_Miller
Date: Wed, 11 Feb 2009 20:14
Date: Wed, 11 Feb 2009 20:14
32 lines
1699 bytes
1699 bytes
Hi there,
I have been successful in finding text using grep and returning the text, but need to identify what page the text is on.
This example finds an email address. So far I have:
Set myInDesign = CreateObject("InDesign.Application.CS3")
Rem Clear the find/change preferences.
myInDesign.FindGrepPreferences = idNothingEnum.idNothing
myInDesign.ChangeGrepPreferences = idNothingEnum.idNothing
Rem Set the find options.
myInDesign.FindChangeGrepOptions.IncludeFootnotes = False
myInDesign.FindChangeGrepOptions.IncludeHiddenLayers = False
myInDesign.FindChangeGrepOptions.IncludeLockedLayersForFind = False
myInDesign.FindChangeGrepOptions.IncludeLockedStoriesForFind = False
myInDesign.FindChangeGrepOptions.IncludeMasterPages = False
Rem Regular expression for finding an email address.
myInDesign.FindGrepPreferences.FindWhat = "(?i)[A-Z]*?@[A-Z]*?[.]..."
Set t = myInDesign.FindGrep
For i = 1 to t.Count
Debug.Print t.Item(i).Contents
' How do I get the page number that the text was found on?
Next
Can anyone help with this?
Thanks,
Ric
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Dave_Saunders
Date: Wed, 11 Feb 2009 20:32
Date: Wed, 11 Feb 2009 20:32
9 lines
976 bytes
976 bytes
Ric, If you can read JavaScript, this has been answered quite recently here: Peter Kahrel, "Extracting info from Links panel" #7, 10 Feb 2009 12:16 pm </webx?14@@.59b7d3ce/6> If you're just concerned with text, the simpler version of the findWhere function (in message 7) will do the job for you. Dave
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Ric_Miller
Date: Wed, 11 Feb 2009 22:25
Date: Wed, 11 Feb 2009 22:25
40 lines
1526 bytes
1526 bytes
Hi Dave,
Thank you for the reference. I am trying to work with this code.
In my example above, would theObj be:
t.Item(i)
I translated the javascript to vb and that doesn't seem to be working.
When I pass it t.item(i) I get an error:
"Object doesn't support this property or method".
I must be passing the function the wrong object.
Function findPage(theObj)
If (theObj.hasOwnProperty("baseline")) Then
theObj = theObj.ParentTextFrames(0)
End If
While theObj <> Null And theObj <> Page
Set t = theObj.constructor
Select Case t
Case Null
Case Character: theObj = theObj.ParentTextFrames(0)
Case Cell: theObj = theObj.InsertionPoints(0).ParentTextFrames(0)
Case Application
End Select
findPage = theObj.Parent
Wend
End Function
Any ideas?
thanks,
Ric
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Dave_Saunders
Date: Wed, 11 Feb 2009 23:16
Date: Wed, 11 Feb 2009 23:16
5 lines
921 bytes
921 bytes
Ah, hasOwnProperty is a JavaScript only method. Since you know that you're passing text, you could just skip that -- eliminate the test and go straight to the parent text frame. Dave
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Ric_Miller
Date: Thu, 12 Feb 2009 01:44
Date: Thu, 12 Feb 2009 01:44
44 lines
2976 bytes
2976 bytes
Hi Dave, OK, so I tried all of these variations and got various errors: t.Item(1).InsertionPoints(0).ParentTextFrames(0) t.Item(1).ParentTextFrames(0) t.InsertionPoints(0).ParentTextFrames(0).item(1) t.ParentTextFrames(0).item(1) I would think that one of the first two would work since they are both text and the object browser shows ParentTextFrame as a property of text. What am I doing wrong. Thanks, Ric Quoting Dave Saunders <member@adobeforums.com>: A new message was posted by Dave Saunders in INDESIGN SCRIPTING -- Using Grep to find text pattern, get found text, and get page number found on Ah, hasOwnProperty is a JavaScript only method. Since you know that you're passing text, you could just skip that -- eliminate the test and go straight to the parent text frame. Dave ------------------------- View/reply at Using Grep to find text pattern, get found text, and get page number found on[1] Replies by email are OK. Use the unsubscribe[2] form to cancel your email subscription. Links: ------ [1] http://www.adobeforums.com/webx?13@@.59b7e2c7/2 [2] http://www.adobeforums.com/webx?280@@.59b7e2c7!folder=.eea52bc
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Dave_Saunders
Date: Thu, 12 Feb 2009 04:19
Date: Thu, 12 Feb 2009 04:19
3 lines
732 bytes
732 bytes
Is the found text overset? That will cause the parent text frames array to be of zero length. Dave
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: robin521
Date: Fri, 13 Feb 2009 03:08
Date: Fri, 13 Feb 2009 03:08
19 lines
2963 bytes
2963 bytes
think i have same pro with you,
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; }
No Response with my code,but no bug, i don't know why?
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: robin521
Date: Fri, 13 Feb 2009 03:10
Date: Fri, 13 Feb 2009 03:10
15 lines
2901 bytes
2901 bytes
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; }
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Peter Kahrel
Date: Fri, 13 Feb 2009 06:24
Date: Fri, 13 Feb 2009 06:24
9 lines
869 bytes
869 bytes
Robin, To make your code examples readable, put <pre> at the beginning of your code examples and </pre> at the end, and replace < with <. Your GREP is faulty: instead of [^A-Za-z0-9/.-] use [^A-Za-z0-9\\.-] Peter
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: 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>
Re: Using Grep to find text pattern, get found text, and get page number found on
Author: Peter Kahrel
Date: Mon, 16 Feb 2009 09:07
Date: Mon, 16 Feb 2009 09:07
4 lines
755 bytes
755 bytes
Don't replace the < in <pre> and </pre> with < Do replace "[^A-Za-z0-9/.-]+" with "[^A-Za-z0-9\\.-]+" Peter
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads