Copy link to clipboard
Copied
Hi,
First post here, so still trying to wrap my head around the way javascript interacts with inDesign.
Basically I wanted to write a script that if I select a spread then it adds a prefix to each of the pages sequentially *the actual defined prefix e.g. 'P' bit can remain in the code*
And then brings up a pop up that lets you choose the page number to apply after all those prefixes i.e. '3'
Is this possible?
Thanks Sam
In this example I would like to be able to tell it rename the currently selected spread —which was duplicated from above—to 'S3, P3, N3'
...
#target indesign
if(app.documents.length>0)
{
var dlg = new Window('dialog', "Add Section Name");
dlg.panel = dlg.add('panel', undefined, "");
dlg.panel.alignChildren = "fill";
dlg.title1 = dlg.panel.add('statictext', undefined, "Prefix");
dlg.prefix = dlg.panel.add('edittext', undefined, "S");
dlg.title2 = dlg.panel.add('statictext', undefined, "Start Page");
dlg.startpage = dlg.panel.add('edittext', undefined, "1");
dlg.title3= dlg.panel.add('statictext', undefined, "Current Page");
dlg.p
Copy link to clipboard
Copied
First Understand the concept of adding or getting section here : https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Section.html ,
For your reference: Rename every page to a text field that page ,
And here is the code you can try or modify accordingly:
var myDoc = app.documents[0];
var myPageNumber = 1;
var pages = myDoc.pages;
var myPrefix = ["S","P","N"];
var preFixConuter = 0;
for(var i = 0; i < pages.length; i++){
try{
var sec = myDoc.sections.add(pages);
}
catch(e){
var sec = pages.appliedSection;
}
sec.continueNumbering = false;
sec.pageNumberStart = myPageNumber;
sec.sectionPrefix = myPrefix[preFixConuter];
if(preFixConuter < 2){
preFixConuter++;
}
else if(preFixConuter == 2){
myPageNumber++;
preFixConuter = 0;
}
}
As far as your pages from spread is concerned, you can check this out:
var myDoc = app.documents[0];
var spreads = myDoc.spreads;
var myPrefix = ["S","P","N"];
for(var i = 0; i < spreads.length; i++){
for(var j = 0; j < spreads.pages.length; j++){
try{
var sec = myDoc.sections.add(spreads.pages
); }
catch(e){
var sec = spreads.pages
.appliedSection; }
sec.continueNumbering = false;
sec.pageNumberStart = parseInt(j+1);
sec.sectionPrefix = myPrefix
; }
}
Best
Sunil
Copy link to clipboard
Copied
Thanks Sunil! Will check it all out.
Copy link to clipboard
Copied
You can use this plugin: Dropbox - SetPage.jsx - Simplify your life
With your example:
Spread 1: input prefix= S,P,N ; start page =1; current page =1.
Spread 2: input prefix=S,P,N ; start page =2; current page =4.
If you dupplicate new spread, you can set the same.
you can double to first page of earch spreads before run script,
It will auto get [start page] and [current page] for you.
Copy link to clipboard
Copied
In spite of uploading your code somewhere far, it is better you put it on forum only.
Because he is not the only person to use it.
Lot of other people for seeking such code and needs explanation of what you did.
it is better you copy your code here itself.
Copy link to clipboard
Copied
#target indesign
if(app.documents.length>0)
{
var dlg = new Window('dialog', "Add Section Name");
dlg.panel = dlg.add('panel', undefined, "");
dlg.panel.alignChildren = "fill";
dlg.title1 = dlg.panel.add('statictext', undefined, "Prefix");
dlg.prefix = dlg.panel.add('edittext', undefined, "S");
dlg.title2 = dlg.panel.add('statictext', undefined, "Start Page");
dlg.startpage = dlg.panel.add('edittext', undefined, "1");
dlg.title3= dlg.panel.add('statictext', undefined, "Current Page");
dlg.pageno = dlg.panel.add('edittext', undefined, "1");
var btnOk = dlg.add("button", undefined, "OK");
var btnClose = dlg.add("button", undefined, "Cancel");
var doc=app.activeDocument;
btnOk.onClick = function(){
dlg.close (12345);
}
try
{
dlg.pageno.text=app.activeWindow .activePage.documentOffset +1 ;
dlg.startpage.text=app.activeWindow .activePage.parent.index+1;
}
catch(e)
{
dlg.pageno.text=1;
}
btnClose.onClick = function(){
dlg.close ();
}
if(dlg.show() == 12345) {
var prefix=dlg.prefix.text;
var startPage=Number(dlg.startpage.text);
var currentPage=Number(dlg.pageno.text);
var arr=prefix.split(",");
var oSection;
var secId=0;
//Delete old section in spread
for (var i=0;i<doc.pages[currentPage-1 ].parent.pages.length;i++)
{
if(currentPage-1 + i >0)
secId=doc.pages[currentPage-2+ i ].appliedSection.id ;
if(doc.pages[currentPage-1 ].parent.pages.documentOffset+1>currentPage && doc.pages[currentPage-1 ].parent.pages.appliedSection.id !=secId)
doc.pages[currentPage-1 ].parent.pages.appliedSection.remove();
}
//Add new section
for (var i=0;i<arr.length;i++)
{
if(currentPage-1 + i >0)
secId=doc.pages[currentPage-2+ i ].appliedSection.id ;
if(doc.pages[currentPage-1 + i ].appliedSection.id !=secId)
{
oSection=doc.pages[currentPage-1 + i ].appliedSection
oSection.sectionPrefix = arr;
// oSection.pageStart =;
oSection.continueNumbering = false;
oSection.pageNumberStart = startPage;
}
else
{
oSection = doc.sections.add( );
oSection.sectionPrefix = arr;
oSection.pageStart =doc.pages[currentPage-1 + i ];
oSection.continueNumbering = false;
oSection.pageNumberStart = startPage;
}
}
}
}
You can use above script to set page name for each Spreads.
With your example:
Spread 1: input prefix= S,P,N ; start page =1; current page =1.
Spread 2: input prefix=S,P,N ; start page =2; current page =4.
If you dupplicate new spread, you can set the same.
you can double to first page of each spreads before run script,
It will auto get [start page] and [current page] for you
Copy link to clipboard
Copied
Thank you so much!
Copy link to clipboard
Copied
daitranthanhoa Is there a way to get this to run through the whole document from a specified starting point.
The main difficulty I have run into so far is if I am asked to add a new spread then I have to re-do—manually—all the spread numbering individually starting from where the new spread was inserted. Ideally it would be great if I could get it to repeat the same process for the spread underneath automatically by adding +1 onto what 'start page' number was defined as on the previous time that I ran the script for 'x' amount of times.
Thanks, Sam
Copy link to clipboard
Copied
Yes, i think , this script can :
#target indesign
if(app.documents.length>0)
{
var dlg = new Window('dialog', "Add Section Name");
dlg.panel = dlg.add('panel', undefined, "");
dlg.panel.alignChildren = "fill";
dlg.title1 = dlg.panel.add('statictext', undefined, "Prefix");
dlg.prefix = dlg.panel.add('edittext', undefined, "S");
dlg.title2 = dlg.panel.add('statictext', undefined, "Start Page");
dlg.startpage = dlg.panel.add('edittext', undefined, "1");
dlg.title3= dlg.panel.add('statictext', undefined, "Current Page");
dlg.pageno = dlg.panel.add('edittext', undefined, "1");
var btnOk = dlg.add("button", undefined, "OK");
var btnClose = dlg.add("button", undefined, "Cancel");
var doc=app.activeDocument;
btnOk.onClick = function(){
dlg.close (12345);
}
try
{
dlg.pageno.text=app.activeWindow .activePage.documentOffset +1 ;
dlg.startpage.text=app.activeWindow .activePage.parent.index+1;
}
catch(e)
{
dlg.pageno.text=1;
}
btnClose.onClick = function(){
dlg.close ();
}
if(dlg.show() == 12345) {
var prefix=dlg.prefix.text;
var startPage=Number(dlg.startpage.text);
var currentPage=Number(dlg.pageno.text);
var arr=prefix.split(",");
var oSection;
var secId=0;
var oSpread=doc.pages[currentPage-1 ].parent;
var iNum=0;
for(var iSp=oSpread.index;iSp<doc.spreads.length;iSp++)
{
// 1spread
oSpread=doc.spreads[iSp];
startPage=startPage +iNum;
iNum=1;
currentPage=oSpread.pages[0].documentOffset +1;
for (var i=0;i<oSpread.pages.length;i++)
{
if(currentPage-1 + i >0)
secId=doc.pages[currentPage-2+ i ].appliedSection.id ;
if(doc.pages[currentPage-1 ].parent.pages.documentOffset+1>currentPage && doc.pages[currentPage-1 ].parent.pages.appliedSection.id !=secId)
doc.pages[currentPage-1 ].parent.pages.appliedSection.remove();
}
for (var i=0;i<arr.length;i++)
{
if(currentPage-1 + i >0)
secId=doc.pages[currentPage-2+ i ].appliedSection.id ;
if(doc.pages[currentPage-1 + i ].appliedSection.id !=secId)
{
oSection=doc.pages[currentPage-1 + i ].appliedSection
oSection.sectionPrefix = arr;
// oSection.pageStart =;
oSection.continueNumbering = false;
oSection.pageNumberStart = startPage;
}
else
{
oSection = doc.sections.add( );
oSection.sectionPrefix = arr;
oSection.pageStart =doc.pages[currentPage-1 + i ];
oSection.continueNumbering = false;
oSection.pageNumberStart = startPage;
}
}
}
}
}
Copy link to clipboard
Copied
Absolute magician, thank you!
Copy link to clipboard
Copied
Is there a script that can clear all of sections as the script doesn't like being run if there are already section markers after the current spread.
Copy link to clipboard
Copied
for you
var doc=app.activeDocument;
for(var i=0;i<doc.pages.length;i++)
{
try
{
doc.pages.appliedSection.remove();
}
catch(e){}
}
Copy link to clipboard
Copied
Hi SamBirch ,
doc.sections come with method remove() .
You cannot remove ALL sections. Only all that come after the first section with page 1 of the document.
Try this code if you want to do this:
var doc = app.documents[0];
if( doc.sections.length > 1 )
{
doc.sections.itemByRange(1,-1).remove();
};
See into itemByRange() that is working on the collection of sections of a document.
For the from parameter I set value 1 which is the second item of the collection.
For the to parameter I set value -1 which is the last item of the collection.
app.documents[0].sections.everyItem().remove();
will not work and throws error number 19462 which is the error $ID/kCantDeleteDefaultSection .
Regards,
Uwe
Find more inspiration, events, and resources on the new Adobe Community
Explore Now