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

start page number

Engaged ,
Dec 21, 2020 Dec 21, 2020

Copy link to clipboard

Copied

I'm not sure this is even possible but it would be a time saver to figure it out. 

 

There are many times I run into a design job that needs design on pages non sequentially, i.e. 1-5, 7,20,40, 80. The way I do it now it create a document of all 80 pages, turn off allow pages to shuffle, in this case select page 7 set the start page number of that page to 7 and do the same for 20,40,80 and delete the pages between.  I've been trying to figure out if its even possible to do this through scripting but with no luck. Whenever I change the starPageNumber using script it always changes page 1 to whatever.  First, is this even possible?

 

I know my for loop is off, but I'm at the edge of my abiltiy with this stuff. Thanks for any insight!

 

// DIALOG
// ======
var dialog = new Window("dialog"); 
    dialog.text = "SETUP"; 
    dialog.orientation = "row"; 
    dialog.alignChildren = ["left","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"}); 
    group1.preferredSize.width = 343; 
    group1.orientation = "column"; 
    group1.alignChildren = ["left","center"]; 
    group1.spacing = 9; 
    group1.margins = 0; 

var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}'); 
    edittext1.text = "Page numbers separated by a comma ie. 1,5,9"; 
    edittext1.preferredSize.width = 368; 
    edittext1.preferredSize.height = 30; 

// GROUP2
// ======
var group2 = dialog.add("group", undefined, {name: "group2"}); 
    group2.orientation = "column"; 
    group2.alignChildren = ["left","center"]; 
    group2.spacing = 10; 
    group2.margins = 0; 

var button1 = group2.add("button", undefined, undefined, {name: "button1"}); 
    button1.text = "OK"; 
    button1.preferredSize.width = 68; 

var button2 = group2.add("button", undefined, undefined, {name: "button2"}); 
    button2.text = "Cancel"; 
    button2.preferredSize.width = 68; 

dialog.show();

var pagesVar = (edittext1.text);
var pageNumbers = eval('['+pagesVar+']');

var lastPage = pageNumbers[pageNumbers.length-1];
myDoc.documentPreferences.pagesPerDocument = lastPage;

for (i=0; i<pageNumbers.length; i++){
    //$.writeln(i)
    var testArr = myDoc.documentPreferences.startPageNumber = (pageNumbers[i]);
    $.writeln(testArr);
}

 

TOPICS
Scripting

Views

860

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

correct answers 1 Correct answer

Participant , Dec 22, 2020 Dec 22, 2020

hi, below is a simple script specially for your task
1 create a new document
2 run the script
3 enter page numbers (for example 1-5,7)
4 be happy

#targetengine 'start page number'
// ______________________________________ DIALOG ______________________________________

function myChoice(){
	var myWindow = new Window('dialog','start page number',undefined)
	myWindow.orientation = 'row'
	myWindow.alignChildren = 'top'
	myWindow.size = [300,100]

	//  ------- PAGES -------
	var myPages = myWindow.add('ed
...

Votes

Translate

Translate
Community Expert ,
Dec 21, 2020 Dec 21, 2020

Copy link to clipboard

Copied

If I'm following what you're looking to do, the script would have to know how many pages were removed.  I'm not sure there's a way to track that.

Might be easier to manually number the pages.  

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
Participant ,
Dec 22, 2020 Dec 22, 2020

Copy link to clipboard

Copied

hi, below is a simple script specially for your task
1 create a new document
2 run the script
3 enter page numbers (for example 1-5,7)
4 be happy

#targetengine 'start page number'
// ______________________________________ DIALOG ______________________________________

function myChoice(){
	var myWindow = new Window('dialog','start page number',undefined)
	myWindow.orientation = 'row'
	myWindow.alignChildren = 'top'
	myWindow.size = [300,100]

	//  ------- PAGES -------
	var myPages = myWindow.add('edittext',undefined,'',{multiline:true})
	myPages.size = [200,75]
	myPages.active = true
	myPages.onChanging = myPages_onChanging
	
	//  ------- BUTTON -------
	var myButton = myWindow.add('button',undefined,'OK')
	myButton.size = [50,40]
	myButton.onClick = myButton_click
	
	myWindow.show()

// ______________________________________ EVENTS ______________________________________
	
	function myPages_onChanging(){
		this.text = this.text.replace(/[^\d-,]+/,'')
	}
	
	function myButton_click(){
		parse(myPages.text)
		myWindow.close()
	}
}

// ______________________________________ MAIN ______________________________________

Array.prototype.indexOf = function(item){
	for(var i = 0; i < this.length; i++){if(this[i] === item){return i}}
	return -1
}

function parse(mstr){
	for(var i = 0; i < mstr.split(',').length; i++){
		if(mstr.split(',')[i] == ''){continue}

		var mcur = mstr.split(',')[i].split('-')
		if(mcur[0] == '' && mcur[1] == ''){continue}
		
		var start = mcur[0] == '' ? mcur[1] : mcur[0]
		var end = mcur[1] == '' || typeof mcur[1] == 'undefined' ? mcur[0] : mcur[1]
		
		for(var ii = Number(start); ii <= Number(end); ii++){
			if(ii == 0 || ii > 9999){continue}
			if(pages.indexOf(ii) == -1){pages.push(ii)}
		}
	}
}

function main(){
	pages.sort(function(a,b){return Number(a)-Number(b)})
	for(var i = 0; i < pages.length; i++){
		doc.pages.add(LocationOptions.AT_END)
		doc.sections.add(doc.pages.item(-1))
		doc.pages.item(-1).appliedSection.continueNumbering = false
		doc.pages.item(-1).appliedSection.pageNumberStart = pages[i]
	}
	alert('done!')
}

try{var doc = app.activeDocument}
catch(e){exit()}

var pages = []
myChoice()
if(pages.length != 0){app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'start page number')}

 

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
Engaged ,
Dec 22, 2020 Dec 22, 2020

Copy link to clipboard

Copied

Jaw drop. That works. Thank you!

 

I was not expecting a functioning script like this. Whenever I think I'm starting to understand this stuff, I am reminded I'm a coding infant. General question, do you think it's better to study the javascript language more than than studying how to write scripts for adobe apps?

 

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
Participant ,
Dec 22, 2020 Dec 22, 2020

Copy link to clipboard

Copied

Sometimes I feel like I'm a total noob)) haha
But! I can help you, which is great.
I do not know what you do at work, but I do know that a specific language is chosen for each specific task. More javascript? For what? Now my task is to create a retail catalog. That is all. And indesign javascript is the best solution - for any OS, any version.
When I have a different task, I will choose a different language (for example, python).
So, first you need to learn not the language, but the API.

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
Engaged ,
Jan 07, 2021 Jan 07, 2021

Copy link to clipboard

Copied

There are no changes to your script at all. It's exactally as you have it written. 

I'm running InDesign 16.0.1 and I'm on a mac running Catalina 10.15.7.

It crashed several times at the beginning but now it won't run at all. What I was trying to accomplish is to break down your script into the simplest form to see if I could understand it but also figure out the issue. 

 

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
Participant ,
Jan 07, 2021 Jan 07, 2021

Copy link to clipboard

Copied

What happens when the script crashes? Does it show some kind of window? Can you post a screenshot here?

 

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
Participant ,
Jan 07, 2021 Jan 07, 2021

Copy link to clipboard

Copied

I'll be back tomorrow (for me now it's 2am)

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
Engaged ,
Jan 07, 2021 Jan 07, 2021

Copy link to clipboard

Copied

2am! Dang man.  

Unfortunately it just crashes inDesign, no error message. Usually it's when I am inputing numbers.

NEW UPDATE* I just tried commenting out this line #targetengine 'start page number' and it seems to be working. I'll keep testing it out to make sure that was it. 

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
Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

I did not test on Mac, on my Win10 it works perfectly.

 

Maybe the problem is on this line:

myPages.active = true

but I'm not sure.

 

If I can test this on a Mac, I'll post in this thread what the problem might be.

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
Engaged ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

I have a question, I'm trying to break down the code and understand it. When I try to use this loop and just set the start numbers for for every page in an open document it throws an error. The loop counts correctly.

 

for(var i = 0; i < pages.length; i++){
myDoc.pages.item(-1).appliedSection.continueNumbering = false
myDoc.pages.item(-1).appliedSection.pageNumberStart = pages[i]
}
 
I get this error
Invalid value for set property 'pageNumberStart'. Expected Long Integer (1 - 999999), but received "1".

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
Participant ,
Jan 08, 2021 Jan 08, 2021

Copy link to clipboard

Copied

ok, i add comments:

for(var i = 0; i < pages.length; i++){
	//add one page to the end of active document
	//now this page is last
	doc.pages.add(LocationOptions.AT_END)
	//add a new page numbering for last page
	doc.sections.add(doc.pages.item(-1))
	//disable auto numbering
	doc.pages.item(-1).appliedSection.continueNumbering = false
	//apply new number for last page
	//exactly a number, not a string: 1 not '1'
	doc.pages.item(-1).appliedSection.pageNumberStart = pages[i]
}

 

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
Community Expert ,
Jan 10, 2021 Jan 10, 2021

Copy link to clipboard

Copied

LATEST

> General question, do you think it's better to study the javascript language more than studying how to write scripts for adobe apps?

 

It amounts to the same: scripts for Adobe apps are written in JavaScript, AppleScrirpt, or Visual Basic. Only JavaScripts can be used on both Mac OS and Windows (AS only on Macs, VBasic only on Windows). So it makes sense to go for JavaScript, also because you get much more support for it e.g. here in this forum. Adobe's version of JavaScript is an old version of JavaScript, but it's legit JS and perfectly capable for writing scripts.

 

I'm trying to break down the code and understand it

 

To restart page numbering in a document, you have to add a section and set that section's page number start. Sychev's script consists of three parts: the interface (function myChoice()), a parser to break down your input (function parse()), and the core of the script, which adds the sections and pages (function main()). 

 

The error you see indicates that the script tries to set the page number start using '1', which is a string, while it should be an integer. Maybe you made a type error if you rekeyed the script, or maybe something funny happened with quotes. You could try using Number (pages[i]) instead of plain pages[i] . Sychev script does return an integer, but maybe something strane happens on your Mac.

 

P.

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