Skip to main content
Known Participant
December 2, 2021
Answered

Scripting: duplicate pages

  • December 2, 2021
  • 2 replies
  • 1490 views

Hey everyone, I'm both new to this forum and new to Javascript, please have as much patience as you can muster!

I'm in the process of writing a script for InDesign, and I've hit a few walls; one is that my code for duplicating a page isn't executing at all.

My script is set up as follows:

1. Prompt user to select a CSV file

2. Parses the CSV data and writes what I need to a couple of arrays and variables

3. Opens a dialog window asking for some user input

4. When you click one of the buttons, an onClick function is called

 

The onClick function does run, but the code I have within it to duplicate pages doesn't do anything. If I have that same code in its own script (not within the onClick function), it runs as expected and duplicates pages... but within the function, it won't run. 

 

Any thoughts or advice would go a long way, I'm sure! Thank you for your time!

 

- Michael

This topic has been closed for replies.
Correct answer willcampbell7

Have onClick handle the interface, get it closed, then do the "work." So in your example code, have...

generateDocument.onClick = function() {
    MP.close(1);
}
cancelButton.onClick = function() {
    MP.close(0);
}

Then test the result of showing the dialog.

if (MP.show() == 1) {
    // generateDocument was clicked.
    // Do the work.
}
// Else MP.show() == 0 means cancel was clicked.
// Let the script end without doing any work.

The values 1 and 0 are arbitrary. These are values passed back when calling .show() function. The values could be 100 and 200. Just so long as you test for what you know a particular button returns.

if (MP.show() == 100)...

For example, is true if the button onClick function called MP.close(100);

 

2 replies

rob day
Community Expert
Community Expert
December 3, 2021

Hi Michael, for straightforward dialogs you might look at InDesign’s built-in dialog class—it lets you build checkboxes, radio buttons, dropdowns, etc. and easily get returned values:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DialogColumn.html#d1e593097

 

Here‘s an example with a checkbox and a group of radio buttons, that captures the results for the main script:

 

//result variables
var ckBox, pageRadio, spreadRadio;  

makeDialog();

/**
* Make the  dialog 
*/
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Dialog", canCancel:true});
    with(theDialog){
        //a column with static labels
        with(dialogColumns.add()){
            staticTexts.add({staticLabel:"Check Box"});
            staticTexts.add({staticLabel:"Pages"});
            staticTexts.add({staticLabel:"Spreads"});
        }
        //a column of controls
        with(dialogColumns.add()){
            ckBox = checkboxControls.add({checkedState:false, minWidth:150});
            with(radiobuttonGroups.add()){
                pageRadio = radiobuttonControls.add({checkedState:true});
                spreadRadio = radiobuttonControls.add({checkedState:true});
            }
        }
    }
    //get the dialog results
    var res = theDialog.show();
    if(res == true){
        ckBox = ckBox.checkedState;
        pageRadio = pageRadio.checkedState
        spreadRadio = spreadRadio.checkedState
        main()
	}else{
        theDialog.destroy();
    }
}


/**
* The Script to run after dialog
* @Return void 
*/

function main(){
    alert("Dialog Results\ncheck box= "+ ckBox + "\npage radio= "+ pageRadio + "\nspread radio= "+ spreadRadio)
}

 

 

 

 

brian_p_dts
Community Expert
Community Expert
December 2, 2021

Posting the relevant code would help. 

mrlagaceAuthor
Known Participant
December 2, 2021

Sorry Brian, didn't want to inundate everyone with sloppy novice code if I didn't have to, thinking it was perhaps a simple structural problem. Here's what I hope is the relevant parts... I've left out the parts processing CSV and input data... 

 

var MP = new Window("dialog"); 
    MP.text = "Proof Tags - Job Information"; 
    MP.preferredSize.width = 500; 
    MP.preferredSize.height = 300; 
    MP.orientation = "column"; 
    MP.alignChildren = ["left","top"]; 
    MP.spacing = 10; 
    MP.margins = 16; 

// additional input fields

var mycomplete = MP.add("group", undefined, {name: "complete"}); 
    mycomplete.orientation = "row"; 
    mycomplete.alignChildren = ["right","center"]; 
    mycomplete.spacing = 10; 
    mycomplete.margins = 0; 

var actionButtonGroup = MP.add('group', undefined, '');
    actionButtonGroup.orientation = 'row';
var loadCSV = actionButtonGroup.add('button', undefined, 'Import CSV', {name:'loadCSV'});
var generateDocument = actionButtonGroup.add('button', undefined, 'Generate Proof Tags', {name:'generateDocument'});
var cancelButton = actionButtonGroup.add('button', undefined, 'Cancel', {name:'cancelButton'});


generateDocument.onClick = function() {
   MP.close();     // close open dialog window

// code sample from the internet
var docRef = app.activeDocument;
var numOfPages = docRef.pages.count();
var currentPageNumber = 1;

for (var i = numOfPages - 1; i >= 0; i--) {
  $.writeln("i: " + i);
  var pageRef = docRef.pages[currentPageNumber];
  pageRef = docRef.pages.add(LocationOptions.AFTER, pageRef);
  docRef.pages[currentPageNumber + 1].appliedMaster = docRef.masterSpreads.item ('B-Parent');
}


MP.show();

  

Thank you!

mrlagaceAuthor
Known Participant
December 2, 2021

No need for anyone else to waste time; I found a work-around, and it's not ideal, but it works... around. 

 

Sorry everyone, but thanks!