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

Dialog scroolbar

Participant ,
May 10, 2023 May 10, 2023

Hi all, I have this code, which allows me to select open documents individually and selectively close them, it works well, but when I have many documents open, the selection window it creates is too long and exits the screen, how can I add a scroll setting Is the height of the window fixed? Thank you

 

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)
    };
}
TOPICS
Scripting
893
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 2 Correct answers

Participant , May 10, 2023 May 10, 2023

Hi m1b, thanks for the answer, I hadn't thought about it, but it was what I needed, I modified the script and now it works as I want, thanks for the suggestion

 

var openDocs = app.documents.everyItem().getElements()
var selectedDocs = [];
showDialog()

function showDialog(){
    var dialog = new Window("dialog", "Seleziona il file");
    var listBox = dialog.add("listbox", [0, 0, 500, 500], undefined, {multiselect: true});
    listBox.maximumSize.width = 600;
    listBox.maximumSize.height = 600;
...
Translate
Community Expert , May 10, 2023 May 10, 2023

Excellent work! Here is a quick version with a few tweaks. You may see a few little things that can help. Keep up the great work! 🙂

- Mark

 

var openDocs = app.documents.everyItem().getElements();
openDocs.sort(function (a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; });

var selectedDocs = [];
showDialog()

function showDialog() {
    var dialog = new Window("dialog", "Seleziona il file");

    var listBox = dialog.add('listBox', undefined, '', { numberOfColum
...
Translate
Community Expert ,
May 10, 2023 May 10, 2023

Hi @Samuel22307458169h, I don't think you can scroll a group of checkboxes. It seems to me that a better control for your script would be a ListBox. You can multi-select quite naturally and probably wouldn't need select all and select none buttons. Also handles scrolling for you. What do you think?

- 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
Participant ,
May 10, 2023 May 10, 2023

Hi m1b, thanks for the answer, I hadn't thought about it, but it was what I needed, I modified the script and now it works as I want, thanks for the suggestion

 

var openDocs = app.documents.everyItem().getElements()
var selectedDocs = [];
showDialog()

function showDialog(){
    var dialog = new Window("dialog", "Seleziona il file");
    var listBox = dialog.add("listbox", [0, 0, 500, 500], undefined, {multiselect: true});
    listBox.maximumSize.width = 600;
    listBox.maximumSize.height = 600;
    

    for (var i = 0; i < openDocs.length; i++) {
        listBox.add("item", openDocs[i].name);
    }

    var buttonGroup = dialog.add("group");
    buttonGroup.orientation = "row";
    buttonGroup.alignment = "center";

    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 < listBox.items.length; i++) {
            if (listBox.items[i].selected == true) {
                selectedDocs.push(app.documents.itemByName(listBox.items[i].text));
            }
        }

        if (selectedDocs.length == 0) {
            alert("Nessun documento selezionato.");
        }

        dialog.close();
    }

    dialog.show();
    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
Community Expert ,
May 10, 2023 May 10, 2023

Excellent work! Here is a quick version with a few tweaks. You may see a few little things that can help. Keep up the great work! 🙂

- Mark

 

var openDocs = app.documents.everyItem().getElements();
openDocs.sort(function (a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; });

var selectedDocs = [];
showDialog()

function showDialog() {
    var dialog = new Window("dialog", "Seleziona il file");

    var listBox = dialog.add('listBox', undefined, '', { numberOfColumns: 1, multiselect: true });

    for (var i = 0; i < openDocs.length; i++) {
        var item = listBox.add('item', openDocs[i].name);
    }

    listBox.maximumSize.height = getScreenDimensions()[1] - 200;
    listBox.preferredSize = [350, -1];
    var buttonGroup = dialog.add("group");
    buttonGroup.orientation = "row";
    //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 < listBox.selection.length; i++) {
            selectedDocs.push(openDocs[listBox.selection[i].index]);
        }
        if (selectedDocs.length == 0) {
            alert("Nessun documento selezionato.");
        }
        dialog.close(1);
    };

    var result = dialog.show();

    if (result === 1) {
        // ok button
        closeSelected(selectedDocs)
    }
    else if (result === 2) {
        // user cancelled
        return;
    }


};

function closeSelected(docs) {
    for (var i = docs.length - 1; i >= 0; i--) {
        try {
            docs[i].close(SaveOptions.YES)
        } catch (error) {
            // user cancelled dialog
        }
    };
};

function getScreenDimensions(index) {
    var screen = $.screens[Math.min(index || 0, $.screens.length - 1)];
    return [screen.right - screen.left, screen.bottom - screen.top];
};

 

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 ,
May 10, 2023 May 10, 2023

Thank you very 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
Community Expert ,
May 10, 2023 May 10, 2023
LATEST

You're welcome!

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