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?
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?
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.
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();