[Jsx] Apply Paragraph Style, then next style via scripting?

Explorer ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Is it possible via scripting to apply a Paragraph Style and next style?

 

Basically right now manually I select a text frame and then select all text. The text frame is linked to other text frames which the text flows to. I then right click on the paragraph style and select "Apply [Paragraph Style Name] and then Next Style" where [Paragraph Style Name] is the name of the paragraph style.

I would like to automate this process.

- I should be able to identify the first text frame via the text frames name (this should not be hard).

- Once the text frame is selected I need to select all of its text. Not exactly sure how to do this and insure it selects all the text included linked text frames.

- Once the text is selected I then apply the paragraph style. I assume something like app.selection.texts[0].appliedParagraphStyle =

app.activeDocument.paragraphStyles.item("[Insert Paragraph Style Name]");
The part I am unsure about is will that apply the next style or is there a certain way I need to call it to apply the next style.

 

Thanks in advance to anyone that helps out!

TOPICS
Scripting

Views

274

Likes

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

correct answers 1 Correct answer

Adobe Community Professional , Feb 02, 2022 Feb 02, 2022
Hi @JO_15 , would something like this work? //rename as needed var sn = "Paragraph Style 1" var s, ns; for(var i=0; i < app.activeDocument.stories.length; i++){ s = app.activeDocument.stories.item(i); for(var j=s.paragraphs.length-1; j > -1; j--){ ns=s.paragraphs.item(j).appliedParagraphStyle.nextStyle; if (s.paragraphs.item(j).appliedParagraphStyle.name == sn ) { s.paragraphs.item(j+1).appliedParagraphStyle = ns; } } }

Likes

Translate

Translate
Adobe Community Professional ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Hi @JO_15, I'm not sure exactly what you are trying to do. When I right-click on the paragraph style I don't see Apply [name] and then Next Style. I'm not sure how that would work either. My understanding is that a paragraph style's Next Style is just the style that gets applied if you press return after *typing* in the original paragraph style and pressing return. I might be wrong.

 

Do you want to be able to select a text frame, and set all text in first textframe to a paragraph style and automatically set all text in the second linked text frame to another paragraph style? Sorry for not understanding.

- Mark

Likes

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
Explorer ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Hi m1b,

 

If I select text inside of a text frame using CTRL-A (windows) and then right click on a paragraph style that has a defined next style I see the following dialogue:

JoJa15_0-1643786911499.png

You can see there is a section that says: Apply "Test Paragraph Style" then Next Style.
What I am trying to find out is, is it possible to apply that paragraph style and then next style (similar to the manual process above) programatically using an Indesign javascript script.

Likes

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
Explorer ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

I forgot to answer your second question. Yes I have several text frames that are linked. Lets say TextFrame1 - 10. I also have several different paragraph styles. Lets say paragraphStyle1-10. Each style is linked to the other in order i.e. paragraphStyle2 is the next style after paragraphStyle1. ParagraphStyle10 has paragraphStyle1 as the next style so it forms a loop. 

I am trying to programatically select all the linked text in TextFrame1-10 (I manually do this by selecting the text inside TextFrame1 and then using CTRL-A select all). Then I am trying to programatically apply paragraphStyle1 and Next Style similar to how I do it manually by right clicking on the paragraph style in the paragraph styles window and selecting Apply "Paragraph Style 1" then Next Style.

I hope that makes sense.

Likes

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 ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Okay that helps a lot. And yes I don't know why I couldn't see the ... then Next Style option before but I can see it now. Maybe my selection was wrong before.

The answer is yes! A script can get a text frame's paragraphs, set the first paragraph to para style X and all the rest to para style X's next style.

Can you explain how the document is set up? Does the script need to locate all 10 text frames? Or are they all the textframes in the document? And how would the script know which paragraph style to apply? eg. one way is to assign a script label to the text frames, say, "frame1", which will be associated with paragraph style "style1", etc. It needs to be logical for the script, or otherwise there could be an ordered list of paragraph style names. It's doable.

- Mark

Likes

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 ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Hi JoJa15

see for example into a script by Timothy Ariel Walden:

 

InDesign Quick Apply with Next Style
by Ariel, August 30, 2016

https://www.id-extras.com/indesign-quick-apply-with-next-style/

 

There are some scripts around that would work if there are no paragraph styles in style groups.

The code is from 2006 or before when no style groups were possible in InDesign.

 

One example where you can perhaps build on:

Nächstes Format anwenden
Martin Fischer, 28. Sep 2006

https://www.hilfdirselbst.ch/gforum/gforum.cgi?post=253739#253739

 

Mark, also see into this below to understand the task:

Can Apply Next Para Style be scripted?
Matt Mayerchak, February 4, 2014
https://creativepro.com/topic/can-apply-next-para-style-be-scripted/

 

 

Regards,
Uwe Laubender

( ACP )

Likes

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
Explorer ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Hi Uwe,

 

Thank you for the links and taking the time to help me out!

 

I am fairly new to InDesign so I am pretty sure I am not using Paragraph Style Groups. I just have a series of Paragraph Styles. 
I checked out the InDesign Quick Apply with Next Style by Ariel, August 20, 2016. Unfortunately when I downloaded it, it comes as a jsxbin file. So I am unable to look at it to try and learn how I can do it myself.

I then checked out Nächstes Format anwenden Martin Fischer, 28. Sep 2006. If I understand it correctly to apply the Paragraph Style then Apply Next Style functionality that InDesign does you need to in your code manually select the next style correct? So instead of calling one function with a parameter that specifies using the next style you need to manually program it. If I am understanding it correctly I would approach it as follows:
- Find the TextFrame that I need to start on.
- Select all the text in the TextFrame and linked TextFrames (similar to select all).
- Loop through the selections paragraphs. The first paragraph gets the first style, the next paragraph would get the next style, and so on until all paragraphs have had a style applied. 
Is that how I need to go about it?

Likes

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 ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Hi @JO_15 , would something like this work?

 

//rename as needed
var sn = "Paragraph Style 1"
var s, ns;

for(var i=0; i < app.activeDocument.stories.length; i++){  
    s = app.activeDocument.stories.item(i);  
    for(var j=s.paragraphs.length-1; j > -1; j--){  
        ns=s.paragraphs.item(j).appliedParagraphStyle.nextStyle; 
        if (s.paragraphs.item(j).appliedParagraphStyle.name == sn ) {
                s.paragraphs.item(j+1).appliedParagraphStyle = ns;  
        } 
    }  
} 

 

Screen Shot 38.png

 

Screen Shot 37.png

Likes

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
Explorer ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Hi Rob,

 

Thank you for the response and code example!

 

I think that should work.

So basically the only way to do ParagraphStyle then Apply Next Style in script is to manually code the functionality. At least that is how I am interpeting your code. 

I wish they would have had just a parameter you could add to appliedParagraphStyle that would make it apply the next style.
Thank you again for all your help!

Likes

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 ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Hi @JO_15, it's great to see you taking an interest in scripting! Looks like you have a few good examples to learn from.

Aas per my questions earlier, a lot still depends on how your document is set up. For example is it best for you to (a) select a text frame, run the script, then select another text frame and run script again, or (b) select multiple text frames and run script once, or (c) no need for selection, just apply script to every text frame in document at once, or (d) operate on stories, not text frames, etc. Knowing these things, a script can be written that requires the least effort to apply.

- Mark

Likes

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
Explorer ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

Thanks. I love taking a difficult problem, trying to break it down into small steps, and then finally making something work. Learning scripting in InDesign has been tough compared to just learning Javascript but it has been a fun challenge too. I am very appreciative that there seems like a great community here. I was not expecting so many responses so quickly.

 

Let me try to answer your questions. Part of the problem is I am very new to InDesign too so how I go about things may not be the best. I really need to take a tutorial series on InDesign so that I full understand its capabilities.

 

Basically I have a single page with ten text frames. I have a txt document (generated from Excel) that I use to fill those ten text frames. It auto flows past the first page creating a duplicate of the first page with different contents. Think of it like an address book with each page a different contact. I then select the first text frame on the first page, I get the text carret and then select all so that it selects all the text that is linked between all the ten text frames on each page. I then manually right click on the first paragraph style and do Apply Paragraph Style then Next Style. Here is an example of what my document looks like with the links.

document.png

 (a) I select one text frame. Then use CTRL-A to select all the text that has auto flowed through linked text frames. Ideally the script would select the one text frame using the text frames name, then select all text in that text frame including the linked text frames, and then apply the paragraph style, then the next style, and next style to each next paragraph.
(b) see above.
(c) I think ideally I just run the script once. The script knows what text frame to look for. It places a txt file in the frame and that auto flows through the linked frames (I already have that working). The script then auto selects all the text and applys the paragraph styles that are linked.
(d) I am not familiar with stories. I need to read up on what InDesign Stories are. I have a lot to learn. 🙂

Likes

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 ,
Feb 02, 2022 Feb 02, 2022

Copy link to clipboard

Copied

I'll assume that (a) there are no other text frames in the document, and (b) that the two paragraph styles you are applying are applied the same to each text frame. In that case, have a look at this script and I think you will understand.

I have used the paragraph style called "Style1" so you will need to change that in the script.

- Mark

/*
    Set Paragraph Styles Then Next Style.js

    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/jsx-apply-paragraph-style-then-next-style-via-scripting/m-p/12723300

*/

// edit this line to match the name of your paragraph style
var paragraphStyleName = 'Style1';


function main() {

    var doc = app.activeDocument;

    // apply to selection:
    var textFrames = doc.textFrames;

    if (!doc.paragraphStyles.itemByName(paragraphStyleName).isValid) {
        alert('The paragraph style "' + paragraphStyleName + '" wasn\'t found in the document.');
        return;
    };

    var paraStyle = doc.paragraphStyles.itemByName(paragraphStyleName),
        nextStyle = paraStyle.nextStyle;

    // iterate over the text frames
    for (var i = 0; i < textFrames.length; i++) {

        // iterate over the paragraphs in the current text frame
        for (var j = 0; j < textFrames[i].paragraphs.length; j++) {

            if (j == 0) {
                // first paragraph of text frame
                textFrames[i].paragraphs[j].appliedParagraphStyle = paraStyle;
            } else {
                // other paragraphs
                textFrames[i].paragraphs[j].appliedParagraphStyle = nextStyle;
            }
            textFrames[i].paragraphs[j].clearOverrides(OverrideType.ALL);

        }
    }

} // end main


app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Set Paragraph Then Next Style");

  

Likes

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 ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

Then use CTRL-A to select all the text that has auto flowed through linked text frames.

 

If you have the threaded text selected, then you should be able to simply loop thru the selection:

 

 

 

var sn = "Paragraph Style 1"
var s = app.documents[0].selection[0];
var ns;

for (var i = 0; i < s.paragraphs.length-1; i++){
    ns=s.paragraphs.item(i).appliedParagraphStyle.nextStyle; 
    if (s.paragraphs.item(i).appliedParagraphStyle.name == sn ) {
            s.paragraphs.item(i+1).appliedParagraphStyle = ns;  
    } 
};

 

 

 

 

Screen Shot 48.pngScreen Shot 49.png

Likes

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 ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

There is an Apply Next Style in the paragraph Style section of Object Styles, but I don’t think it will work on threaded stories:

 

 

Screen Shot 44.png

 

Here AHeads Next Style is BHead, BHead’s next style is Text, and Text’s next style is AHead—the styles loop when the Object Style is applied to the frame

 

Screen Shot 45.png

 

 

Likes

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 ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

LATEST

"…but I don’t think it will work on threaded stories:"

 

Right. Object styles with Next Style will only work in stories with one text frame.

 

Regards,
Uwe Laubender

( ACP )

Likes

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 ,
Feb 03, 2022 Feb 03, 2022

Copy link to clipboard

Copied

The ParagraphStyle object does have a nextStyle property, which I’m applying in the loop—I don’t think there would be away to aviod the for loop.

Likes

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