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

ScriptUI Button function is not working

Explorer ,
Mar 10, 2023 Mar 10, 2023

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();
TOPICS
Scripting
557
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 , Mar 10, 2023 Mar 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. 

Translate
Community Expert ,
Mar 10, 2023 Mar 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. 

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
Explorer ,
Mar 10, 2023 Mar 10, 2023
LATEST

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

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