通过 ExtendScript 刷新 InDesign 对话框以切换复选框?

问题描述 投票:0回答:1

我正在使用扩展脚本在 InDesign 中创建对话框。我正在从 .csv 文件读取数据以获取名称列表,并为每个名称创建复选框,以便用户在继续执行脚本之前决定与每个名称相关的一些选项。就脚本而言,一切都运行得很好,包括用户对选项的选择。

但是,鉴于所有单击来选中或取消选中复选框可能既耗时又烦人,我想在对话框中添加按钮,允许切换用于每个名称的相同选项的所有复选框。我完成了切换逻辑,它的工作方式就像我想要的那样(我从有效的一个版本中知道这一点,请参见下文),但我无法让对话框实际显示所做的更改。

当用户按下按钮来切换某些复选框组时,没有任何反应。我在对话框上使用 .update() 但这似乎没有做任何事情。

除了对话框上的 .update() 之外,我还尝试了其他选项,例如移动对话框、禁用并再次启用它、删除复选框并再次添加它们等,但到目前为止没有任何效果。我让这一切正常工作的唯一方法(以及为什么我知道切换逻辑本身正在工作)是实际上完全关闭对话框,然后在应用更改后立即再次打开它。然而,这看起来确实非常不令人满意并且容易出现错误/bug;点击太快就会崩溃,即使点击慢一点,对话框的消失和重新出现也不美观。

如果有人对如何刷新对话框以使其实际显示复选框值的更改有任何想法,我将非常感激。谢谢。

编辑:这是我的代码的通用版本以及显示带有一些测试数据的对话框的屏幕截图。

// The arrays 'participants' and 'workshops' are declared elsewhere

// Declare arrays to store the state of options for participants and workshops
optionsParticipants = [];
optionsWorkshops = [];

// Define the labels for participant options as "Option 1" to "Option 6"
var participantOptionLabels = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6"];

// Define the labels for workshop options as "Option 1" to "Option 5"
var workshopOptionLabels = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"];

// Initialize participant options array with a default checked state
for (var i = 0; i < participantOptionLabels.length; i++) {
    optionsParticipants[i] = [];
    for (var j = 0; j < participants.length; j++) {
        optionsParticipants[i][j] = true;
    }
}

// Initialize workshop options array with a default checked state
for (var i = 0; i < workshopOptionLabels.length; i++) {
    optionsWorkshops[i] = [];
    for (var j = 0; j < workshops.length; j++) {
        optionsWorkshops[i][j] = true;
    }
}

// Function to display the dialog window with options for workshops and participants
function showDialog() {
    // Create the main dialog window
    dlg = new Window('dialog', 'Workshop and Participant Options');
    dlg.orientation = 'column';
    dlg.alignChildren = 'fill';

    // Create a panel for the workshop section
    var workshopPanel = dlg.add('panel', undefined, 'Workshops');
    workshopPanel.alignChildren = 'fill';
    workshopPanel.margins = [10, 10, 10, 10];
    workshopPanel.preferredSize.width = 450;

    // Create a scrollable area for workshop options
    var workshopScrollGroup = workshopPanel.add('group', undefined);
    workshopScrollGroup.orientation = 'row';
    workshopScrollGroup.alignment = ['fill', 'fill'];
    workshopScrollGroup.margins = 10;
    workshopScrollGroup.maximumSize.height = 200;

    // Add a panel inside the scrollable area for workshop options
    var workshopScrollablePanel = workshopScrollGroup.add('panel');
    workshopScrollablePanel.orientation = 'column';
    workshopScrollablePanel.alignChildren = ['fill', 'top'];
    workshopScrollablePanel.spacing = 5;
    workshopScrollablePanel.preferredSize.width = 450;

    // Loop through each workshop and add options for them
    for (var i = 0; i < workshops.length; i++) {
        var workshopGroup = workshopScrollablePanel.add('group', undefined);
        workshopGroup.orientation = 'row';
        workshopGroup.alignment = 'left';

        // Display the date or label for each workshop
        var workshopDateText = workshopGroup.add('statictext', undefined, workshops[i].dateForInDesignOptions);
        workshopDateText.preferredSize.width = 150;

        // Add checkboxes for each option related to the workshop
        for (var j = 0; j < workshopOptionLabels.length; j++) {
            var workshopCheckbox = workshopGroup.add('checkbox', undefined, workshopOptionLabels[j]);
            workshopCheckbox.value = optionsWorkshops[j][i];

            // Store the state of the checkbox in the workshop options array
            (function(i, j) {
                workshopCheckbox.onClick = function () {
                    optionsWorkshops[j][i] = workshopCheckbox.value;
                };
            })(i, j);
        }

        // Add a separator line between workshop entries
        if (i < workshops.length - 1) {
            var separator = workshopScrollablePanel.add('panel', undefined, '');
            separator.alignment = 'fill';
            separator.minimumSize.height = 1;
            separator.maximumSize.height = 1;
            separator.graphics.foregroundColor = separator.graphics.newPen(separator.graphics.PenType.SOLID_COLOR, [0, 0, 0, 1], 1);
        }
    }

    // Add a scrollbar to the workshop scroll group
    var workshopScrollbar = workshopScrollGroup.add('scrollbar', undefined, 0, 0, 100);
    workshopScrollbar.alignment = ['right', 'fill'];
    workshopScrollbar.preferredSize.width = 20;

    // Link the scrollbar to the workshop scrollable panel
    workshopScrollbar.onChanging = function () {
        workshopScrollablePanel.location.y = -workshopScrollbar.value;
    };

    // Add a separator line between the workshop and participant sections
    var sectionSeparator = dlg.add('panel', undefined, '');
    sectionSeparator.alignment = 'fill';
    sectionSeparator.minimumSize.height = 1;
    sectionSeparator.maximumSize.height = 1;
    sectionSeparator.graphics.foregroundColor = sectionSeparator.graphics.newPen(sectionSeparator.graphics.PenType.SOLID_COLOR, [0, 0, 0, 1], 1);

    // Create a panel for the participant section
    var listPanel = dlg.add('panel', undefined, 'Participants');
    listPanel.alignChildren = 'fill';
    listPanel.margins = [10, 10, 10, 10];
    listPanel.preferredSize.width = 450;

    // Create a button group to allow toggling of participant checkboxes
    var buttonGroup = listPanel.add('group', undefined);
    buttonGroup.orientation = 'row';
    buttonGroup.alignment = 'center';

    // Add a button for each participant option to toggle its checkboxes
    for (var j = 0; j < participantOptionLabels.length; j++) {
        (function(j) {
            var button = buttonGroup.add('button', undefined, 'All ' + participantOptionLabels[j]);
            button.onClick = function () {
                toggleAllOptions(j);
            };
        })(j);
    }

    // Create a scrollable area for participant options
    var scrollGroup = listPanel.add('group', undefined);
    scrollGroup.orientation = 'row';
    scrollGroup.alignment = ['fill', 'fill'];
    scrollGroup.margins = 10;
    scrollGroup.maximumSize.height = 450;

    // Add a panel inside the scrollable area for participant options
    var scrollablePanel = scrollGroup.add('panel');
    scrollablePanel.orientation = 'column';
    scrollablePanel.alignChildren = ['fill', 'top'];
    scrollablePanel.spacing = 5;
    scrollablePanel.preferredSize.width = 450;

    // Loop through each participant and add options for them
    for (var i = 0; i < participants.length; i++) {
        var participantGroup = scrollablePanel.add('group', undefined);
        participantGroup.orientation = 'row';
        participantGroup.alignment = 'left';

        // Display the name or label for each participant
        var nameText = participantGroup.add('statictext', undefined, participants[i].name);
        nameText.preferredSize.width = 150;

        // Add checkboxes for each option related to the participant
        for (var j = 0; j < participantOptionLabels.length; j++) {
            var checkbox = participantGroup.add('checkbox', undefined, participantOptionLabels[j]);
            checkbox.value = optionsParticipants[j][i];

            // Store the state of the checkbox in the participant options array
            (function(i, j) {
                checkbox.onClick = function () {
                    optionsParticipants[j][i] = checkbox.value;
                };
            })(i, j);
        }

        // Add a separator line between participant entries
        if (i < participants.length - 1) {
            var separator = participantGroup.add('panel', undefined, '');
            separator.alignment = 'fill';
            separator.minimumSize.height = 1;
            separator.maximumSize.height = 1;
            separator.graphics.foregroundColor = separator.graphics.newPen(separator.graphics.PenType.SOLID_COLOR, [0, 0, 0, 1], 1);
        }
    }

    // Add a scrollbar to the participant scroll group
    var scrollbar = scrollGroup.add('scrollbar', undefined, 0, 0, 100);
    scrollbar.alignment = ['right', 'fill'];
    scrollbar.preferredSize.width = 20;

    // Link the scrollbar to the participant scrollable panel
    scrollbar.onChanging = function () {
        scrollablePanel.location.y = -scrollbar.value;
    };

    // Add OK and Cancel buttons for the dialog actions
    var actionButtonGroup = dlg.add('group', undefined);
    actionButtonGroup.alignment = 'center';
    actionButtonGroup.add('button', undefined, 'OK');
    actionButtonGroup.add('button', undefined, 'Cancel');

    // Show the dialog window
    var result = dlg.show();

    // Exit if the user clicks Cancel
    if (result !== 1) {
        exit();
    }
}

// Function to toggle the checkboxes for a specific participant option
function toggleAllOptions(optionIndex) {
    var checkedCount = 0;
    var uncheckedCount = 0;

    // Count how many checkboxes are checked or unchecked for the selected option
    for (var i = 0; i < participants.length; i++) {
        if (optionsParticipants[optionIndex][i]) {
            checkedCount++;
        } else {
            uncheckedCount++;
        }
    }

    // Determine whether to check or uncheck all based on the majority state
    var toggleState = checkedCount <= uncheckedCount;

    // Apply the determined state to all checkboxes for the selected option
    for (var i = 0; i < participants.length; i++) {
        var checkbox = scrollablePanel.children[i].children[1].children[optionIndex];
        checkbox.value = toggleState;
        optionsParticipants[optionIndex][i] = toggleState;
    }

    // Refresh the dialog layout to apply the changes
    dlg.layout.layout();
}

// Initialize and show the dialog
showDialog();

显示对话框的屏幕截图

adobe-indesign extendscript
1个回答
0
投票

我在 Adobe 论坛上找到了一些可以解决此问题的帮助。这基本上是一个正确引用复选框的问题,具有正确的级别(一旦结构发生一点变化就需要调整)和索引(需要为+1)。

您可以在此处找到包含解决方案的线程:https://community.adobe.com/t5/indesign-discussions/refresh-indesign-dialog-via-extendscript-to-toggle-checkboxes/m-p/14809933#M585729

© www.soinside.com 2019 - 2024. All rights reserved.