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

ScriptUI Button function is not working

Explorer ,
Mar 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

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

Views

391

Translate

Translate

Report

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. 

Votes

Translate

Translate
Community Expert ,
Mar 10, 2023 Mar 10, 2023

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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