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
Forgot to mention - thanks to the ObjectStyle - you can set your own properties, including ParaStyle to format text.
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 )
Copy link to clipboard
Copied
hello
i tried using this scriot and it gives me this error:
can someone help please?
thank you
lital
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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"
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
i made the variable "PageSize"
and the code is from this thread
Copy link to clipboard
Copied
i made the variable "PageSize"
and the code is from this thread
By @Lital5E82
Can you share your INDD document - part of it?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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:
So I have a similar error:
But after adding this Variable:
I have this - "><" are mine:
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
The MOST LAZY 😄 solution that I can think of would be to just declare multiple Text Variables in your document:
Then modify your script like this:
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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:
- 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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more