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

Can't add items to dynamically created page

Guest
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

I'm writing a Javascript script that will take a JSON file describing several page layouts (comprising one or more of text frames) and automatically create an InDesign document from it.

The script mostly works, but has a strange bug where the items destined for the last page always get put onto the last-but-one page. Any thoughts on why this might be happening? Any thoughts would be greatly appreciated!

Here's the minimal code:

// load JSON file using a parser into jsonObj

// add the required number of pages

for (var i = 0; i < jsonObj.PageLayouts.length - 1; i++) {

  app.activeDocument.pages.add(LocationOptions.AFTER, app.activeDocument.pages);

}

// create text frames using JSON doc on each page

var docPages = app.activeDocument.pages;

for (var i = 0; i < jsonObj.PageLayouts.length; i++) {

  for (var j = 0; j < jsonObj.PageLayouts.Placeholders.length; j++) {

  origin_x = jsonObj.PageLayouts.Placeholders.Frame.Origin.X / 10;

  origin_y = jsonObj.PageLayouts.Placeholders.Frame.Origin.Y / 10;

  height   = jsonObj.PageLayouts.Placeholders.Frame.Size.Height / 10;

  width    = jsonObj.PageLayouts.Placeholders.Frame.Size.Width / 10;

  bottom_x = origin_x + width;

  bottom_y = origin_y + height;

  var tf = docPages.textFrames.add({

  geometricBounds: [origin_y, origin_x, bottom_y, bottom_x]

  });

  $.writeln('  - Added rect (page ' : i + ' , rect ' + j + ')');

  }

}

And here's the sample JSON, which should create 3 pages, with 1 textFrames on the first 2 pages, and 2 textFrames on the 3rd page. Instead, I get 3 textFrames on the second page, and none on the last page.

{

    "PageLayouts": [

        {

            "Placeholders": [

                {

                    "Frame": {

                        "Origin": {

                            "X": 0,

                            "Y": 0

                        },

                        "Size": {

                            "Height": 2100,

                            "Width": 2970

                        }

                    }

                }

            ]

        },

        {

            "Placeholders": [

                {

                    "Frame": {

                        "Origin": {

                            "X": 120,

                            "Y": 120

                        },

                        "Size": {

                            "Height": 1860,

                            "Width": 2730

                        }

                    }

                }

            ]

        },

        {

            "Placeholders": [

                {

                    "Frame": {

                        "Origin": {

                            "X": 120,

                            "Y": 1800

                        },

                        "Size": {

                            "Height": 200,

                            "Width": 2730

                        }

                    },

                },

                {

                    "Frame": {

                        "Origin": {

                            "X": 120,

                            "Y": 120

                        },

                        "Size": {

                            "Height": 1640,

                            "Width": 2730

                        }

                    }

                }

            ]

        }

    ]

}       

TOPICS
Scripting

Views

311

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
Community Expert ,
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

This should be moved over to InDesign Scripting

Mike Witherell

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
People's Champ ,
Dec 13, 2016 Dec 13, 2016

Copy link to clipboard

Copied

try this:

var main = function() {

  var d = app.properties.activeDocument,

  data, pls, i = 0, n = 0;

  if ( !d) return;

  data = { 

    "PageLayouts": [ 

        { 

            "Placeholders": [ 

                { 

                    "Frame": { 

                        "Origin": { 

                            "X": 0, 

                            "Y": 0 

                        }, 

                        "Size": { 

                            "Height": 2100, 

                            "Width": 2970 

                        } 

                    }  

                } 

            ] 

        }, 

        { 

            "Placeholders": [ 

                { 

                    "Frame": { 

                        "Origin": { 

                            "X": 120, 

                            "Y": 120 

                        }, 

                        "Size": { 

                            "Height": 1860, 

                            "Width": 2730 

                        } 

                    } 

                } 

            ] 

        }, 

        { 

            "Placeholders": [ 

                { 

                    "Frame": { 

                        "Origin": { 

                            "X": 120, 

                            "Y": 1800 

                        }, 

                        "Size": { 

                            "Height": 200, 

                            "Width": 2730 

                        } 

                    }, 

                }, 

                { 

                    "Frame": { 

                        "Origin": { 

                            "X": 120, 

                            "Y": 120 

                        }, 

                        "Size": { 

                            "Height": 1640, 

                            "Width": 2730 

                        } 

                    } 

                } 

            ] 

        } 

    ] 

}         

  pls = data.PageLayouts;

  n = pls.length;

  while ( i<n )  {

  addPlaceHolders (d, pls, i );

  i++;

  }

}

function addPlaceHolders(doc, pageLayouts, index ) {

  var phs = pageLayouts[index].Placeholders, ph, page, origins, sizes ;

  page = doc.pages[index];

  !page.isValid && page = doc.pages.add();

  while ( ph = phs.pop() ) {

  origins = ph.Frame.Origin;

  sizes = ph.Frame.Size;

  page.textFrames.add({ 

  geometricBounds: [origins.Y, origins.X, origins.Y+sizes.Height, origins.X+sizes.Width]

  });  

  }

}

main();

HTH

Loic

http://www.ozalto.com/

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
Guest
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

Thanks Loic! Much nicer code than mine. However it gave the same problem.

We figured it out - it's because the "Facing Pages" option is enabled, which means that odd-numbered pages from 3 onwards are on the right side of a double-page spread, and so the origin coordinates need to be changed to account for this. (Both scripts work fine when Facing Pages is disabled.)

All the best,

Rehan

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
Guest
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

LATEST

Apologies for this, I'm new to the forums and didn't see the separate Scripting forum. I'll make sure I post there from now on.

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