Copy link to clipboard
Copied
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";
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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"
}
- - - - - - - - - - - - -
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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";
Copy link to clipboard
Copied
That worked!! Thank you so much for your help!!
Copy link to clipboard
Copied
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"
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I don´t get it – where do I put that last mentioned string?
This: pages[i].textFrames.itemByName("pagesize").contents
Copy link to clipboard
Copied
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 )