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

replace groups and their content

Engaged ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

Hi!

This shouldn´t be a question but a post about a short script I worked on for a week (the destiny of each newbee).

It works in larger documents. Of course there is a need for a library and its elements, special kind of items the script searches for and an open document with some pages.

I´m sure there are many other ways to make the script more short and with higher performance. Feel free to leave your opinion...

var myDoc = app.activeDocument;

var myObj = myDoc.selection[0];

var myPages = myDoc.pages.everyItem();

var numberOfPages = myDoc.pages.length;

var lastPgNumber = myDoc.pages.lastItem().name;

var newLabel;

var myNewLabel;

var myGroups = myDoc.groups;

var allMyGroups = myGroups.length;

var allMyObjects;

var myPageNumber; var oldObjPgNr;

var myObjects = [],

badObj = [],

myText = [],

myRectPositionY1 = [],

myRectPositionX1 = []

newPagesArray = [];

app.doScript(deleteObj, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Preislabel ändern");

function deleteObj() {

    for (var i=0; i < allMyGroups; i++) {

        var thisGroup = myGroups;

        //selecting groups (I only need the groups ON pages, with TWO items inside, one of the items should be a RECTANGLE) ...

        if (thisGroup.parentPage != null && thisGroup.pageItems.length == 2 && thisGroup.allPageItems[1].constructor.name == "Rectangle") {

            // ... in addition: the rectangle should be WHITE

            if(thisGroup.rectangles[0].fillColor.name == "Paper") {

                // add all "useful" groups in an ARRAY

                myObjects.push(thisGroup);

            }

        }

           //else if (badObj.push(thisGroup));

    }

allMyObjects = myObjects.length;

for(var n=0; n < allMyObjects; n++){

    // calculate the "real" PAGE-NUMBER

    oldObjPgNr = myObjects.parentPage.name;

    myPageNumber = numberOfPages - (lastPgNumber - oldObjPgNr)-1;

   

    // get coordinates for the current object

    myRectPositionY1.push(myObjects.geometricBounds[1]+ 6);

    myRectPositionX1.push(myObjects.geometricBounds[0] );

   

    // grab new object from  ibrary

    newLabel = app.libraries.itemByName("SF_Kataloge_Zutaten.indl").assets.itemByName("SF_Preis_rot_NEU");

    myNewLabel = newLabel.placeAsset(myDoc);

   

    // move new object to the same page and location like the current object

    myNewLabel[0].move(myDoc.pages[myPageNumber]);   

    myNewLabel[0].move([myRectPositionY1, myRectPositionX1])

   

    // replace contents of the new object with the contents of the current object

    myNewLabel[0].contents = myObjects.textFrames[0].contents;

     }

for(var b=0; b < allMyObjects; b++){

     // delete all groups

    myObjects.remove();

}  

}  

Regards

A.

ps. It shouldn´t be forgetted: of course I had a lot of help from the community here!!! Thank you by the way!

TOPICS
Scripting

Views

443

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 ,
May 05, 2017 May 05, 2017

Copy link to clipboard

Copied

Okay, little problem:

In one doc it causes no error. But in the others suddenly the object gets "not valid".

Line 29 & line 55 will be marked.

I tried to find out which object causes the mess with:

alert(thisGroup.parentPage.name + "\r" + thisGroup.textFrames[0].contents);

... added after line 29. I can´t find anything wrong...

Feedback after fixing that problem.

regrets...

I meen regards

A.

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 ,
May 12, 2017 May 12, 2017

Copy link to clipboard

Copied

LATEST

The problem was: there are not only rectangles in the documents but also POLYGONS :

...

for (var i=0; i < allMyGroups; i++) {
var thisGroup = myGroups;
resolvedItem = app.documents[0].groups.pageItems[0].getElements()[0]; 
if(thisGroup.parentPage != null && resolvedItem.parent.allPageItems.length == 2)  {
if(resolvedItem.constructor.name == "Rectangle" || resolvedItem.constructor.name == "Polygon") {
if(resolvedItem.fillColor.name == "Paper"){
myObjects.push(thisGroup);  
}
}
}; 

   

}

...

(thank U Uwe: count number of objects in a group )

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