InDesign javascript - First Instance of a Paragraph Style

Community Beginner ,
Nov 24, 2020 Nov 24, 2020

Copy link to clipboard

Copied

I have a document where I have scripted a GREP find \ change that has applied a specific paragraph style to a portion of the text.

Now I want to select the first instance that the paragraph style has been applied to.

Would I be able to select the first instance somehow or will I need to put all of the instances of the particular paragraph style into an array and select it that way?

Any suggestions welcome.

Thanks

TOPICS
How to, Scripting

Views

102

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 24, 2020 Nov 24, 2020

Copy link to clipboard

Copied

Hi Rory,

please define the best as you can "First Instance". What do you expect?

Do you assume that the first instance of a found text will show you the first instance in e.g. geometric order in a given document? If you do, you may assume wrong.

So, it would make sense to loop through the array of all found instances, note the page and the geometric position ( x/y value ) of the first insertion point of every found instance and later decide what's the "first one" in a given "coordinate system".

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 24, 2020 Nov 24, 2020

Copy link to clipboard

Copied

Hi Laubender

I have a long document of text like the below example

roryt91880286_0-1606222455413.png

You can see all of the gold text has a paragraph style applied to it and I want to select the first instance of that style (indicated with an arrow) that has the words "Subject Exhibition:".

What I want to do then is, above the first instance of that entry I will insert a new header and that will allow me to then remove the repetition of the word "Subject Exhibition:" from each entry, so it will then look like the below:

2020-11-24_22-09-43.png

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 24, 2020 Nov 24, 2020

Copy link to clipboard

Copied

Hi Rory,

I don't know next to nothing about your document.

So if the line that says: "Subject Exhibition:"you want to change can be found only one time per story I'd suggest to change the scope of your search to a single story so you could find the text in a differnt way:

 

Just loop all the paragraphs of that story and check the value of property appliedParagraphStyle ( the one that is applied to the paragraphs with the golden formatting in your screenshot ). The first one you'll find will be the one you want to change.

To complete the search loop all stories of the document.

 

FWIW: You never know or at least it's hard to predict where InDesign will find the first instance document wide, if you are using GREP Find/Change with the whole document as scope.

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 25, 2020 Nov 25, 2020

Copy link to clipboard

Copied

After having a look at the document I have figured out that I have a more reliable way of locating the exact position I want to target. For instance I know that the exact line of text I want to find is called "Subject Exhibition: Accounting and Finance" so I have done a GREP within InDesigns Find\Change and used a positive lookahead to get to the position at the start of the line. where I then insert the new header and give it a style.

The full Positive Lookahead is (?=Subject Exhibition: Accounting and Finance~b).

 

Screen Shot 2020-11-25 at 8.07.12 pm.png

When I run this find and change via the above InDesign dialog box is works as i want it to adding in the new header above the starting point:

2020-11-25_20-16-26.jpg

 

However, when I try the same GREP find and change via my js script I get no result. Is anyone able to explina when it isn't working in the script?

 

Below is the full script;

main();

function main (){

//----------------------------- SET THE PREFERENCES -----------------------------

//Clear Grep preferences
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

// set some options, add any that are needed
app.findChangeGrepOptions.includeLockedLayersForFind = true;
app.findChangeGrepOptions.includeLockedStoriesForFind = true;
app.findChangeGrepOptions.includeHiddenLayers = true;
app.findChangeGrepOptions.includeMasterPages = false;
app.findChangeGrepOptions.includeFootnotes = true;


//----------------------------- SUBJECT EXHIBITION -----------------------------


//**************************** ADD IN SUBJECT HEADER ****************************

//Find the first instance of Subject Exhibition
app.findGrepPreferences.findWhat = "(?=Subject Exhibition: Accounting and Finance~b)";
app.findGrepPreferences.appliedParagraphStyle = "Award Name";

//and add in the Section Header and Subject Exhibiton header above the found instance
app.changeGrepPreferences.changeTo = "Subject Exhibition~b$0";
app.changeGrepPreferences.appliedParagraphStyle = "Award Header";

//Run the find\Change
app.activeDocument.changeGrep ();

//Clear Grep preferences
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;

alert("Finished")

}

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines