Skip to main content
manuelb27477138
Inspiring
August 27, 2018
Answered

How apply correct appliedCharacterStyle

  • August 27, 2018
  • 1 reply
  • 1512 views

Hello!

I have this script to apply the Character Style Italic  when the script find the array elements, in this case the Alphabetic letters A, B, C, ...

But only when the letters had applied the next paragraph styles:

Index Level 01 Roman

Index Level 02 Roman

Index Level 03 Roman

Index Level 04 Roman

Index Level 01 Roman Tracking-...

Index Level 02 Roman Tracking-...

Index Level 03 Roman Tracking-...

Index Level 04 Roman Tracking-...

But is not working, I think the error is this line, but I can not see the solution, Please, can you see something wrong?

myFound.appliedCharacterStyle = 'Italic';} else{}}

I know is working because if I replace this line, and I try to replace a Paragraph Style instance of a Character Style, the script works:

myFound.appliedParagraphStyle = IndexSectionHead;

THE FULL SCRIPT:

//_____________________________________________________________________________________

// COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME            

var Doc = app.documents[0];

var paragraphStyle1 = [];

var paragraphStyle2 = [];  

var paragraphStyle3 = [];

var paragraphStyle4 = [];

var counter = 0;    

var paragraphStyleGroup1 = Doc.paragraphStyleGroups.itemByName("INDEX"); 

while(counter < paragraphStyleGroup1.paragraphStyles.length){     

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 01 Roman Tracking-") != -1){   

        paragraphStyle1.push(paragraphStyleGroup1.paragraphStyles[counter]);}

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 02 Roman Tracking-") != -1){   

        paragraphStyle2.push(paragraphStyleGroup1.paragraphStyles[counter]);}

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 03 Roman Tracking-") != -1){   

        paragraphStyle3.push(paragraphStyleGroup1.paragraphStyles[counter]);}

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 04 Roman Tracking-") != -1){   

paragraphStyle4.push(paragraphStyleGroup1.paragraphStyles[counter]);}        

  counter++;}  

 

totalParagraphStyle1=paragraphStyle1.length;

totalParagraphStyle2=paragraphStyle2.length; 

totalParagraphStyle3=paragraphStyle3.length;

totalParagraphStyle4=paragraphStyle4.length;

//____________________________________________________________________________________

//      END COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME

//_____________________________________________________________________________________

// APPLY PARAGRAPH STYLE ONLY IF MATCH 

var myDoc = app.activeDocument,     

myCodes = [

    "\^A\r",

    "\^B\r",

    "\^C\r",

    "\^D\r",

    "\^E\r",

    "\^F\r",

    "\^G\r",

    "\^H\r",

    "\^I\r",

    "\^J\r",

    "\^K\r",

    "\^L\r",

    "\^M\r",

    "\^N\r",

    "\^Ñ\r",

    "\^O\r",

    "\^P\r",

    "\^Q\r",

    "\^R\r",

    "\^S\r",

    "\^T\r",

    "\^U\r",

    "\^V\r",

    "\^W\r",

    "\^X\r",

    "\^Y\r",

    "\^Z\r"

],   

C = myCodes.length,  c;     

app.findGrepPreferences = null;   

for ( c = 0; c < myCodes.length; c++) {        

    app.findGrepPreferences.findWhat = myCodes;        

    myFound = myDoc.findGrep();     

    var F = myFound.length,  f;

    var IndexSectionHead = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Section Head");  

    var IndexLevel01Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 01 Roman");  

    var IndexLevel02Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 02 Roman");  

    var IndexLevel03Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 03 Roman");  

    var IndexLevel04Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 04 Roman");  

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

        if ((myFound.appliedParagraphStyle == IndexLevel01Roman ||

            myFound.appliedParagraphStyle == IndexLevel02Roman ||

            myFound.appliedParagraphStyle == IndexLevel03Roman ||

            myFound.appliedParagraphStyle == IndexLevel04Roman

        )){ myFound.appliedCharacterStyle = 'Italic';} else{}}

      

    for(i=1; i<=totalParagraphStyle1 ;i++){

        var IndexLevel01RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 01 Roman Tracking-"+i);

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

        if (myFound.appliedParagraphStyle == IndexLevel01RomanTracking)

         {  myFound.appliedCharacterStyle = 'Italic';} else{}}}

    for(i=1; i<=totalParagraphStyle2 ;i++){

        var IndexLevel02RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 02 Roman Tracking-"+i);

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

        if (myFound.appliedParagraphStyle == IndexLevel02RomanTracking)

         {  myFound.appliedCharacterStyle = 'Italic';} else{}}}

        

    for(i=1; i<=totalParagraphStyle3 ;i++){

        var IndexLevel03RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 03 Roman Tracking-"+i);

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

        if (myFound.appliedParagraphStyle == IndexLevel03RomanTracking)

         {  myFound.appliedCharacterStyle = 'Italic';} else{}}}

    for(i=1; i<=totalParagraphStyle4 ;i++){

        var IndexLevel04RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 04 Roman Tracking-"+i);

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0];

        if (myFound.appliedParagraphStyle == IndexLevel04RomanTracking)

         {  myFound.appliedCharacterStyle = 'Italic';} else{}}}

}

//_____________________________________________________________________________________

// END APPLY PARAGRAPH STYLE ONLY IF MATCH 

This topic has been closed for replies.
Correct answer Laubender

Sorry. Forgot to add an s :

characterStyles.itemByName

Regards,
Uwe

1 reply

Community Expert
August 28, 2018

Hi Manuel,

if you want to assign a character style first get the style by its name and then assign it.

It's always best not only using the name of the style but the style itself.

Example:

var charStyleItalic = app.documents[0].characterStyle.itemByName( "Italic" );

/* some other code */

myFound.appliedCharacterStyle = charStyleItalic;

Note: That will only work if the character style is not defined within a character style group.

Regards,
Uwe

manuelb27477138
Inspiring
August 28, 2018

Hi Laubender!

I get this error:

I try other many thing but still the error.

Thanks!

//_____________________________________________________________________________________ 

// COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME              

 

 

var Doc = app.documents[0]; 

var paragraphStyle1 = []; 

var paragraphStyle2 = [];    

var paragraphStyle3 = [];  

var paragraphStyle4 = [];  

var counter = 0;      

var paragraphStyleGroup1 = Doc.paragraphStyleGroups.itemByName("INDEX");   

while(counter < paragraphStyleGroup1.paragraphStyles.length){       

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 01 Roman Tracking-") != -1){     

        paragraphStyle1.push(paragraphStyleGroup1.paragraphStyles[counter]);}  

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 02 Roman Tracking-") != -1){     

        paragraphStyle2.push(paragraphStyleGroup1.paragraphStyles[counter]);} 

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 03 Roman Tracking-") != -1){     

        paragraphStyle3.push(paragraphStyleGroup1.paragraphStyles[counter]);} 

    if(paragraphStyleGroup1.paragraphStyles[counter].name.toString().indexOf("Index Level 04 Roman Tracking-") != -1){     

paragraphStyle4.push(paragraphStyleGroup1.paragraphStyles[counter]);}          

  counter++;}    

   

totalParagraphStyle1=paragraphStyle1.length;  

totalParagraphStyle2=paragraphStyle2.length;   

totalParagraphStyle3=paragraphStyle3.length;  

totalParagraphStyle4=paragraphStyle4.length; 

//____________________________________________________________________________________ 

//      END COUNT ALL PARAGRAPH STYLES IN A PARAGRAPH STYLES GROUP WITH THE SAME NAME 

 

 

 

 

 

 

//_____________________________________________________________________________________ 

// APPLY PARAGRAPH STYLE ONLY IF MATCH   

var myDoc = app.activeDocument,       

myCodes = [ 

    "\^A\r", 

    "\^B\r", 

    "\^C\r", 

    "\^D\r", 

    "\^E\r", 

    "\^F\r", 

    "\^G\r", 

    "\^H\r", 

    "\^I\r", 

    "\^J\r", 

    "\^K\r", 

    "\^L\r", 

    "\^M\r", 

    "\^N\r", 

    "\^Ñ\r", 

    "\^O\r", 

    "\^P\r", 

    "\^Q\r", 

    "\^R\r", 

    "\^S\r", 

    "\^T\r", 

    "\^U\r", 

    "\^V\r", 

    "\^W\r", 

    "\^X\r", 

    "\^Y\r", 

    "\^Z\r" 

],     

C = myCodes.length,  c;       

app.findGrepPreferences = null;     

for ( c = 0; c < myCodes.length; c++) {          

    app.findGrepPreferences.findWhat = myCodes;          

    myFound = myDoc.findGrep();       

    var F = myFound.length,  f;  

    var IndexSectionHead = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Section Head");    

    var IndexLevel01Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 01 Roman");    

    var IndexLevel02Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 02 Roman");    

    var IndexLevel03Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 03 Roman");    

    var IndexLevel04Roman = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 04 Roman");    

    var charStyleItalic = app.documents[0].characterStyle.itemByName( "Italic" );

 

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0]; 

        if ((myFound.appliedParagraphStyle == IndexLevel01Roman || 

            myFound.appliedParagraphStyle == IndexLevel02Roman || 

            myFound.appliedParagraphStyle == IndexLevel03Roman || 

            myFound.appliedParagraphStyle == IndexLevel04Roman 

        )){ myFound.appliedCharacterStyle = charStyleItalic;} else{}}  

 

 

    for(i=1; i<=totalParagraphStyle1 ;i++){ 

        var IndexLevel01RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 01 Roman Tracking-"+i); 

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0]; 

        if (myFound.appliedParagraphStyle == IndexLevel01RomanTracking)  

         {  myFound.appliedCharacterStyle = charStyleItalic;} else{}}} 

 

 

    for(i=1; i<=totalParagraphStyle2 ;i++){ 

        var IndexLevel02RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 02 Roman Tracking-"+i); 

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0]; 

        if (myFound.appliedParagraphStyle == IndexLevel02RomanTracking)  

         {  myFound.appliedCharacterStyle = charStyleItalic;} else{}}} 

          

    for(i=1; i<=totalParagraphStyle3 ;i++){ 

        var IndexLevel03RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 03 Roman Tracking-"+i); 

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0]; 

        if (myFound.appliedParagraphStyle == IndexLevel03RomanTracking)  

         {  myFound.appliedCharacterStyle = charStyleItalic;} else{}}} 

 

 

    for(i=1; i<=totalParagraphStyle4 ;i++){ 

        var IndexLevel04RomanTracking = myDoc.paragraphStyleGroups.itemByName("INDEX").paragraphStyles.itemByName("Index Level 04 Roman Tracking-"+i); 

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

        myFound.appliedCharacterStyle = myDoc.characterStyles[0]; 

        if (myFound.appliedParagraphStyle == IndexLevel04RomanTracking)  

         {  myFound.appliedCharacterStyle = charStyleItalic;} else{}}} 

 

 

}  

//_____________________________________________________________________________________ 

// END APPLY PARAGRAPH STYLE ONLY IF MATCH   

LaubenderCommunity ExpertCorrect answer
Community Expert
August 28, 2018

Sorry. Forgot to add an s :

characterStyles.itemByName

Regards,
Uwe