Skip to main content
dublove
Legend
August 25, 2025
Answered

Once the dialog box is open, is it necessary to obtain the value? How can cancellation be allowed?

  • August 25, 2025
  • 3 replies
  • 333 views

Sometimes I open the dialog box, but then realize it was a mistake, so I cancel it.
At this point, I get an empty value, which causes an exception.
How can I cancel the dialog box and make it seem like nothing happened?

 

function makeDialog() {
    var mm = 2.835;
    var theDialog = app.dialogs.add({ name: "CN and CSP settings", canCancel: true, minHeight: 600, minWidth: 400, });
    with (theDialog.dialogColumns.add()) {
        staticTexts.add({ staticLabel: "CN:" });
        staticTexts.add({ staticLabel: "SP:" });
    }
    with (theDialog.dialogColumns.add()) {
        getCn = integerEditboxes.add({ editValue: 4 });
        app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
        getCsp = measurementEditboxes.add({ editValue: (6 * mm), editUnits: MeasurementUnits.millimeters });
    }

    if (theDialog.show() == true) {
        getCn = getCn.editContents;
        getCsp = getCsp.editValue / mm;
        //run a function that uses the global results
        //main()
        theDialog.destroy();
    }
}

 

Correct answer rob day

Remember to call the makeDialog() and main() functions, and set the result variables as global. If you click Cancel, the main() function never runs.

 

makeDialog()
var getCn, getCsp
function makeDialog() {
    var mm = 2.83465;
    var theDialog = app.dialogs.add({ name: "CN and CSP settings", canCancel: true, minHeight: 600, minWidth: 400, });
    with (theDialog.dialogColumns.add()) {
        staticTexts.add({ staticLabel: "CN:" });
        staticTexts.add({ staticLabel: "SP:" });
    }
    with (theDialog.dialogColumns.add()) {
        getCn = integerEditboxes.add({ editValue: 4 });
        getCsp = measurementEditboxes.add({ editValue: (6 * mm), editUnits: MeasurementUnits.millimeters });
    }

    if (theDialog.show() == true) {
        getCn = getCn.editContents;
        getCsp = getCsp.editValue / mm;
        //run a function that uses the global results
        //when OK is clicked
        //if Cancel is clicked main() does not run
        main()
        theDialog.destroy();
    }
}


function main(){
    app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
    alert(getCn + "\r" + getCsp)
    
    // CODE...
    
    //reset units
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
}

 

3 replies

rob day
Community Expert
Community Expert
August 25, 2025

Also, not sure if it matters to you but the 2.835 constant isn’t accuratly converting points to millimeters—I’m getting this:

 

 

2.83465 converts to 6mm as the default:
 

 

 

dublove
dubloveAuthor
Legend
August 25, 2025

Hi rob day.

Thank you very much.

 

My code is a bit messy and disorganized.
There is no main() function.
I just used it as is and didn't bother with it.

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
August 25, 2025

Remember to call the makeDialog() and main() functions, and set the result variables as global. If you click Cancel, the main() function never runs.

 

makeDialog()
var getCn, getCsp
function makeDialog() {
    var mm = 2.83465;
    var theDialog = app.dialogs.add({ name: "CN and CSP settings", canCancel: true, minHeight: 600, minWidth: 400, });
    with (theDialog.dialogColumns.add()) {
        staticTexts.add({ staticLabel: "CN:" });
        staticTexts.add({ staticLabel: "SP:" });
    }
    with (theDialog.dialogColumns.add()) {
        getCn = integerEditboxes.add({ editValue: 4 });
        getCsp = measurementEditboxes.add({ editValue: (6 * mm), editUnits: MeasurementUnits.millimeters });
    }

    if (theDialog.show() == true) {
        getCn = getCn.editContents;
        getCsp = getCsp.editValue / mm;
        //run a function that uses the global results
        //when OK is clicked
        //if Cancel is clicked main() does not run
        main()
        theDialog.destroy();
    }
}


function main(){
    app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
    alert(getCn + "\r" + getCsp)
    
    // CODE...
    
    //reset units
    app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
}

 

m1b
Community Expert
Community Expert
August 25, 2025

I don't use Indesign's native dialogs, so I'm not an expert, but if theDialog.show() returns false then I assume the user cancelled? If so, then you need to (a) change this

    if (theDialog.show() == true) {
        getCn = getCn.editContents;
        getCsp = getCsp.editValue / mm;
        //run a function that uses the global results
        //main()
        theDialog.destroy();
    }

to

    if (theDialog.show() !== true)
        // user cancelled
        return false;

    getCn = getCn.editContents;
    getCsp = getCsp.editValue / mm;
    
    // do your real stuff here
    
    theDialog.destroy();
    return true;

 

and then (b) in your parent scope main function do

if (!makeDialog())
    // user cancelled
    return;

 this a flowing river, returning false from each function and doing nothing each time.

 

But note: there are a million ways to set this up, and you didn't share enough code to be sure how you have done so. This is just for learning, not parroting.

- Mark

dublove
dubloveAuthor
Legend
August 25, 2025

Thanks, m1b.
I don't have a deep understanding obout"return". But you reminded me.

I tried many times, but I didn't know where to put the following, and it felt a little slow, to the point that my ID froze several times.

if (!makeDialog())
// user cancelled
return;


In the end, it worked simple like this,I don't know if this is the most efficient method.

    if (theDialog.show() == true) {
        getCn = getCn.editContents;
        getCsp = getCsp.editValue / mm;
        theDialog.destroy();
    }
    else {
        exit();
    }

 

m1b
Community Expert
Community Expert
August 25, 2025

That seems fine @dublove. You can only return from a function, not the top-level. I was assuming that you are running a "main" function via app.doScript, so you get the advantage of a single Undo record.

 

So you can't do this:

if (!makeDialog())
    return;

 

but you can do this:

function main() {
    if (!makeDialog())
        return;
};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Did something');

 

Anyway, using exit() is fine. I don't use it because I also write scripts for other apps which don't have it, and I never remember about it!