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

Script to display InDesign Page Size instead of Document Setup size

Explorer ,
Sep 02, 2014 Sep 02, 2014

Hello, I was very kindly helped out with the below script (using a text variable) which displays the size of the current page within an InDesign document.

The result it gives is based on the Document Setup, and I was wondering if it was possible to amend it to base the result on the Page Size instead (as you can now change the page size independently of the Document Setup within InDesign using the Page Tool). I don't know anything about scripting by the way!

Many thanks in advance for any help.

var doc = app.activeDocument; 

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters; 

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters; 

var w = doc.documentPreferences.pageWidth, 

    h = doc.documentPreferences.pageHeight, 

    pagesize = app.activeDocument.textVariables.itemByName("PageSize"); 

pagesize.variableOptions.contents = h + " mm X " + w + " mm"; 

TOPICS
Scripting
8.1K
Translate
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

correct answers 2 Correct answers

Enthusiast , Sep 02, 2014 Sep 02, 2014

Try this,

var doc = app.activeDocument;

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;

for(var i =0;i<doc.pages.length;i++)

{

        alert("Page Width : " +  doc.pages.bounds[3] + "\rPage Height : " + doc.pages.bounds[2] )

    }

Regards,

Chinna

Translate
LEGEND , Jul 06, 2024 Jul 06, 2024

@Lital5E82 

 

Forgot to mention - thanks to the ObjectStyle - you can set your own properties, including ParaStyle to format text.

 

Translate
Enthusiast ,
Sep 02, 2014 Sep 02, 2014

Try this,

var doc = app.activeDocument;

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;

for(var i =0;i<doc.pages.length;i++)

{

        alert("Page Width : " +  doc.pages.bounds[3] + "\rPage Height : " + doc.pages.bounds[2] )

    }

Regards,

Chinna

Translate
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
Explorer ,
Sep 03, 2014 Sep 03, 2014

Hi Chinna, thank you for this - this is great - apologies if i didn't make it clear but this displays the info as an alert, and i needed it to appear in the "PageSize" text variable field as per the text in my post. However i think I've managed to work it out (pasted below if you're interested), and it works a treat now, so thank you ever so much for all your help.

- - - - - - - - - - - - -

var doc = app.activeDocument; 

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters; 

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters; 

for(var i =0;i<doc.pages.length;i++)

{

    pagesize = app.activeDocument.textVariables.itemByName("PageSize"); 

pagesize.variableOptions.contents = doc.pages.bounds[2] + "mm x " + doc.pages.bounds[3] + "mm"

}

- - - - - - - - - - - - -

Translate
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
New Here ,
Jun 18, 2019 Jun 18, 2019

Hi All,

I've had this script working perfectly in InDesign CS6 for years, but when updating to InDesign CC19 I get a error in line 7.

Error Number: 45

Error String: Object is invalid

Any ideas?

Translate
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 ,
Jun 18, 2019 Jun 18, 2019

Hi Beckett,

Seems you don't have the PageSize text variable defined. You can check that by using the text variable panel that can be accessed using Type>Text Variables>Define menu you can use the same panel to define the variable if its not defined. Once this is done then you can run the code and it should set the value just fine

I have modified the code to show a proper error in this case

var doc = app.activeDocument;

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;

for(var i =0;i<doc.pages.length;i++)

{

    pagesize = app.activeDocument.textVariables.itemByName("PageSize");

    if(pagesize.isValid)

          pagesize.variableOptions.contents = doc.pages.bounds[2] + "mm x " + doc.pages.bounds[3] + "mm"

    else

          alert("The PageSize variable does not exist. Please add it before running the code")

}

Note:- If you define text variable using the method described above while no document is open in InDesign you will have this variable defined for all the documents you create after that, else it would be created for only active document.

-Manan

Translate
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
New Here ,
Mar 09, 2021 Mar 09, 2021

Hello, I've been trying to add a page size text varibale forever, this is so exciting!

I've tried adding the script below, but get this error message (I've defined the text variable in the file)

Error Number: 55

Error String: Object does not suppor the property or method 'bounds'

 

Line: 13

Souce: pagesize.variableOptions.contents = doc.pages.bounds[2] + "mm x " + doc.pages.bounds[3] + "mm";

 

Also, wondering if I can simply replace millimeters for inches?

 

Thank you for your help!

Translate
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 ,
Mar 09, 2021 Mar 09, 2021

Old code; looks like there was an error in it, or the 'i' iterator got stripped at some point. Adding mm should be fine as you have it. 

pagesize.variableOptions.contents = doc.pages[i].bounds[2] + "mm x " + doc.pages[i].bounds[3] + "mm";

 

Translate
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
New Here ,
Mar 09, 2021 Mar 09, 2021

That worked!! Thank you so much for your help!!

Translate
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
New Here ,
Mar 09, 2021 Mar 09, 2021

I work in Environmental grahics so ofen times I have multiple page sizes in a single file, I noticed with this script, if I update 1 page size it updates all the text variables to the new size (ie if the first page is 11"x11" and I run the script for the text variable it shows up as 11" x11", but if the next page is 11"x 22" and I run the script it will update both pages to say 11"x 22". Does anyone know if a way to fix that? See script I am using below:

 

var doc = app.activeDocument;

 

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;

 

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches

 

; for(var i =0;i<doc.pages.length;i++)

 

{

 

pagesize = app.activeDocument.textVariables.itemByName("PageSize");

 

pagesize.variableOptions.contents = doc.pages[i].bounds[2] + "in x " + doc.pages[i].bounds[3] + "in"

 

}

Translate
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 ,
Mar 09, 2021 Mar 09, 2021

Yeah, a text variable won't really work here. On each page are you able to create a text frame and name it in the Layer panel to something like "pagesize". This could be done on masters. 

 

Then as you are looping through the pages, you can set: 

...pages[i].textFrames.itemByName("pagesize").contents = the bounds calculation. 

Translate
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
New Here ,
Nov 22, 2022 Nov 22, 2022

I don´t get it – where do I put that last mentioned string?
This: pages[i].textFrames.itemByName("pagesize").contents

Translate
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 ,
Mar 10, 2021 Mar 10, 2021

Brian said:

"Old code; looks like there was an error in it, or the 'i' iterator got stripped at some point. Adding mm should be fine as you have it. "

 

Hi Brian,

the code was damaged when this thread was moved over from the old InDesign or InDesign Scripting forum to this new one in late 2019. And yes, the whole expression: [i] was stripped by that bug. That's typical for this kind of damage…

 

Regards,
Uwe Laubender

( ACP )

Translate
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 Beginner ,
Jul 02, 2024 Jul 02, 2024

hello

i tried using this scriot and it gives me this error:

Screenshot 2024-07-02 at 10.50.29.pngcan someone help please?

thank you

lital

Translate
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
LEGEND ,
Jul 02, 2024 Jul 02, 2024

@Lital5E82

 

Can you post your code? 

 

None of the examples posted in this thread are 25+ lines long? 

 

Translate
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 Beginner ,
Jul 02, 2024 Jul 02, 2024

var doc = app.activeDocument;

 

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;

 

doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches

 

; for(var i =0;i<doc.pages.length;i++)

 

{

 

pagesize = app.activeDocument.textVariables.itemByName("PageSize");

 

pagesize.variableOptions.contents = doc.pages[i].bounds[2] + "mm x " + doc.pages[i].bounds[3] + "mm"

 

}

Translate
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
LEGEND ,
Jul 02, 2024 Jul 02, 2024

@Lital5E82

 

Do you have "PageSize" variable defined in your INDD document.

 

And the code - overall - doesn't make much sense - the loop part. 

The loop was for the 1st version - when it was displaying size of each page - this version will set only the size of the last Page.

 

Translate
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 Beginner ,
Jul 02, 2024 Jul 02, 2024

i made the variable "PageSize"

and the code is from this thread

 

Translate
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
LEGEND ,
Jul 02, 2024 Jul 02, 2024
quote

i made the variable "PageSize"

and the code is from this thread


By @Lital5E82

 

Can you share your INDD document - part of it? 

 

Translate
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 Beginner ,
Jul 02, 2024 Jul 02, 2024

no problem

this file already has the pagesize written but it was made manualy and i want a fix for future work - making it automaticly

Translate
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
LEGEND ,
Jul 04, 2024 Jul 04, 2024
quote

no problem

this file already has the pagesize written but it was made manualy and i want a fix for future work - making it automaticly


By @Lital5E82

 

Sorry for the later reply.

 

In your document - you don't have this Text Variable defined:

RobertatIDTasker_0-1720135890617.png

 

So I have a similar error:

RobertatIDTasker_1-1720135991942.png

 

 

But after adding this Variable:

 

RobertatIDTasker_2-1720136040830.png

 

I have this - "><" are mine:

RobertatIDTasker_3-1720136125002.png

 

But like I've said earlier - the way this code works - it will "remember" size of the LAST page in the document - and will show the same value on every page.

 

You need a different script.

 

Translate
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 Beginner ,
Jul 04, 2024 Jul 04, 2024

thank you for the reply

i thought i only need to define the variable but i got it now and you are right the script is not good for multiply pages. do you know how to fix it maybe?

 

thanks again for all the help

Translate
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
LEGEND ,
Jul 05, 2024 Jul 05, 2024

@Lital5E82 

 

The MOST LAZY 😄 solution that I can think of would be to just declare multiple Text Variables in your document:

 

RobertatIDTasker_0-1720213343661.png

 

Then modify your script like this:

RobertatIDTasker_1-1720213414528.png

 

 

var doc = app.activeDocument;
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches
for(var i =0;i<doc.pages.length;i++)
{
   pagesize = app.activeDocument.textVariables.itemByName("PageSize_"+(i+1));
   pagesize.variableOptions.contents = doc.pages[i].bounds[2] + "in x " + doc.pages[i].bounds[3] + "in"
}

 

Of course you don't have to add "_" - you can add number straight after "PageSize" - it's just for better visibility.

 

Translate
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 Beginner ,
Jul 06, 2024 Jul 06, 2024

Oh but I have documents with 100 different pages so I don't think this can work for it

 

but thank you for trying I really appreciate it 

Translate
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
LEGEND ,
Jul 06, 2024 Jul 06, 2024
quote

Oh but I have documents with 100 different pages so I don't think this can work for it

 

but thank you for trying I really appreciate it 


By @Lital5E82

 

Would be much easier if you were working on a PC... 

 

I'll try to convert it to a version that will work with a dedicated TextFrame - but I'm not JS guy so it can take me a bit. 

 

Translate
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
LEGEND ,
Jul 06, 2024 Jul 06, 2024
quote

Oh but I have documents with 100 different pages so I don't think this can work for it

 

By @Lital5E82

 

Here is script for you:

 

var doc = app.activeDocument;
var myTF = '';
var myName = 'Page Size';
try
{
   doc.layers.itemByName(myName).locked = false;
   doc.layers.itemByName(myName).remove();
}
catch (e) {};
try
{
   var myLayer = doc.layers.add({name:myName});
}
catch (e) 
{
   var myLayer = doc.layers.itemByName(myName);
};

try
{
   var myObjStyle = doc.objectStyles.add({name:myName});
}
catch (e) 
{
   var myObjStyle = doc.objectStyles.itemByName(myName);
};

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.inches;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.inches
var myDocRulerOrigin = doc.viewPreferences.rulerOrigin;
doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
var myDocZeroPoint = doc.zeroPoint;
doc.zeroPoint = [0,0];
for(var i =0;i<doc.pages.length;i++)
{
   myTF = doc.pages[i].textFrames.add(myLayer);
   myTF.geometricBounds = [-1,0,-0.5,1];
   myTF.parentStory.contents = doc.pages[i].bounds[2].toFixed(3) + "in x " + doc.pages[i].bounds[3].toFixed(3) + "in";
   myTF.appliedObjectStyle = myObjStyle;
}
doc.zeroPoint = myDocZeroPoint;
doc.viewPreferences.rulerOrigin = myDocRulerOrigin;
doc.layers.itemByName(myName).locked = true;

 

 

 

var myLayerName = 'Page Size';

 

name of the Layer your TFs will be placed on - you can change to whatever you prefer.

 

Script will DELETE this Layer every time you run script - so you don't have to remove it manually - but it also means that you SHOULDN'T put there your own objects.

But, just in case, script is locking this Layer - for your convenience.

 

This line defines size and location of the TFs:

 

   myTF.geometricBounds = [0,0,1,2];

 

In [ ] you can enter your own location and size:

 

[top, left, bottom, right]

 

Those are absolute coordinates - not (x,y) and (width,height).

 

Script will apply ObjectStyle - same name as your Layer - and also preserve ZeroPoint and RulerOrigin - so your TFs are always in the top-left corner of the page - but as mentioned above - you can set your own values for the location and size.

 

This:

 

   myTF.geometricBounds = [-1,0,-0.5,1];

 

will give you this:

RobertatIDTasker_0-1720298292875.png

 

- one inch above the page,

- aligned with the left edge of the page,

- bottom of the TF will be half inch above the top edge of the page,

- right edge of the TF will be one inch from the left edge of the page.

 

So in the end - half inch high and one inch wide.

 

Translate
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