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

remove multiple elements from array

Engaged ,
Sep 23, 2020 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?

TOPICS
Scripting

Views

419

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 Expert , Sep 23, 2020 Sep 23, 2020

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();
 
...

Votes

Translate

Translate
Community Expert ,
Sep 23, 2020 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 )

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
Community Expert ,
Sep 23, 2020 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();

 

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
Community Expert ,
Sep 23, 2020 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. 

 

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
Engaged ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Looks good Brian. I´ll try!

Thanks 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
Engaged ,
Sep 24, 2020 Sep 24, 2020

Copy link to clipboard

Copied

LATEST

Update: it works fine!

🙂

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
Engaged ,
Sep 23, 2020 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!

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
Advocate ,
Sep 23, 2020 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

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
Engaged ,
Sep 24, 2020 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

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