Override elements by script problems

Community Beginner ,
Apr 21, 2014

Copy link to clipboard

Copied

I need to override elements by script and have them end up at the same place they were in master. Now it overrides and puts them to the new page but it changes their position. Here is my function:

function loadPagesAndOverrideElements(document, csvData) {

    // add pages defined in CSV and correct layout

    for (var i=0; csvData.numberOfRows>i; i++) {

        var masterSpread = document.masterSpreads.itemByName(csvData["master"]);

        document.pages.add();

        document.pages[i+1].appliedMaster = masterSpread;

        var allItems = document.pages[i+1].appliedMaster.pageItems.everyItem().getElements();

        for(var j=0;j<allItems.length;j++){

            try {

                allItems.override(document.pages[i+1]);

            } catch(e) {

                // alert(e);

            }

        }

    }

    document.pages[0].remove();

    return document;

}

Uwe, Trevor, tomas8,

Thanks to Uwe's investigations/tests I finally came up with a scripted solution that sounds clean to me—but it still needs to be tested in CS5/5.5.

My main finding is that the matrix Page.masterPageTransform (property available in CS5 and later) accurately reflects the shift we are trying to circumvent.

When a new page is created in the 'corrupted' document based on any master spread, CS6 and CC show the following result:

alert( myNewPage.masterPageTransform.matrixValues );

// => 1,0,0,1,-150,-150      Here it is!

As far as I understand this matrix determines the transformation "applied to the master page before it is applied to Page," that is, how the page is mapped to its master page. In normal case that should be—I suppose—the IDENTITY matrix.

My guess is that CS6/CC improperly uses those matrix values during scripted overrides, so that we have to explicitly apply the inverse matrix to the newly created page, as follows:

var document = app.activeDocument; 

var csvData = {

    "master" : ["A-Master", "B-Master", "C-Master"],

    "numberOfRows" : 3

    };

 

loadPagesAndOverrideElements(document, csvData);

 

 

function loadPagesAndOverrideElements(document, csvData)

{

    // Constants

    // ---

    const CS_INNER = +CoordinateSpaces.innerCoordinates,

          ORIGIN = [[0,0],CS_INNER];

   

    // Variables

    // ---

    var i, n = csvData.numberOfRows,

        ms, pg, mx;

    // Unlock ALL layers

    // ---

    document.layers.everyItem().locked = false;

    // Page creation loop

    // ---

    for( i=0 ; i < n ; ++i )

        {

        // Select the master spread

        // ---

        ms = document.masterSpreads.itemByName(csvData["master"]);

       

        // Create new page and override master items

        // ---

        ms.pageItems.everyItem().override( pg=document.pages.add({appliedMaster:ms}) );

        // Revert the masterPageTransform if needed

        // ---

        (mx=pg.properties.masterPageTransform) && pg.transform(CS_INNER, ORIGIN, mx.invertMatrix());

        }

   

    // Remove the 1st page

    // ---

    document.pages[0].remove();

};

Seems to work for me.

What about CS5?

@+

Marc

Topics

Scripting

Views

10.0K

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
Contributor ,
Sep 21, 2018

Copy link to clipboard

Copied

I am using InDesign CC 2018 13.1 and the script with the fix still applies. Very useful! Thank you

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...
Community Beginner ,
Oct 24, 2018

Copy link to clipboard

Copied

If everyone should need a applescript solution for this override issue…

tell application id "com.adobe.indesign"

  tell document 1

  tell page 1

  set mMasterPageTransform to master page transform

  end tell

  transform page 1 in inner coordinates from center anchor with matrix (invert matrix mMasterPageTransform)

  end tell

end tell

and it works in CC2018

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...