Question
Help - indesign script code - dialog window preventing complete action
- October 8, 2024
- 2 replies
- 265 views
Can someone help me with a script code? I already have the code ready and done. This code does the following: 1) It breaks the texts into lines,2) breaks these lines into boxes, 3) applies a paragraph style to each of these boxes and 4) creates a small dash above each text box (separate).
I created a dialog window (palette) so that I can choose which paragraph style to apply and whether or not I want this dash to be inserted. However, the action only runs once, because it is as if the window closed and the code did not continue. How can I adjust this?
#targetengine 'sessions'
// Creating the Dialog Window
var myDlg = new Window('palette', 'Dimension Adjustment');
// Adding dimension style options
myDlg.panel1 = myDlg.add('panel', undefined, 'Dimension Style');
myDlg.panel1.orientation = 'column';
myDlg.panel1.alignChildren = ['left', 'top'];
myDlg.radiobutton1 = myDlg.panel1.add('radiobutton', undefined, 'Cota_POR');
myDlg.radiobutton2 = myDlg.panel1.add('radiobutton', undefined, 'Cota_POR_2');
myDlg.radiobutton2.value = true; // Cota_POR_2 as default
// Adding options for the line
myDlg.panel2 = myDlg.add('panel', undefined, 'Insert line?');
myDlg.panel2.orientation = 'row';
myDlg.radiobutton3 = myDlg.panel2.add('radiobutton', undefined, 'Yes');
myDlg.radiobutton4 = myDlg.panel2.add('radiobutton', undefined, 'No');
myDlg.radiobutton3.value = true; // "Yes" as default
// Adding action buttons
myDlg.buttonAdjust = myDlg.add('button', undefined, 'Adjust');
myDlg.buttonCancel = myDlg.add('button', undefined, 'Cancel');
// Function for the adjust button
myDlg.buttonAdjust.onClick = function() {
// Close the window
myDlg.close();
// Logic to process the selection and apply the style
if (app.selection.length === 1 && app.selection[0].hasOwnProperty("baseline") && app.selection[0].paragraphs.length > 0) {
var parStyle;
// Check if the paragraph style has been selected
if (myDlg.radiobutton1.value) {
parStyle = app.activeDocument.paragraphStyleGroups.item("IMAGEM").paragraphStyles.item("Cota_POR");
} else if (myDlg.radiobutton2.value) {
parStyle = app.activeDocument.paragraphStyleGroups.item("IMAGEM").paragraphStyles.item("Cota_POR_2");
}
// Replace two or more consecutive spaces with "\r" (line break)
app.findGrepPreferences = null; // Clear preferences
app.changeGrepPreferences = null; // Clear preferences
app.findGrepPreferences.findWhat = "\\s{2,}"; // Find two or more spaces
app.changeGrepPreferences.changeTo = "\r"; // Replace with line break
app.selection[0].changeGrep();
// Reference to the original text frame
var p = app.selection[0].parentTextFrames[0];
var lh = (p.lines[-1].baseline - p.lines[0].baseline) / (p.lines.length - 1); // Line height
var left = p.geometricBounds[1]; // Initial horizontal coordinate for the first box
var top = p.geometricBounds[0]; // Vertical coordinate (fixed to keep the boxes aligned horizontally)
var distanceBetweenFrames = 10; // Distance between text boxes (in points)
var frames = [];
// Iterate over the paragraphs (already separated by the inserted line breaks)
var paragraphs = app.selection[0].paragraphs;
for (var i = 0; i < paragraphs.length; i++) {
var para = paragraphs[i];
// Check if the paragraph is not empty
var paraContents = para.contents.replace(/^\s+|\s+$/g, ""); // Remove extra whitespace
if (paraContents !== "") {
// Create a new text box for each paragraph
var f = app.activeDocument.layoutWindows[0].activePage.textFrames.add({
geometricBounds: [
top, // Top position (fixed)
left, // Left side of the new box (horizontal position that increases for each box)
top + lh, // Height of the new box
left + 100 // Right side of the new box (arbitrary initial width)
]
});
// Move the current paragraph to the new text box
para.move(LocationOptions.AFTER, f.texts[0]);
frames.push(f); // Store the reference of the new box
// ** Apply the paragraph style **
if (parStyle) {
f.texts[0].applyParagraphStyle(parStyle, true);
}
// Apply vertical and horizontal autofit
f.textFramePreferences.autoSizingReferencePoint = AutoSizingReferenceEnum.TOP_LEFT_POINT;
f.textFramePreferences.autoSizingType = AutoSizingTypeEnum.HEIGHT_AND_WIDTH; // Autofit both height and width
// Adjust the horizontal coordinate for the next box (side by side)
left = f.geometricBounds[3] + distanceBetweenFrames;
// If "Yes" was selected, insert a line
if (myDlg.radiobutton3.value) {
var frameCenter = (f.geometricBounds[1] + f.geometricBounds[3]) / 2; // Center of the box (horizontally)
var line = app.activeDocument.layoutWindows[0].activePage.graphicLines.add({
geometricBounds: [
f.geometricBounds[0] - 4, // Top position of the line (above the box)
frameCenter, // Initial horizontal position of the line
f.geometricBounds[0] - 1, // Bottom position (length of 3 mm)
frameCenter // Final horizontal position of the line
]
});
// Set line thickness
line.strokeWeight = 0.75; // Thickness of 0.75pt
// Apply color and tint to the line
var colorName = "1 POR 2025";
var color;
try {
color = app.activeDocument.swatches.item(colorName);
if (!color.isValid) {
throw new Error("Color not found.");
}
// Apply the color to the line stroke
line.strokeColor = color;
line.strokeTint = 50;
} catch (e) {
// The alert has been removed
}
}
}
// Remove the processed paragraph (only if it was not moved)
para.remove();
}
// Remove the original text box after moving all content
p.remove();
// Clear search and replace preferences
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
// Success message
alert("Dimensions and lines successfully applied and created!");
} else {
alert("Please select text to process.");
}
}
// Function for the cancel button
myDlg.buttonCancel.onClick = function() {
myDlg.close();
}
// Displaying the window
myDlg.show();
