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();
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.
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.
Copy link to clipboard
Copied
Thank you @brian_p_dts , I change it to "palette" and now it is working.