Highlighted

remove multiple elements from array

Contributor ,
Sep 23, 2020

Copy link to clipboard

Copied

Hi.

I have an almost finished script. Unfortunately it doesn´t work like I want:

var
myDoc = app.activeDocument
myParGroup = myDoc.paragraphStyleGroups;
myParStyles = myDoc.paragraphStyles.everyItem().name;

afGrArray = [], afArray =  [], doubAfArray = [];
afGrNr = myDoc.paragraphStyleGroups.count();


for (s=0;s<myDoc.paragraphStyles.count(); s++) {
    afArray.push(myParStyles[s]);
    }


for (i=0 ; i<afGrNr; i++) {
    
    afGr = myParGroup[i]; //.name + "\r"
    afGrName = afGr.name;
        
    afNr = myParGroup[i].paragraphStyles.count();
    
    for (n = 0; n < afNr; n++) {
        
        af = afGr.paragraphStyles[n];
        afName = af.name;
        
        afArray.push(afName);
        
        }
    
    }

   function checkMultiple(element, index) {
      for(t = 0; t < afArray.length;t++) {

        for(u = 0; u < afArray.length;u++) {
            
            if(u !== t) {

                if(afArray[u] === afArray[t] ){
                   
                    doubAfArray.push(afArray[t]); 
                    break;
                   
                    }
                }
            
         }

      }
  
  if(doubAfArray.length>0) {
     alert(doubAfArray);
  } 
  
  else {
         alert('There are no double elements');
      }
   }   

checkMultiple();

When I get the results they are double.

Like "paragraphStyle 1paragraphStyle 5paragraphStyle 1paragraphStyle 5"

Correct would be "paragraphStyle 1paragraphStyle 5". Why this doubling?

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

It looks like you're trying to identify if a style with the same name is used multiple times within a document. Building on Uwe's suggestion to use allParagraphStyles, here's a simplified version of your script: 

 

 

var checkMultiple = function() {
    var myDoc = app.activeDocument,
        allPstyles = myDoc.allParagraphStyles,
        nameArr = [],
        doubleName = [];

    for (var p = 0; p < allPstyles.length; p++) {
        nameArr.push(allPstyles[p].name);
    }
    nameArr.sort();
    
    for (var i = nameArr.length - 1; i > 0; i--) {
        if (nameArr[i] == nameArr[i-1]) {
            doubleName.push(nameArr[i]);
            do {
                i--;
                if (i == 0) { break; } 
            }
            while (nameArr[i] == nameArr[-1]);
        }
    }
    
    if (doubleName.length == 0) {
        alert("No duplicate Paragraph Style names");
    } else {
        doubleName.sort();
        alert(doubleName);
    }
};

checkMultiple();

 

TOPICS
Scripting

Views

131

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

remove multiple elements from array

Contributor ,
Sep 23, 2020

Copy link to clipboard

Copied

Hi.

I have an almost finished script. Unfortunately it doesn´t work like I want:

var
myDoc = app.activeDocument
myParGroup = myDoc.paragraphStyleGroups;
myParStyles = myDoc.paragraphStyles.everyItem().name;

afGrArray = [], afArray =  [], doubAfArray = [];
afGrNr = myDoc.paragraphStyleGroups.count();


for (s=0;s<myDoc.paragraphStyles.count(); s++) {
    afArray.push(myParStyles[s]);
    }


for (i=0 ; i<afGrNr; i++) {
    
    afGr = myParGroup[i]; //.name + "\r"
    afGrName = afGr.name;
        
    afNr = myParGroup[i].paragraphStyles.count();
    
    for (n = 0; n < afNr; n++) {
        
        af = afGr.paragraphStyles[n];
        afName = af.name;
        
        afArray.push(afName);
        
        }
    
    }

   function checkMultiple(element, index) {
      for(t = 0; t < afArray.length;t++) {

        for(u = 0; u < afArray.length;u++) {
            
            if(u !== t) {

                if(afArray[u] === afArray[t] ){
                   
                    doubAfArray.push(afArray[t]); 
                    break;
                   
                    }
                }
            
         }

      }
  
  if(doubAfArray.length>0) {
     alert(doubAfArray);
  } 
  
  else {
         alert('There are no double elements');
      }
   }   

checkMultiple();

When I get the results they are double.

Like "paragraphStyle 1paragraphStyle 5paragraphStyle 1paragraphStyle 5"

Correct would be "paragraphStyle 1paragraphStyle 5". Why this doubling?

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

It looks like you're trying to identify if a style with the same name is used multiple times within a document. Building on Uwe's suggestion to use allParagraphStyles, here's a simplified version of your script: 

 

 

var checkMultiple = function() {
    var myDoc = app.activeDocument,
        allPstyles = myDoc.allParagraphStyles,
        nameArr = [],
        doubleName = [];

    for (var p = 0; p < allPstyles.length; p++) {
        nameArr.push(allPstyles[p].name);
    }
    nameArr.sort();
    
    for (var i = nameArr.length - 1; i > 0; i--) {
        if (nameArr[i] == nameArr[i-1]) {
            doubleName.push(nameArr[i]);
            do {
                i--;
                if (i == 0) { break; } 
            }
            while (nameArr[i] == nameArr[-1]);
        }
    }
    
    if (doubleName.length == 0) {
        alert("No duplicate Paragraph Style names");
    } else {
        doubleName.sort();
        alert(doubleName);
    }
};

checkMultiple();

 

TOPICS
Scripting

Views

132

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
Sep 23, 2020 0
Adobe Community Professional ,
Sep 23, 2020

Copy link to clipboard

Copied

Hi cmoke73,

if you want to work with an array of all available paragraph styles then work with:

app.documents[0].allParagraphStyles

This returns an array of paragraph styles.

 

Loop the array to get all the names.

 

If you want to know the specific paragraph style group one of the styles is in, check the value of parent of an individual style. If parent is your document the style is in the "root". If parent is not your document the parent is the paragraph style group that holds the style.

 

Note: One can have two or more paragraph styles with the same name. But that requires different paragraph style groups.

 

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
Reply
Loading...
Sep 23, 2020 1
Adobe Community Professional ,
Sep 23, 2020

Copy link to clipboard

Copied

It looks like you're trying to identify if a style with the same name is used multiple times within a document. Building on Uwe's suggestion to use allParagraphStyles, here's a simplified version of your script: 

 

 

var checkMultiple = function() {
    var myDoc = app.activeDocument,
        allPstyles = myDoc.allParagraphStyles,
        nameArr = [],
        doubleName = [];

    for (var p = 0; p < allPstyles.length; p++) {
        nameArr.push(allPstyles[p].name);
    }
    nameArr.sort();
    
    for (var i = nameArr.length - 1; i > 0; i--) {
        if (nameArr[i] == nameArr[i-1]) {
            doubleName.push(nameArr[i]);
            do {
                i--;
                if (i == 0) { break; } 
            }
            while (nameArr[i] == nameArr[-1]);
        }
    }
    
    if (doubleName.length == 0) {
        alert("No duplicate Paragraph Style names");
    } else {
        doubleName.sort();
        alert(doubleName);
    }
};

checkMultiple();

 

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
Reply
Loading...
Sep 23, 2020 2
Adobe Community Professional ,
Sep 23, 2020

Copy link to clipboard

Copied

Error in my post, and I can't edit it. The while line should be: 

 

 

while (nameArr[i] == nameArr[i-1]);

 

If there weren't more than three of the same name it shouldn't matter. 

 

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
Reply
Loading...
Sep 23, 2020 0
Contributor ,
Sep 23, 2020

Copy link to clipboard

Copied

Looks good Brian. I´ll try!

Thanks in advance

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
Reply
Loading...
Sep 23, 2020 0
cmoke73 LATEST
Contributor ,
Sep 24, 2020

Copy link to clipboard

Copied

Update: it works fine!

🙂

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
Reply
Loading...
Sep 24, 2020 0
Contributor ,
Sep 23, 2020

Copy link to clipboard

Copied

Hi Uwe.

 

This "special way" that I created the script is because the first step was to reach the groups as well. This resulted in this form.

But good to know, your´s more elegant. 🙂

thanks!

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
Reply
Loading...
Sep 23, 2020 0
Adobe Community Professional ,
Sep 23, 2020

Copy link to clipboard

Copied

You are getting double because there are for loop two times in your code.

Just remove the second for loop. Doubling won't happen.

Reason is in first loop you are pushing all style naimes in Array named afArray.

Just remove second for loop.

 

Best

Sunil

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
Reply
Loading...
Sep 23, 2020 0
Contributor ,
Sep 24, 2020

Copy link to clipboard

Copied

Oh, okay.

But, shame on me, I can't make this adjustment to my script. 😞

Will try to understand the process in brians script with my mechanical brain...

 

regards

Andreas

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
Reply
Loading...
Sep 24, 2020 0