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

Find different text and apply different paragraph styles

New Here ,
May 16, 2018 May 16, 2018

Copy link to clipboard

Copied

Hi,

I have so many documents. Which style may be repeated, But not next after next.

My requirement is:

Step 1:

Find Text Called "SECTION" in content and apply the style name called "Level-1";

Step 2:

Find Numbers started in content like "1.1, 1.2, 1.3 to 16.1, 16.2, 16.3 so on " and apply the style name called "Level-2";

Step 3: Remaining text Should apply "Para";

Step 4: In Paragraph if Tab(Space) Comes before that apply the charecter style called "Para Bold".

Below is a sample example:

Find different text and apply different paragraph styles.jpg

I am very new to scripting. can any one help !

Below the code i have tried and getting a error:

var myDoc = app.activeDocument,     

myCodes = ["SECTION","Numbers"],   

C = myCodes.length,  c;     

app.findTextPreferences = null;   

for ( c = 0; c < C; c++) {        

    app.findTextPreferences.findWhat = myCodes;        

    myFound = myDoc.findText();     

    var F = myFound.length,  f;

   

    var Style_01 = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Level-1");

    var Style_02 = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Level-2");

     

   

    for ( f = 0; f < F; f++) {   

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

       

        if ((myFound.appliedParagraphStyle == Style_01) ||

            (myFound.appliedParagraphStyle == Style_02) ||

            ) {   

                            myFound.appliedParagraphStyle = MyStyle;

        } else{}

    }   

}

//I have tried this script too but not understanding,

var main = function() {             

    //VARS           

    var doc = app.properties.activeDocument,           

    fgp = app.findGrepPreferences.properties,           

    texts = [], n = 0, nthText, myParaStyle,         

    texts2 = [], n = 0, nthText2, myParaStyle2,   

    texts3 = [], n = 0, nthText3, myParaStyle3,   

    texts4 = [], n = 0, nthText4, myParaStyle4,  

    paraStyleName = "Text New";    

    paraStyleName2 = "Text New 12";    

    paraStyleName3 = "Quotation New Bold";     

    paraStyleName4 = "Quotation New Italic"; 

               

    //Exit if no documents open           

    if ( !doc ) {           

    alert("You need an open document" );           

    return;           

    }               

               

    //Create ParaStyle if needed. Otherwise use it       

    var paraStyle = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName);     

    myParaStyle = doc.paragraphStyles.item ( paraStyleName );           

    // if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );           

     

    var paraStyle2 = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName2); 

    myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );           

    // if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );   

 

 

    var paraStyle3 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName3); 

    myParaStyle3 = doc.paragraphStyles.item ( paraStyleName3 );           

    // if ( !myParaStyle3.isValid ) myParaStyle3 = doc.paragraphStyles.add ( paraStyleName3 );   

 

 

    var paraStyle4 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName4); 

    myParaStyle4 = doc.paragraphStyles.item ( paraStyleName4 );           

    // if ( !myParaStyle4.isValid ) myParaStyle4 = doc.paragraphStyles.add ( paraStyleName4 );  

               

    //Setting grep find preferences           

    app.findGrepPreferences = null;         

    app.findGrepPreferences.properties = {           

    findWhat:".+",           

    }           

               

    //Finding texts with static properties           

    texts = doc.findGrep();           

    n = texts.length;           

           

    //Looping through found texts to inspect further properties with values ranges.           

    while ( n--  ) {nthText = texts;                    

        if (nthText.fontStyle =="Roman" && nthText.pointSize <= 10.5 ){nthText.appliedParagraphStyle = paraStyle;}  

        if (nthText.pointSize == 12){nthText.appliedParagraphStyle = paraStyle2;}  

        if (nthText.fontStyle == "Bold"){nthText.appliedParagraphStyle = paraStyle3;}  

        if (nthText.fontStyle == "Italic") {nthText.appliedParagraphStyle = paraStyle4;}    

    }       

               

    //reverting UI values in the F/C panel           

    app.findGrepPreferences.properties = fgp;           

    }           

    var u;                  

    app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

Thanks in Advace

TOPICS
Scripting

Views

738

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
People's Champ ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

myFound.appliedParagraphStyle = MyStyle;

Where is "MyStyle" referenced ?

And what kind of error do you get exactly ?

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 ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

Hi Loic,

I really don't have idea. I have searched some topics in forum and try to edit that.

And the error i got is "Object Invalid".

If you can help in this that would be great.

Thanks

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
People's Champ ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

And the error i got is "Object Invalid".

there is probably more like the line number…

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 ,
May 22, 2018 May 22, 2018

Copy link to clipboard

Copied

LATEST

Hi Loic,

I am tried to edit a bit more and it is applying para style to text, But when i put a condition in text it's not working. When find text apply level-1 style. The updated codes as follows:

var myIndFile = File.openDialog ("Select Indesign File"); 

var myWordFile = File.openDialog ("Select Word File"); 

 

app.open (myIndFile); 

 

var mydoc = app.activeDocument; 

 

mydoc.textPreferences.smartTextReflow = true; 

mydoc.textPreferences.limitToMasterTextFrames = false; 

mydoc.textPreferences.deleteEmptyPages = false; 

mydoc.textPreferences.addPages = AddPageOptions.endOfStory; 

 

mydoc.pages.item(0).textFrames.item(0).place(File(myWordFile)); 

/*Style AREA*/

    try{

    myParagraphStyle = mydoc.paragraphStyles.item("Para");

    //If the paragraph style does not exist, trying to get its name will generate an error.

    myName = myParagraphStyle.name;

    }

    catch (myError){

    //The paragraph style did not exist, so create it.

    myParagraphStyle = mydoc.paragraphStyles.add({name:"Para"});

    }

/*End of Style AREA*/

    var myTextFrame = mydoc.pages.item(0).textFrames.item(0);

    myTextFrame.parentStory.texts.item(0).applyParagraphStyle(myParagraphStyle, true);

   

   

myCodes = ["SECTION"],  

C = myCodes.length,  c;    

app.findTextPreferences = null;  

for ( c = 0; c < C; c++) {       

    app.findTextPreferences.findWhat = myCodes;       

    myFound = myDoc.findText();    

    var F = myFound.length,  f;

   

     for ( f = 0; f < F; f++) {  

          if ((myFound.appliedParagraphStyle == myParagraphStyle) && (myFound == myCodes)) {  

                            myFound.appliedParagraphStyle = Level1;

        }

         }

     }

var wordName = myWordFile.name;

app.activeDocument.save(new File(myIndFile.path + "/"  +myIndFile.name.replace(".indd", "") + "_" +wordName+"_Spanish" + "_V1.0.indd"));

Line no : 21 to 55 needs some suggestions to edit.

My next task is find line starts with numbers chage to level-2 style.

Thanks,

Suresh.

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