0

I have a script in InDesign which opens a folder dialog.

How can I show an alert if the user pressed the Cancel button and then stop the script?

Ria S.
  • 63
  • 1
  • 11

2 Answers2

2

There are two functions that open a File/Folder selection dialog and their behavior is subtly different, but this is the same for both:

• If the user clicks OK, returns a File object for the selected file, or an array of objects if multiple files are selected.
• If the user cancels, returns null.

So where you now have a line such as

 myFile = var myFolder.openDlg("Select a file", "*.*", false);

you can add this right after:

if (myFile == null)
{
    alert ("You pressed Cancel!");
    exit();
}

From a user interface point of view, I'd like to add that the alert is probably not necessary. If a script is said to undertake some action on/with a selected file or folder, pressing "Cancel" in an Open or Save Dialog clearly means the user changed his mind and wants it to stop.

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • One side note: while this works with predefined dialogs (folder or file open), how do i track the cancel button on custom dialogs in InDesign? It's a simple dialog with 2 options (create new template or open an existing one) but when I hit cancel it proceeds with an alert saying "Select your files" which means it continues to execute the script. – Ria S. May 25 '15 at 16:19
  • What dialog object are you using the InDesign Dialog Object or the ScriptUI framework ? – Loic May 26 '15 at 15:34
  • I think ScriptUI (sorry not too familiar with InDesign). I'm initializing it as var myDialog = app.dialogs.add({name:"Welcome"}); – Ria S. May 26 '15 at 18:19
  • 1
    It's not ScriptUI but InDesign Dialog Object. – Loic May 27 '15 at 07:52
2

It's not ScriptUI but InDesign Dialog Object.

here is a snippet from the doc…

var myDialog = app.dialogs.add({name:"Simple Dialog"});
//Add a dialog column.
with(myDialog.dialogColumns.add()){
staticTexts.add({staticLabel:"This is a very simple dialog box."});
}
//Show the dialog box.
var myResult = myDialog.show();
//If the user clicked OK, display one message;
//if they clicked Cancel, display a different message.
if(myResult == true){
alert("You clicked the OK button.");
}
else{
alert("You clicked the Cancel button.");
}
//Remove the dialog box from memory.
myDialog.destroy();
Loic
  • 2,173
  • 10
  • 13