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

Array content duplicating from one page to another

Participant ,
Oct 09, 2020 Oct 09, 2020

Copy link to clipboard

Copied

In the below code, I got two set colors with compare two set of arrays UniqueColors(used colors in each page) and graphicsTreatmentArray(set of colors name to compare with UniqueColors) and get two set colors it has to be insert in each pages. but the problem is the finalarray colors are duplicated from one page to another even if the array empty declaration assigned in end of the page loop.

 

 var graphicsTreatmentArray = ["color1", "color2", "color3", "color4", "color5", "color6", "color7"]

   // app.menuActions.itemByName("$ID/Add All Unnamed Colors").invoke();
    var UsedPagesColors_Obj = [];
    var UsedPagesColors = [];
    var UsedGraphicsTreatmentColors = [];
    var UsedInkColors = [];
    var UniqueColors;
    var UniqueColors_Obj;
    Char_Style_Create();
    Adding_Pages();

function Char_Style_Create(){
    try{
        var myCharc = doc.characterStyles.add({name:"Icon",
            pointSize:16,fontStyle:"Regular",appliedFont:"SF Pro Text",baselineShift:-2.5});
    }catch(e){}
}

//Pages 
function Adding_Pages(){
    var mypages = app.activeDocument.spreads;
    if(mypages.length>0){
        for(var b=0;b<mypages.length;b++){

            var myPageItems = mypages[b].allPageItems;
            for(var c=0;c<myPageItems.length;c++){
                if(myPageItems[c].parentPage != null){
                    if (myPageItems[c].itemLayer.visible == true && myPageItems[c].visible == true){
                        if(myPageItems[c].constructor.name != "Group" && myPageItems[c].constructor.name != "PDF"
                        && myPageItems[c].constructor.name != "Image" && myPageItems[c].constructor.name != "EPS"){
                            if(myPageItems[c].fillColor.name != "None" && myPageItems[c].fillColor.name != "Paper" ){
                                if(myPageItems[c].fillColor.name.search("DO NOT PRINT","i") == -1 &&
                                myPageItems[c].fillColor.name.search("DO_NOT_PRINT","i") == -1 &&
                                myPageItems[c].fillColor.name.search("DONOTPRINT","i") == -1){
                                    UsedPagesColors.push(myPageItems[c].fillColor.name);
                                }
                            }
                            if(myPageItems[c].strokeWeight != 0 && myPageItems[c].strokeColor.name != "None" && 
                            myPageItems[c].strokeColor.name != "Paper" && myPageItems[c].strokeColor.name != "Black"){
                                if(myPageItems[c].strokeColor.name.search("DO NOT PRINT","i") == -1 &&
                                myPageItems[c].strokeColor.name.search("DO_NOT_PRINT","i") == -1 &&
                                myPageItems[c].strokeColor.name.search("DONOTPRINT","i") == -1){
                                    UsedPagesColors.push(myPageItems[c].strokeColor.name);
                                }
                            }
                        }
                        if(myPageItems[c].constructor.name == "TextFrame"){
                            if(myPageItems[c].textStyleRanges.length>0){
                                var myChar = myPageItems[c].textStyleRanges;
                                for(var d=0;d<myChar.length;d++){
                                    if(myChar[d].fillColor.name != "None" && myChar[d].fillColor.name != "Paper" && myChar[d].fillColor.name != "Black"){
                                        if(myChar[d].fillColor.name.search("DO NOT PRINT","i") == -1 && 
                                        myChar[d].fillColor.name.search("DO_NOT_PRINT","i") == -1 &&
                                        myChar[d].fillColor.name.search("DONOTPRINT","i") == -1){
                                            UsedPagesColors.push(myChar[d].fillColor.name);
                                        }
                                    }
                                    if(myChar[d].strokeWeight != 0 && myChar[d].strokeColor.name != "None" && 
                                    myChar[d].strokeColor.name != "Paper"){
                                        if(myChar[d].strokeColor.name.search("DO NOT PRINT","i") == -1 &&
                                        myChar[d].strokeColor.name.search("DO_NOT_PRINT","i") == -1 &&
                                        myChar[d].strokeColor.name.search("DONOTPRINT","i") == -1){
                                            UsedPagesColors.push(myChar[d].strokeColor.name);
                                            
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            UniqueColors = unique(UsedPagesColors);
            var finalGraphicsColors = uniqueGraphicsColors(UniqueColors)
            Adding_ParagraphsStyles(UniqueColors);
            placing_Pasteboard_Graphics(mypages[b]);
            placing_Pasteboard_Inks(mypages[b]);
        }
    }
}

//Get Unique Colors
function uniqueGraphicsColors(colors){
    var myColors = String(colors).split(",");
    for(a=0; a<myColors.length; a++){
        var myGraphicTreatments = graphicsTreatmentArray.join("\r");
            if(myGraphicTreatments.search(myColors[a])>-1){
                UsedGraphicsTreatmentColors.push(myColors[a])
            } else {
                UsedInkColors.push(myColors[a])
            }
        
    } 
    return UsedGraphicsTreatmentColors, UsedInkColors;
}

//Graphics Treatment
function placing_Pasteboard_Graphics(pageName){
    var pasteBoardtable = pageName.allPageItems;
    for(var b=0;b<pasteBoardtable.length;b++){
        if(pasteBoardtable[b].parentPage == null){
            if(pasteBoardtable[b].constructor.name == "TextFrame"){
                if(pasteBoardtable[b].tables.length > 0){
                    for(var c=0;c<pasteBoardtable[b].tables[0].cells.length;c++){
                        if(pasteBoardtable[b].tables[0].cells[c].contents.match(/GRAPHIC TREATMENTS:/)){
                            pasteBoardtable[b].tables[0].cells[c].words[2].contents = String(UsedGraphicsTreatmentColors.length);
                            if(pasteBoardtable[b].tables[0].cells[c+2].paragraphs.length>1){
                                pasteBoardtable[b].tables[0].cells[c+2].paragraphs[0].select();
                                pasteBoardtable[b].tables[0].cells[c+2].paragraphs[-1].select(SelectionOptions.addTo);
                                app.selection[0].remove();
                            }                 
                            pasteBoardtable[b].tables[0].cells[c+2].insertionPoints[0].contents = UsedGraphicsTreatmentColors.join("\r");
                            if(pasteBoardtable[b].tables[0].cells[c+2].paragraphs.length>0){
                                for(var x=0;x<pasteBoardtable[b].tables[0].cells[c+2].paragraphs.length;x++){
                                    var con = pasteBoardtable[b].tables[0].cells[c+2].paragraphs[x].contents.replace(/\r/g,"");
                                    pasteBoardtable[b].tables[0].cells[c+2].paragraphs[x].applyParagraphStyle(app.activeDocument.paragraphStyles.itemByName(con));
                                }
                            
                            }
                        } 

                    }

                }

            }

        }
    }
    UsedGraphicsTreatmentColors = [];
}

//Ink Colors
function placing_Pasteboard_Inks(pageName){
    var pasteBoardtable = pageName.allPageItems;
    for(var b=0;b<pasteBoardtable.length;b++){
        if(pasteBoardtable[b].parentPage == null){
            if(pasteBoardtable[b].constructor.name == "TextFrame"){
                if(pasteBoardtable[b].tables.length > 0){
                    for(var c=0;c<pasteBoardtable[b].tables[0].cells.length;c++){
                        if(pasteBoardtable[b].tables[0].cells[c].contents.match(/INKS:/)){
                            pasteBoardtable[b].tables[0].cells[c].words[1].contents = "4/"+String(UsedInkColors.length);
                            if(pasteBoardtable[b].tables[0].cells[c+2].paragraphs.length>1){
                                pasteBoardtable[b].tables[0].cells[c+2].paragraphs[1].select();
                                pasteBoardtable[b].tables[0].cells[c+2].paragraphs[-1].select(SelectionOptions.addTo);
                                app.selection[0].remove();
                            }                 
                            pasteBoardtable[b].tables[0].cells[c+2].insertionPoints[-1].contents = UsedInkColors.join("\r");
                            if(pasteBoardtable[b].tables[0].cells[c+2].paragraphs.length>0){
                                for(var x=1;x<pasteBoardtable[b].tables[0].cells[c+2].paragraphs.length;x++){
                                    var con = pasteBoardtable[b].tables[0].cells[c+2].paragraphs[x].contents.replace(/\r/g,"");
                                        pasteBoardtable[b].tables[0].cells[c+2].paragraphs[x].applyParagraphStyle(app.activeDocument.paragraphStyles.itemByName(con));
                                }
                            
                            }
                        } 
                    }
                }
            }
        }
    }
    UsedInkColors = [];
}

function Adding_ParagraphsStyles(UniqueColors){
    if(UniqueColors.length>0){

        for(var a=0;a<UniqueColors.length;a++){
            try{
                var myPara = app.activeDocument.paragraphStyles.add({name:UniqueColors[a], fillColor:UniqueColors[a],
                    pointSize:7,fontStyle:"Regular",appliedFont:"SF Pro Text"});
                    //myPara.nestedStyles.add({appliedCharacterStyle:app.activeDocument.characterStyles.item("Icon"), 
                    //delimiter:" ", repetition:1, inclusive:true});
                    myPara.paragraphDirection = ParagraphDirectionOptions.LEFT_TO_RIGHT_DIRECTION;
                    myPara.justification = 1818584692;
                    myPara.bulletsAndNumberingListType=ListType.BULLET_LIST; 
                    myPara.bulletsCharacterStyle = "Icon";
                    myPara.bulletsTextAfter = " ";
                    myPara.bulletChar.characterType= BulletCharacterType.UNICODE_ONLY;
                    myPara.bulletChar.characterValue = 9632;
            }catch(e){}
        }
    }
}







    

    function unique(array) {
        var o = {};
        r = [];
        for (i = 0; i < array.length; i++) {
            o[array[i]] = array[i];
        }
        for (i in o) {
            r.push(o[i]);
        }
        return r;
    };

 

 

TOPICS
Scripting

Views

215

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 ,
Oct 09, 2020 Oct 09, 2020

Copy link to clipboard

Copied

Lotta code to wade through but one thing to check is you want a different variable than the second i in your unique function. You're also not using the finalGraphicsColor that you are returning from your UniqueGraphicsColor function. You also are returning two separate arrays in that function, but not grouping them as a single object (ie an array of 2 elements), thus what you are returning is actually just the second array. 

 

Lots of debugging to do. 

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
Participant ,
Oct 10, 2020 Oct 10, 2020

Copy link to clipboard

Copied

LATEST

finalGraphicsColor is not a issue but I have declared the array return to fetch the data.

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