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

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

Guide ,
Aug 25, 2025 Aug 25, 2025

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

 

TOPICS
How to , Scripting
280
Translate
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 3 Correct answers

Guide , Aug 25, 2025 Aug 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();
    }
  
...
Translate
Community Expert , Aug 25, 2025 Aug 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

...
Translate
Community Expert , Aug 25, 2025 Aug 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:" });
    }
    wi
...
Translate
Community Expert ,
Aug 25, 2025 Aug 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

Translate
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
Guide ,
Aug 25, 2025 Aug 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();
    }

 

Translate
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 ,
Aug 25, 2025 Aug 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!

Translate
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 ,
Aug 25, 2025 Aug 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;
}

 

Translate
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 ,
Aug 25, 2025 Aug 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:

 

Screen Shot 1.png

 

2.83465 converts to 6mm as the default:
 
Screen Shot 2.png

 

 

Translate
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
Guide ,
Aug 25, 2025 Aug 25, 2025
LATEST

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.

Translate
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