Copy link to clipboard
Copied
Hi forum,
InDesign CC 2017.0 (12.0.0.81 x64 Build), Windows 10
I am trying to mimic the following manual steps by script:
The starting point: I have two master pages in the document – H-Horizontal and V-Vertical (both are of A3 page size). [None] is applied to the first and only page which has horizontal orientation (also A3).
Step 1: I apply V-Vertical master page to the first page and at this moment a warning pops-up: “Page 1 has a custom page size. This page can retain its current size or be resized to match the new master”. I choose “Use master page”.
Step 2 I Ctrl+Shift Click the four text frames to override the text frames and get the result I want
Now, let’s try to do the same by script:
Main();
function Main() {
var doc = app.activeDocument;
var page = doc.pages[0];
var orientation = "V";
var verMaster = doc.masterSpreads.itemByName("V-Vertical");
var horMaster = doc.masterSpreads.itemByName("H-Horizontal");
if (verMaster.isValid && horMaster.isValid) {
if (orientation == "V") {
page.appliedMaster = verMaster;
}
else if (orientation == "H") {
page.appliedMaster = horMaster;
}
var allMasterTextFtrames = page.appliedMaster.textFrames.everyItem().getElements();
for (var i = allMasterTextFtrames.length - 1; i >= 0; i--) {
if (allMasterTextFtrames.label != "") {
allMasterTextFtrames.override(page);
}
}
}
}
Note: the text frames have been moved, for some unconceivable reason, to bottom left. This happens at the moment when the script executes the override command: it happens even if I manually apply the master page and then override frames by script. I guess this is a bug.
Off the top of my head, I can work around this problem by recording x, y coordinates (or geometricBounds) of the frames with insertLabel first, and then, after applying override, read them using extractLabel and restore the original position.
Any ideas?
Here's the test file I used for testing.
— Kas
P.S. This happens only when the page size is different: if I use the horizontal master page -- var orientation = "H"; -- it works as expected. I haven't found the options available when I do this manually -- use master page size or keep current page size -- in the scripting DOM.
1 Correct answer
The idea I set forth in my previous post worked well in practice so I'd like to expound on it a little:
The script uses a several templates to create indd-files for different products which are finally exported to pdf (for print).
The script (js) reads the data directly from Excel workbook consisting of several spreadsheets used as a sort of database (vbs called from js) to fill in pages with text and pictures (using labeled boxes).
A spreadsheet has a column indicating which orientation should be
...Copy link to clipboard
Copied
It seems thinking aloud helps me -- here's another idea:
I can create a template with two pages: vertical and horizontal.
Then, instead of adding new pages and applying master pages to them, I can duplicate them as necessary and finally remove the original two pages.
Copy link to clipboard
Copied
The idea I set forth in my previous post worked well in practice so I'd like to expound on it a little:
The script uses a several templates to create indd-files for different products which are finally exported to pdf (for print).
The script (js) reads the data directly from Excel workbook consisting of several spreadsheets used as a sort of database (vbs called from js) to fill in pages with text and pictures (using labeled boxes).
A spreadsheet has a column indicating which orientation should be used for each page -- landscape or portrait.
The template has two master pages – named L- landscape and P-Portrait – and two 'original' pages: 1st – portrait, 2nd – landscape which are duplicated as much as necessary and deleted at the end:
Here's the relative code snippet:
function ProcessProduct(obj) {
try {
var spread, page, bylineTextFtrames, pageNum,
template = new File(templatesFolderPath + obj.product +".indt"),
pdfFile = new File(obj.pdfExportFolderPath + obj.schoolNameShort + " - " + obj.classNameShort + " - " + obj.product + " - Print Files.pdf");
if (obj.product == "Order forms") {
pageNum = obj.data.length / 4 - 1;
}
else if (obj.product == "Placemat") {
pageNum = obj.data.length;
}
else {
pageNum = obj.data.length - 1;
}
if (pdfFile.exists && !set.cbOverwriteExistingPdfsValue && pdfFile.length >= pdfSizeLimit) {
return;
}
progressTxt.text = "Processing school: " + obj.schoolNameShort + ", class: " + obj.classNameShort + " (" + countClass + " out of " + countClasses + ") -- Making " + obj.product;
progressTxt1.text = "Making InDesign document from template";
if (obj.product == "Class order summary") {
ProcessClassOrderSummary(obj);
}
else { // other products
doc = app.open(template, !set.cbInvisibleModeValue);
if (set.cbSaveInDesignFilesValue) {
doc.save(new File(obj.inddFolderPath + obj.schoolNameShort + " - " + obj.classNameShort + " - " + obj.product + ".indd"));
}
var currPageNum = 1; // used only for Placemat so far
for (var i = 0; i < pageNum; i++) {
progressTxt1.text = "Duplicating pages: " + (i + 1) + " out of " + pageNum;
if (obj.product == "Placemat") { // Placemat
if (obj.data[0] == "P") { // Portrait
spread = doc.spreads[0].duplicate(); // duplicate the spread instead of adding a new page because of the bug
}
else { // Landscape
spread = doc.spreads[1].duplicate();
}
var page = spread.pages[0];
var allMasterTextFrames = page.appliedMaster.textFrames.everyItem().getElements();
for (var j = allMasterTextFrames.length - 1; j >= 0; j--) {
var textFrame = allMasterTextFrames
; if (textFrame.label != "") {
textFrame.override(page);
}
}
IncrementLabels(spread, obj.product, currPageNum);
currPageNum++;
}
else { // Other products
spread = doc.spreads[doc.spreads.length -1].duplicate(); // duplicate the spread instead of adding a new page because of the bug
IncrementLabels(spread, obj.product);
}
}
if (obj.product == "Placemat") { // Placemat - remove the first two pages
doc.spreads[0].remove();
doc.spreads[0].remove();
}
if (obj.product == "Order forms") {
PlaceImagesOrderForms(doc, obj.imageFiles);
}
else {
for (var j = 0; j < doc.pages.length; j++) {
page = doc.pages
; imageFile = GetImageForPage(page.name, obj.imageFiles);
if (imageFile != null) {
PlaceImages(page, imageFile);
}
else {
$.writeln("Can't find the image for page:" + page);
}
}
}
var allTextFtrames = doc.textFrames.everyItem().getElements();
FillInTextFrames(obj, allTextFtrames);
ExportPDF(doc, obj);
}
}
catch(err) {
try {
LogError("Error: " + err.message + ", file: " + doc.name + ", line: " + err.line);
$.writeln(err.message + ", line: " + err.line);
doc.close(SaveOptions.NO);
}
catch(err) {
$.writeln(err.message + ", line: " + err.line);
}
}
}
Copy link to clipboard
Copied
While answering Uwe’s post on uservoice, I stumpled across your workaround. That’s a very nice idea to work around this bug!
To cross reference for others with the same problem – here’s a solution from 2014 in the forum:
https://community.adobe.com/t5/indesign/override-elements-by-script-problems/m-p/6072078
Please leave a vote to get this fixed here:

