Highlighted

Problem with overriding objects on custom size pages

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

22-08-2017 19-06-40.png

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.

22-08-2017 19-15-00.png

22-08-2017 19-18-53.png

Step 2 I Ctrl+Shift Click the four text frames to override the text frames and get the result I want

22-08-2017 19-19-40.png

Now, lets 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);

            }

        }

     }

}

22-08-2017 19-24-13.png

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.

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.

25-08-2017 12-14-59.png

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:

25-08-2017 11-01-58.png

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);

        }

    }

}

Topics

Scripting

Views

427

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

Problem with overriding objects on custom size pages

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

22-08-2017 19-06-40.png

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.

22-08-2017 19-15-00.png

22-08-2017 19-18-53.png

Step 2 I Ctrl+Shift Click the four text frames to override the text frames and get the result I want

22-08-2017 19-19-40.png

Now, lets 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);

            }

        }

     }

}

22-08-2017 19-24-13.png

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.

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.

25-08-2017 12-14-59.png

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:

25-08-2017 11-01-58.png

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);

        }

    }

}

Topics

Scripting

Views

428

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

Re: Problem with overriding objects on custom size pages

It seems thinking aloud helps me -- here's another idea:

I can create a template with two pages: vertical and horizontal.

22-08-2017 20-43-48.png

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.

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

Re: Problem with overriding objects on custom size pages

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.

25-08-2017 12-14-59.png

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:

25-08-2017 11-01-58.png

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);

        }

    }

}

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

Re: Problem with overriding objects on custom size pages

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:

https://indesign.uservoice.com/forums/913162-adobe-indesign-sdk-scripting-bugs-and-features/suggesti...

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