• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script - Find paragrapf style and change with apply my style then next style

New Here ,
Nov 26, 2024 Nov 26, 2024

Copy link to clipboard

Copied

Hi community,

i would like to find a solution for the next problem

I have a 400 pages book with a text set up in this way:

paragraph style "Subtitle 1" then

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

(the number of paragraphs "Body text" isn't a fix number

...

paragraph style "Subtitle 1" then

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

paragraph style "Body text"

etc.

...

I would like to find, in selection text, all paragraps "Body text" between each "paragraph style "Subtitle 1" and apply paragraph styles in this way

"My new style 1"

than next style "My new style 2"

then next style "My new style 3"

than next style "My new style 2"

then next style "My new style 3"

....

until the next paragraph style "Subtitle 1" 
and than repeat all until the and of selection

Is this possible with a script?
Thank you in advance
Marko

 

 

TOPICS
Scripting

Views

95

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

Community Beginner , Nov 26, 2024 Nov 26, 2024

You can tweek it to suit your needs better:

 

app.doScript(Main, language=ScriptLanguage.JAVASCRIPT, withArguments=[], 
    undoMode=UndoModes.ENTIRE_SCRIPT, undoName='Testing');

function Main(){
    //reference to each style
    const titleStyle = app.activeDocument.paragraphStyles.itemByName('Title')
    const textStyle1 = app.activeDocument.paragraphStyles.itemByName('Text1')
    const textStyle2 = app.activeDocument.paragraphStyles.itemByName('Text2')
    const textStyle3 = app.activeDocument
...

Votes

Translate

Translate
Community Beginner ,
Nov 26, 2024 Nov 26, 2024

Copy link to clipboard

Copied

You can tweek it to suit your needs better:

 

app.doScript(Main, language=ScriptLanguage.JAVASCRIPT, withArguments=[], 
    undoMode=UndoModes.ENTIRE_SCRIPT, undoName='Testing');

function Main(){
    //reference to each style
    const titleStyle = app.activeDocument.paragraphStyles.itemByName('Title')
    const textStyle1 = app.activeDocument.paragraphStyles.itemByName('Text1')
    const textStyle2 = app.activeDocument.paragraphStyles.itemByName('Text2')
    const textStyle3 = app.activeDocument.paragraphStyles.itemByName('Text3')

    //selected paragraphs (including first title)
    var paras = app.selection[0].paragraphs;
    var prevstyle = paras[0].appliedParagraphStyle;
    
    for (i=1; i<paras.length; i++){
        var para = paras[i];
        var curstyle = para.appliedParagraphStyle;
        
        //ignore titles
        if (curstyle==titleStyle){
            continue;
        }

        //apply new style
        if (prevstyle==titleStyle){
            para.applyParagraphStyle(textStyle1);
        }
        else if (prevstyle==textStyle1){
            para.applyParagraphStyle(textStyle2);
        }
        else if (prevstyle==textStyle2){
            para.applyParagraphStyle(textStyle3);
        }
        else if (prevstyle==textStyle3){
            para.applyParagraphStyle(textStyle2);
        }

        //update prev
        prevstyle = para.appliedParagraphStyle;
    }
}

Votes

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
New Here ,
Nov 26, 2024 Nov 26, 2024

Copy link to clipboard

Copied

Woow!

Greate job @luis_guimaraes !

Thank you very much

Marko

Votes

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
New Here ,
Nov 26, 2024 Nov 26, 2024

Copy link to clipboard

Copied

LATEST

Hi everyone,

Is it possible to set this script so that every time it encounters the "Title" style it starts over with the "Text1" style and then the Next styles?
For example:
"Title" -"Text1" - "Text2" - "Text3"" - "Text2" - "Text3" ....... "Title" - "Text1" - "Text2" - "Text3"" - "Text2" - "Text3" ...

Thank you in advance

Votes

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