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

Function to close selected file

Participant ,
Apr 14, 2023 Apr 14, 2023

hello, I have a script that allows me to select the open files and I would like pressing the OK button to close only the selected ones, but it doesn't work and I don't understand why

var openDocs = app.documents;
var docNames = [];

for (var i = 0; i < openDocs.length; i++) {
  docNames.push(openDocs[i].name);
}

var dialog = new Window("dialog", "Seleziona il file");
var checkGroup = dialog.add("group");
checkGroup.orientation = "column";
checkGroup.alignment = "left";
var checkboxes = [];
for (var i = 0; i < docNames.length; i++) {
  checkboxes[i] = checkGroup.add("checkbox", undefined, docNames[i]);
  checkboxes[i].value = false;
}
var buttonGroup = dialog.add("group");
buttonGroup.orientation = "row";
buttonGroup.alignment = "center";
var selectAllButton = buttonGroup.add("button", undefined, "Seleziona tutto");
selectAllButton.onClick = function() {
  for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].value = true;
  }
}
var deselectAllButton = buttonGroup.add("button", undefined, "Deseleziona tutto");
deselectAllButton.onClick = function() {
  for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].value = false;
  }
}
var okButton = buttonGroup.add("button", undefined, "OK");
okButton.onClick = function() {
  var selectedDocs = [];
  for (var i = 0; i < checkboxes.length; i++) {
    if (checkboxes[i].value == true) {
      selectedDocs.push(openDocs[i]);
    }
  }
  if (selectedDocs.length == 0) {
    alert("Nessun documento selezionato.");
  } else {
    try {
      for (var i = 0; i < selectedDocs.length; i++) {
        selectedDocs[i].close(SaveOptions.YES);
        while (selectedDocs[i].isValid) {
          // attesa che il documento sia chiuso
        }
      }
      dialog.close();
    } catch (error) {
      alert("Si è verificato un errore durante la chiusura dei documenti: " + error);
    }
  }
}
var cancelButton = buttonGroup.add("button", undefined, "Annulla");
cancelButton.onClick = function() {
  dialog.close();
}

dialog.show();

 

TOPICS
Scripting
930
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 1 Correct answer

Community Expert , Apr 14, 2023 Apr 14, 2023

I think the selectedDocs array isn’t right. Try this:

 

var openDocs = app.documents.everyItem().getElements()
var selectedDocs = [];
showDialog()
function showDialog(){
    var dialog = new Window("dialog", "Seleziona il file");
    var checkGroup = dialog.add("group");
    checkGroup.orientation = "column";
    checkGroup.alignment = "left";
    var checkboxes = [];
    for (var i = 0; i < openDocs.length; i++) {
      checkboxes[i] = checkGroup.add("checkbox", undefined, openDocs[i].name);
  
...
Translate
Community Expert ,
Apr 14, 2023 Apr 14, 2023

Hi,

 

Can you be a bit more explicit around the "it doesn't work" bit, what happens? is there an error, is it displaying a dialog or is it just doing nothing?

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
Participant ,
Apr 14, 2023 Apr 14, 2023

Yes, when i press OK button appear an error and do not happen nothing

 

An error occurred while closing documents:
Error: The request could not be handled due to a window
modal dialogue or an active alert.

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 ,
Apr 14, 2023 Apr 14, 2023

Hi @Samuel22307458169h , You are trying to close the docs before your dialog is closed. Try something like this where the closeSelected is called after the dialog closes :

 

var openDocs = app.documents;
var docNames = [];
for (var i = 0; i < openDocs.length; i++) {
  docNames.push(openDocs[i].name);
}

var selectedDocs = [];
showDialog()
function showDialog(){
    var dialog = new Window("dialog", "Seleziona il file");
    var checkGroup = dialog.add("group");
    checkGroup.orientation = "column";
    checkGroup.alignment = "left";
    var checkboxes = [];
    for (var i = 0; i < docNames.length; i++) {
      checkboxes[i] = checkGroup.add("checkbox", undefined, docNames[i]);
      checkboxes[i].value = false;
    }
    var buttonGroup = dialog.add("group");
    buttonGroup.orientation = "row";
    buttonGroup.alignment = "center";
    var selectAllButton = buttonGroup.add("button", undefined, "Seleziona tutto");
    selectAllButton.onClick = function() {
      for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].value = true;
      }
    }
    var deselectAllButton = buttonGroup.add("button", undefined, "Deseleziona tutto");
    deselectAllButton.onClick = function() {
      for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].value = false;
      }
    }
    //Button named "cancel" doesn’t need a close function
    var cancelButton = buttonGroup.add("button", undefined, undefined, {name: "cancel"}); 
    cancelButton.text = "Annulla"; 
    var okButton = buttonGroup.add("button", undefined, "OK");
    okButton.onClick = function() {
      for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].value == true) {
          selectedDocs.push(openDocs[i]);
        }
      }
      if (selectedDocs.length == 0) {
        alert("Nessun documento selezionato.");
      }
      dialog.close();
    }
    dialog.show()
    //after the dialog is closed run closeSelected function
    closeSelected()
}


function closeSelected(){
    for (var i = 0; i < selectedDocs.length; i++){
        selectedDocs[i].close(SaveOptions.NO)
    };   
}

 

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
Participant ,
Apr 14, 2023 Apr 14, 2023

Hi Rob, thanks for the answer, the script it works only if is selected 1 file, if are selected 2 or more files appear the error below

 

 

2023-04-14 15_06_41-Window.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
Community Expert ,
Apr 14, 2023 Apr 14, 2023

I think the selectedDocs array isn’t right. Try this:

 

var openDocs = app.documents.everyItem().getElements()
var selectedDocs = [];
showDialog()
function showDialog(){
    var dialog = new Window("dialog", "Seleziona il file");
    var checkGroup = dialog.add("group");
    checkGroup.orientation = "column";
    checkGroup.alignment = "left";
    var checkboxes = [];
    for (var i = 0; i < openDocs.length; i++) {
      checkboxes[i] = checkGroup.add("checkbox", undefined, openDocs[i].name);
      checkboxes[i].value = false;
    }
    var buttonGroup = dialog.add("group");
    buttonGroup.orientation = "row";
    buttonGroup.alignment = "center";
    var selectAllButton = buttonGroup.add("button", undefined, "Seleziona tutto");
    selectAllButton.onClick = function() {
      for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].value = true;
      }
    }
    var deselectAllButton = buttonGroup.add("button", undefined, "Deseleziona tutto");
    deselectAllButton.onClick = function() {
      for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].value = false;
      }
    }
    //Button named "cancel" doesn’t need a close function
    var cancelButton = buttonGroup.add("button", undefined, undefined, {name: "cancel"}); 
    cancelButton.text = "Annulla"; 
    var okButton = buttonGroup.add("button", undefined, "OK");
    okButton.onClick = function() {
      for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].value == true) {
            $.writeln(checkboxes[i].text)
            //returns the checkboxes text which can be used to get the document by name:
            selectedDocs.push(app.documents.itemByName(checkboxes[i].text));
        }
      }
      if (selectedDocs.length == 0) {
        alert("Nessun documento selezionato.");
      }
      dialog.close();
    }
    dialog.show()
    //after the dialog is closed run closeSelected function
    closeSelected()
}


function closeSelected(){
    for (var i = 0; i < selectedDocs.length; i++){
        selectedDocs[i].close(SaveOptions.YES)
    };
}

 

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
Participant ,
Apr 17, 2023 Apr 17, 2023
LATEST

Hi Rob, it works perfectly! Thanks so much!!!

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