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

remove paragraph style error

Contributor ,
Nov 20, 2018 Nov 20, 2018

Hi experts,

My script like this:

var cDoc = app.activeDocument;

       myPara = cDoc.ParagraphStyle.name.match(/\(Note\)/,g);

        if (myPara.isValid){

        myPara.remove();

        }

        else exit;      

aim to remove paragraph style which name match (Note).

how can I make it work?

thanks

regard

John

TOPICS
Scripting
602
Translate
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 Expert , Nov 21, 2018 Nov 21, 2018

Hi John,

check your variable named: myPara.

This would hold the result of the match if everything would be ok in that code line.

However it is not:

There is no property ParagraphStyle for a document.

Also check the regular expression. Best test something like that before with a string you are familiar with:

var s = "Before (Note) After";

s.match( /\(Note\)/,g); // will give an error: g is undefined.

You have to write g as a string. Sorry, I have to correct myself. See corrected code below:

Also note, tha

...
Translate
Contributor ,
Nov 20, 2018 Nov 20, 2018

hi John ,

    you can remove paragraph style like this,

var cDoc = app.activeDocument; 

       myPara = cDoc.paragraphStyles.item("Note"); 

        if (myPara.isValid){ 

        myPara.remove(); 

        } 

        else exit; 

Translate
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 Expert ,
Nov 21, 2018 Nov 21, 2018

Hi John,

check your variable named: myPara.

This would hold the result of the match if everything would be ok in that code line.

However it is not:

There is no property ParagraphStyle for a document.

Also check the regular expression. Best test something like that before with a string you are familiar with:

var s = "Before (Note) After";

s.match( /\(Note\)/,g); // will give an error: g is undefined.

You have to write g as a string. Sorry, I have to correct myself. See corrected code below:

Also note, that there could be more instances than one paragraph style name that could match.

If you want to check all paragraph styles loop the allParagraphStyles array of the document so that you can also see into paragraph styles in style groups.

Something like that should be robust enough:

var doc = app.activeDocument;

var paraStyles = doc.allParagraphStyles; // Returns an array;

// Array to store the ids of all matches:

var idsOfStylesToRemove = [];

// Loop the array to check every paragraph style name in the document:

for( var n=0; n<paraStyles.length; n++ )

{

    if( paraStyles.name.match( /\(Note\)/g ) )//EDITED

    { idsOfStylesToRemove[ idsOfStylesToRemove.length++ ] = paraStyles.id };

};

// Loop the results and remove every style there:

for( var n=0; n<idsOfStylesToRemove.length; n++ )

{ doc.paragraphStyles.itemByID( idsOfStylesToRemove ).remove() };

Regards,
Uwe

Translate
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
Contributor ,
Nov 21, 2018 Nov 21, 2018
LATEST

Thank you Uwe

thank you so much.

John

Translate
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