You must be logged in to post Login

Search Forums:


 






find text and get the page it is on

UserPost

2:10 am
July 20, 2011


simondawdry

Community Member

posts 4

Hi, 


I'm new to indesign scripting, and working with indesign server CS5 and Javascript.  

I'm working on a script to find text within an indd document, and retrieve the page(s) it appears on.  I have this so far, cobbled together from various sources and hopefully some kind soul will take pity on me and have a look to see where I'm going wrong!


result = (myFindResult + "is on page" + myPageName);myPageName = myFindResult.text.item(0).parentTextFrame.name;myFindResult = mySearch.item(0);mySearch = app.findTextPreferences.findWhat = "text";


Any suggestions gratefully received, 


Simon

8:32 am
July 20, 2011


Kasyan Servetsky

Kiev, Ukraine

Member

posts 65

Hi Simon,

Here is a simple example:

var doc = app.activeDocument;
// clear find change text preferences before search
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
// this is scripting equivalent of the 'Find What:' edit text field
app.findTextPreferences.findWhat = "text";
// search it in the active document
// you can also search it in almost everywhere: story, text frame, paragraph, line, word, etc
var finds = doc.findText();
if (finds.length > 0) { // something has been found
    // for the 1st found item display the name of the page where the 1st text frame (there can be several threaded frames) containing it is located
    alert("Found " + finds.length + " items, the first of them is on page " + finds[0].parentTextFrames[0].parentPage.name);
}
else { // found nothing
    alert("Nothing has been found");
}
// clear find change text preferences after search
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;

Hope this helps.
Kasyan

8:38 am
July 20, 2011


Kasyan Servetsky

Kiev, Ukraine

Member

posts 65

I wrote this in desktop version of InDesign so that to demonstrate my approach. Alerts will not work in InDesign server.

8:46 am
July 20, 2011


simondawdry

Community Member

posts 4

fantastic, many thanks for the reply Kaysan, I'll give that code a go and let you know how I get on :-)