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
} 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 );
//
Copy link to clipboard
Copied
@Trevor – hmm, using an emoticon like you did, generated an empty e-mail for me (I will be notified via e-mail if a new post will be added to this thread). Sorry for being OT again…
Here the mail that came in:
Uwe
Copy link to clipboard
Copied
@Marc – back from OT to the real issue behind this case…
I had another look at the provided document. One thing what was making me suspicious is the fact that the dimensions of the pages are 500 x 300 px while a look to the Pages Panel states the name for the Alternate Layout is "800 x 600 H".
The initial layout for this document was "800 x 600 px" for Digital Publishing. Later in the process this was changed to 500 x 300 px. Not by the Pages Tool, but in the GUI for the Document Setup. (A clear indication for that fact is the uniformity of Master Spread [None] to the rest of the master spreads and the first page.)
I just did that with a version of InDesign CC:
Creating a new document with 800 x 600 px and changing it to 500 x 300 px using Document Setup.
And running the script. Bam. There is it: translation!!
Now we know WHY it happened.
If we have used the Pages Tool this problem would not occur…
Uwe
Copy link to clipboard
Copied
Did the same experiment with InDesign CS5.5 and CS6:
CS5.5 was ok.
CS6 was showing the issue.
So I conclude this bug (or strange behavior) sneaked in with CS6.
Uwe
Copy link to clipboard
Copied
Just showing some numbers:
var page = app.documents[0].spreads[0].pages[0];
var pageMatrix = page.transformValuesOf(CoordinateSpaces.parentCoordinates)[0];
var pageMatrixValues = pageMatrix.matrixValues;
$.writeln(pageMatrixValues+"\t"+"PageBounds:"+" "+page.bounds);
//Results CS5.5:
//NEW DOCUMENT WITH 1 PAGE: SIZE 800 x 600 px:
//1,0,0,1,-400,-300 PageBounds: 0,0,600,800
//AFTER RESIZE in GUI with Document Setup to size 500 x 300 px:
//1,0,0,1,-250,-150 PageBounds: 0,0,300,500
//AFTER RESIZE in GUI with Pages Tool on applied Masterpage A:
//1,0,0,1,-250,-150 PageBounds: 0,0,300,500
//Results CS6 and above:
//NEW DOCUMENT WITH 1 PAGE: SIZE 800 x 600 px:
//1,0,0,1,-400,-300 PageBounds: 0,0,600,800
//AFTER RESIZE in GUI with Document Setup to size 500 x 300 px:
//NUMBERS NOT CHANGED! OPPOSED TO InDesign CS5.5:
//1,0,0,1,-400,-300 PageBounds: 0,0,300,500
//AFTER RESIZE in GUI with Pages Tool on applied Masterpage A:
//1,0,0,1,-250,-150 PageBounds: 0,0,300,500
I think, this case is quite clear now.
Uwe
Copy link to clipboard
Copied
And two screenshots to illustrate the difference of changing document page sizes in InDesign CS6 (and above):
1. "Wrong" or "dangerous" approach with the Document Setup GUI:
2. "Appropriate" approach with the Pages Tool:
"Wrong", "dangerous" and "appropriate" in the sense, that scripting will work as expected without moving overridden page items to their original positions.
To correct the problematic example #1, just use the Document Setup GUI and go back to the original size and change size again by using the Page Tool.
Uwe
Copy link to clipboard
Copied
Hi Uwe
Just clarifying / summarizing things.
When one wants to change the page dimensions of a clean non-messed up document use the page tool and not the document page setup GUI.
Copy link to clipboard
Copied
@Trevor – oh, how drastic. 😉
Now, we need a good scripting solution to:
1. Detect cases like that
2. "Correct" or "normalize" the transformation matrix for all master pages ?!
I'm a little clueless how to get that together by scripting… I'm glad to do it in the GUI. If there are clues like the naming of the Alternate Layout, ok. But can we rely on that? No. The user could renamed the Alternate Layout. So, is inspecting the transformation matrix of a page, of a master page enough?
We can consider us lucky, that the document is a single sided one.
Let's check the 6 numbers of the matrix ( a - f 😞
1,0,0,1,-400,-300
The first 4 numbers, a,b,c and d indicate, that principly everything is well in order. It's values are showing Identity: 1,0,0,1.
No scaling, no rotation, no shear. Good.
e and f will give the translation part of the matrix, a hint when compared to the bounds, that something could be wrong.
Let's look at the bounds of page one:
PageBounds: 0,0,300,500
Can we always assume that these numbers are in direct relation to the values in the matrix?
A formula, that says:
- (bounds[3] / 2) should always be equal to e in the matrix, if a,b,c,d equals 1,0,0,1
and:
- (bounds[2] / 2) should always be equal to f in the matrix, if a,b,c,d equals 1,0,0,1
And if not, expect a translation of page items that will be overridden…
( Setting my high hopes in Marc )
Uwe
Copy link to clipboard
Copied
Laubender wrote:
Let's look at the bounds of page one:
PageBounds: 0,0,300,500
Can we always assume that these numbers are in direct relation to the values in the matrix?
By no means! Never assume that myPage.bounds—or myPageItem.geometricBounds—can lead to anything reliable when you deal with transformations. There are too many ways to retrieve confusing 'bounds.' Those values only reflect the current top-left-bot-right locations of a some spread-relative bounding box according to the current user units, the current ruler coordinate system, the current custom zero-point and so on. Coordinate spaces and transformations require some learning and training, but they definitely allow you to remove those contextual hazards. (The biggest problem with InDesign coordinates, IMHO, is that pathpoints are all given relative to ruler space!)
@+
Marc
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
@Marc – ah. I summoned you 😉 To the rescue!
Will try your script with CS5 and CS5.5…
Did not read your recent comment before sending mine; was logged out inadvertently several times the last hour (arrgh!).
Will be back again soon…
Uwe
Copy link to clipboard
Copied
@Marc – your code is working very well in InDesign CS5 and CS5.5.
Just added the line that unlocked all layers in the document.
Wow. What a journey to the world of matrices…
A toast to Marc Autret, the Master of the Matrix!
(Got a glass of wine in my hand: A 2009 Rosso di Montalcino)
Uwe
Copy link to clipboard
Copied
Oops. Unlocking the layers was already provided in Marc's code…
Now. I have to digest the code. Will take a while…
Uwe
Copy link to clipboard
Copied
Laubender wrote:
(Got a glass of wine in my hand: A 2009 Rosso di Montalcino)
Please upload
Copy link to clipboard
Copied
Best I could do, not to refreshing but the best I could do.
Cheers
Copy link to clipboard
Copied
Thank you guys, I will test your fixing code tomorrow. From @Laubender tests I would say it is a bug in Indesign. Is there a way how to give adobe a notice about this issue?
Copy link to clipboard
Copied
@Trevor – thank you standing in for me.
I couldn't do better 🙂
Cheers!
Uwe
Copy link to clipboard
Copied
This script is not full, or it's not working on InDesign CC 2014, or am I doing something wrong?
I need simple working script that will override all master page items, and fix those shifting
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
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
Copy link to clipboard
Copied
Hi!
I'm working with CC 2020 and I guess I'm having the same issue...
I used your script and altered csvDATA to my needs (4 masters as alternative layouts)
However, InDesign returns error 30480:
"There is no data of the requested type" (translation from German, sorry)
ms = document.masterSpreads.itemByName(csvData["master"]);
I tried all the few other solutions I could find on google/bing. Only this one worked yet, but it messes up the alternative layouts:
https://stackoverflow.com/questions/31994396/indesign-applescript-override-master-page-items-glitch
(the js, not the applescripts)
The only thing I want to do is the dropdown menu task "Override All Master Page Items".
All the best
Mathis
Copy link to clipboard
Copied
Hi Mathis,
if the menu command "Override All Master Page Items" is working for you and you do not use InDesign Server, but the desktop version, you should be able to invoke the menu command by scripting like so:
var mA = app.menuActions.itemByName("$ID/Override All Master Page Items");
if( mA.isValid && mA.enabled ){ mA.invoke() };
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hej Uwe,
thanks for the quick reply and wonderful solution 🙂
I actually wanted it to apply to all pages, so I built a for-loop around it and it works great:
var myDocument = app.activeDocument;
var TotalPages = (myDocument.pages.count());
var mA = app.menuActions.itemByName("$ID/Override All Master Page Items");
for(var CurrentPage=0; CurrentPage < TotalPages; CurrentPage++) {
app.activeWindow.activePage=app.activeDocument.pages[CurrentPage]; //just switches the active page
if( mA.isValid && mA.enabled ){ mA.invoke() };
}
app.activeWindow.activePage=app.activeDocument.pages[0]; // to jump back to page 1
and added an undo script, because that got out of hand quickly 😄
https://indesignsecrets.com/add-undo-to-your-script.php
Thanks again and best wishes
Mathis
Copy link to clipboard
Copied
Hi Mathis,
thank you for your positive feedback.
Depending on the structure of your document you could shorten your loop a bit by using the activeSpread property:
var mA = app.menuActions.itemByName("$ID/Override All Master Page Items");
var doc = app.documents[0];
var allDocSpreads = doc.spreads.everyItem().getElements();
var allDocSpreadsLength = allDocSpreads.length;
for( var n=0; n<allDocSpreadsLength; n++ )
{
doc.layoutWindows[0].activeSpread = allDocSpreads[n];
if( mA.isValid && mA.enabled ){ mA.invoke() };
};
Alternative by scripting avoiding the loop:
I also tried to select all pages with the Page Tool which is selecting also all pages in the Pages panel, but I had no success to override all master page items for all pages in one go by invoking the menu action. It only worked for the active spread or active page.
FWIW: When I do this within the UI it will work:
[1] Make the Page Tool the active tool.
[2] Do Select All with the keyboard shortcut.
[3] Invoke the menu "Override all master page items" from the Pages panel.
Regards,
Uwe Laubender
( ACP )