Script for auto Typescales - paragraph styles applied to some sample text but not all?
Hello all - hopefully a quick one
I have a scipt which makes x5 styles and once run it makes paragraph styles and creates a text box with a x5 lines of sammple text one for each paragraph style it makes. It apples the para styles to body, style 1 2 3 but fails to do it for 4 5.
I am confused as the var for 4 5 I think are named okay. The paragraph style is applied by the code looking at the sample text and hunting for style N: for example, it then hunts for paragraph styles sees style N and then applies the style to the sample text
Here is the code
Many thanks as always,
Best
Smyth
// Create a dialog box
var dialog = app.dialogs.add({
name: "Body Text Size",
canCancel: true
});
// Add a dialog column
var dialogColumn = dialog.dialogColumns.add();
// Add a static text field
var staticText = dialogColumn.staticTexts.add({
staticLabel: "Enter the body text size in points:"
});
// Add an editbox for user input
var editbox = dialogColumn.textEditboxes.add({
minWidth: 100
});
// Add a dropdown with scale factor options
var dropdown = dialogColumn.dropdowns.add({
stringList: ["Body (1x)", "Style 1 (1x)", "Style 2 (2x)", "Style 3 (4x)", "Style 4 (4x)", "Style 5 (5x)"]
});
// Show the dialog box
if (dialog.show() == true) {
// Get the user's input from the dropdown
var selectedOption = dropdown.selectedIndex;
// Define an array of scale factors corresponding to the dropdown options
var scaleFactors = [1, 1, 2, 4, 4, 5]; // Scale factors for "Body" and "Style 1" to "Style 5"
// Get the selected scale factor
var scaleFactor = scaleFactors[selectedOption];
var doc = app.activeDocument;
// Function to create or update a paragraph style
function createOrUpdateParagraphStyle(styleName, pointSize) {
try {
var style = doc.paragraphStyles.itemByName(styleName);
style.pointSize = pointSize;
} catch (e) {
style = doc.paragraphStyles.add({ name: styleName, pointSize: pointSize });
}
}
// Create or update paragraph styles with calculated sizes based on the selected scale factor
var bodyTextSize = parseFloat(editbox.editContents);
if (!isNaN(bodyTextSize)) {
createOrUpdateParagraphStyle("Body", bodyTextSize); // Keep "Body" as "Body"
createOrUpdateParagraphStyle("Style 1", bodyTextSize * scaleFactor);
createOrUpdateParagraphStyle("Style 2", bodyTextSize * scaleFactor * 2);
createOrUpdateParagraphStyle("Style 3", bodyTextSize * scaleFactor * 4);
createOrUpdateParagraphStyle("Style 4", bodyTextSize * scaleFactor * 4);
createOrUpdateParagraphStyle("Style 5", bodyTextSize * scaleFactor * 5);
// Create a text frame to display sample text with narrower bounds
var textFrame = doc.pages[0].textFrames.add({
geometricBounds: [10, 10, 100, 100], // Adjust the coordinates and dimensions as needed
fillColor: "None",
strokeColor: "None"
});
// Define sample text in the desired order with Unicode escape sequences for quotes and larger line gaps
var sampleText =
"Body: Sample Body Text\r\r" +
"Style 1: Sample Style 1 Text\r\r" +
"Style 2: Sample Style 2 Text\r\r" +
"Style 3: Sample Style 3 Text\r\r" +
"Style 4: Sample Style 4 Text\r\r" +
"Style 5: Sample Style 5 Text";
// Place the sample text in the text frame and apply paragraph styles
textFrame.contents = sampleText;
var paragraphs = textFrame.paragraphs;
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
var styleName = paragraph.contents.split(":")[0];
var style = doc.paragraphStyles.itemByName(styleName);
if (style.isValid) {
paragraph.appliedParagraphStyle = style;
}
}
alert("Styles created or updated:\nBody: " + bodyTextSize + "pt\nStyle 1: " + (bodyTextSize * scaleFactor) + "pt\nStyle 2: " + (bodyTextSize * scaleFactor * 2) + "pt\nStyle 3: " + (bodyTextSize * scaleFactor * 4) + "pt\nStyle 4: " + (bodyTextSize * scaleFactor * 4) + "pt\nStyle 5: " + (bodyTextSize * scaleFactor * 5) + "pt");
} else {
alert("Invalid input. Please enter a valid number for the body text size.");
}
// Close the dialog box
dialog.destroy();
} else {
// User canceled the dialog
dialog.destroy();
}
