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

Scripting: duplicate pages

Explorer ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

818

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

Enthusiast , Dec 03, 2021 Dec 03, 2021

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

...

Votes

Translate

Translate
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Posting the relevant code would help. 

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
Explorer ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

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!

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
Explorer ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

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!

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 ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Hi Michael,

This seems an issue due to your dialog type, it's a modal dialog(which stops all user interactions until the dialog is dimissed). I have encountered issues with such modal dialogs where operations that result in changes on the document fail. What you could try is changing your dialog to a palette type. For that, change the argument to the Window constructor to palette, you would also need to run the code in a persistent engine for the palette to work. So the first two lines of your could would be something like the following

#targetengine "test"
var MP = new Window("palette");

-Manan

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
Enthusiast ,
Dec 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

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);

 

William Campbell

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
Explorer ,
Jan 17, 2022 Jan 17, 2022

Copy link to clipboard

Copied

LATEST

I very belated thanks, William... your solution got me immediately back on track, and I suppose my mind was too wrapped up in coding to be civilized!

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 ,
Dec 03, 2021 Dec 03, 2021

Copy link to clipboard

Copied

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)
}

 

Screen Shot.pngScreen Shot 1.png

 

 

 

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