Skip to main content
Sayed Ali Mousawi
Known Participant
March 10, 2023
Answered

ScriptUI Button function is not working

  • March 10, 2023
  • 1 reply
  • 670 views

Hi friends, Everything seems fine in this code, but in practice, when I select a few paragraphs of text, run the script, and click the Apply button, nothing happens.

// Create a new window
var dialog = new Window("dialog");
dialog.text = "Apply Paragraph Styles";
// Add a button to the window
var applyButton = dialog.add("button", undefined, "Apply");
// Add an event listener for the button click event
applyButton.onClick = function() {
  // Get a reference to the active document
  var doc = app.activeDocument;
  // Get a reference to the selected paragraphs
  var selParas = doc.selection[0].paragraphs;
  // Loop through the selected paragraphs and apply the paragraph styles
  for (var i = 0; i < selParas.length; i++) {
    // Apply the first paragraph style to the first paragraph
    if (i == 0) {
      selParas[i].appliedParagraphStyle = doc.paragraphStyles.itemByName("MK 1");
    }
    // Apply the second paragraph style to the last paragraph
    else if (i == selParas.length - 1) {
      selParas[i].appliedParagraphStyle = doc.paragraphStyles.itemByName("MK 3");
    }
    // Apply the first paragraph style to all paragraphs in between
    else {
      selParas[i].appliedParagraphStyle = doc.paragraphStyles.itemByName("MK 2");
    }
  }
  // Close the dialog window
  dialog.close();
}
// Show the window
dialog.show();
This topic has been closed for replies.
Correct answer brian_p_dts

Can't run ID tasks while a dialog is open; try changing your window type to "panel" instead. But, what's the point of the window anyways? Why not just run the function inside the onClick? Dialogs/panels are useful from getting user info that you then pass to a func like you have in your onClick, outside the window after it has closed. 

1 reply

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
March 10, 2023

Can't run ID tasks while a dialog is open; try changing your window type to "panel" instead. But, what's the point of the window anyways? Why not just run the function inside the onClick? Dialogs/panels are useful from getting user info that you then pass to a func like you have in your onClick, outside the window after it has closed. 

Sayed Ali Mousawi
Known Participant
March 10, 2023

Thank you @brian_p_dts  , I change it to "palette" and now it is working.