Why is this script not applying paragraph styles
Hello
I have a script which makes paragraph styles based on typescale ratios
it seems that if the header - the first paragraph style - is too large, it will bump all other text to become overset
to which the script will then stop applying paragraph styles overset text
Any ideas how to get this script to apply paragraph styles to overset text
in short there are about 5 styles and 5 sample text samples of which the code generates
code below
as always thanking you all
Best
Smyth
// Define a function for your script
function myScript() {
// 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 static text field for preset ratios
var presetRatiosText = dialogColumn.staticTexts.add({
staticLabel: "Preset Ratios (Select One):"
});
// Add a dropdown with the specified names and scale factors, including "None"
var dropdown = dialogColumn.dropdowns.add({
stringList: ["None", "1.067 - Minor Second", "1.125 - Major Second", "1.200 - Minor Third", "1.250 - Major Third", "1.333 - Perfect Fourth", "1.414 - Augmented Fourth", "1.500 - Perfect Fifth", "1.618 - Golden Ratio"]
});
// Set "None" as the default selection
dropdown.selectedIndex = 0;
// Add a static text field for custom scale factor
var customScaleFactorText = dialogColumn.staticTexts.add({
staticLabel: "OR Enter a Custom Scale Factor:"
});
// Add an editbox for custom scale factor input
var customScaleFactorEditbox = dialogColumn.textEditboxes.add({
minWidth: 100
});
// Show the dialog box
if (dialog.show() == true) {
// Get the user's input from the dropdown
var selectedOption = dropdown.selectedIndex;
// Get the user's input from the custom scale factor editbox
var customScaleFactor = parseFloat(customScaleFactorEditbox.editContents);
// Get the body text size input
var bodyTextSize = parseFloat(editbox.editContents);
// Check if none of the values are entered
if (selectedOption === 0 && isNaN(customScaleFactor)) {
alert("Please enter a valid body text size and either pick a preset ratio or enter a custom scale factor.");
dialog.destroy();
return;
}
// Define an array of scale factors corresponding to the dropdown options
var scaleFactors = [null, 1.067, 1.125, 1.200, 1.250, 1.333, 1.414, 1.500, 1.618]; // Use null for "None"
// Get the selected scale factor or the user-defined scale factor
var scaleFactor;
if (selectedOption === 0) {
// If "None" is selected, use the custom scale factor
scaleFactor = customScaleFactor;
} else {
scaleFactor = scaleFactors[selectedOption];
}
var doc = app.activeDocument;
// Function to create or update a paragraph style with leading and no hyphens
function createOrUpdateParagraphStyle(styleName, pointSize, scaleFactor) {
try {
var style = doc.paragraphStyles.itemByName(styleName);
style.pointSize = pointSize;
style.leading = pointSize; // Set leading to match font size
style.hyphenation = false; // Disable hyphenation
} catch (e) {
style = doc.paragraphStyles.add({
name: styleName,
pointSize: pointSize,
leading: pointSize, // Set leading to match font size
hyphenation: false // Disable hyphenation
});
}
}
// Create or update paragraph styles with calculated sizes based on the selected scale factor
if (!isNaN(bodyTextSize) && selectedOption > 0) {
createOrUpdateParagraphStyle("Header", bodyTextSize * 1.5 * scaleFactor * 2, scaleFactor);
createOrUpdateParagraphStyle("Subheader", bodyTextSize * scaleFactor * 1.35, scaleFactor);
createOrUpdateParagraphStyle("Quote", bodyTextSize * 1.35, scaleFactor);
createOrUpdateParagraphStyle("Caption", bodyTextSize * 0.76, scaleFactor);
createOrUpdateParagraphStyle("Label", bodyTextSize, scaleFactor);
createOrUpdateParagraphStyle("Body Text", bodyTextSize, scaleFactor);
// Get the currently active spread and page
var activeSpread = app.activeWindow.activeSpread;
var activePage = app.activeWindow.activePage;
// Define a margin value (adjust this if needed)
var margin = 12; // You can adjust this value
// Calculate the bounds within the margins for the active page
var textFrameBounds = [
activePage.bounds[1] + activePage.marginPreferences.top + margin, // Top
activePage.bounds[0] + activePage.marginPreferences.left + margin, // Left
activePage.bounds[2] - activePage.marginPreferences.bottom - margin, // Bottom
activePage.bounds[3] - activePage.marginPreferences.right - margin // Right
];
// Create a text frame within the adjusted bounds on the active page
var textFrame = activePage.textFrames.add({
geometricBounds: textFrameBounds,
fillColor: "None",
strokeColor: "None",
autoSizingReferencePoint: AutoSizingReferenceEnum.TOP_LEFT_POINT,
autoSizingType: AutoSizingTypeEnum.OFF // Prevent text frame resizing
});
// Define sample text in the desired order with Unicode escape sequences for quotes and larger line gaps
var sampleText =
"Header: Sample Header Text\r\r" + // Add two "\r" for larger line gap
"Subheader: Sample Subheader Text\r\r" + // Add two "\r" for larger line gap
"Body Text: Sample Body Text\r\r" + // Add two "\r" for larger line gap
"Quote: \u201CSample Quote Text\u201D\r\r" + // Add two "\r" for larger line gap and Unicode escape sequences for smart quotes
"Caption: Sample Caption Text\r\r" + // Add two "\r" for larger line gap
"Label: Sample Label 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 Text: " + bodyTextSize + "pt\nHeader: " + bodyTextSize * scaleFactor * scaleFactor + "pt\nSubheader: " + bodyTextSize * scaleFactor * 1.2 + "pt\nQuote: " + bodyTextSize + "pt\nCaption: " + bodyTextSize + "pt\nLabel: " + bodyTextSize + "pt");
} else if (!isNaN(bodyTextSize) && selectedOption === 0) {
// No preset selected, use the custom scale factor
createOrUpdateParagraphStyle("Header", bodyTextSize * 1.5 * scaleFactor * 2, scaleFactor);
createOrUpdateParagraphStyle("Subheader", bodyTextSize * scaleFactor * 1.35, scaleFactor);
createOrUpdateParagraphStyle("Quote", bodyTextSize * 1.35, scaleFactor);
createOrUpdateParagraphStyle("Caption", bodyTextSize * 0.76, scaleFactor);
createOrUpdateParagraphStyle("Label", bodyTextSize, scaleFactor);
createOrUpdateParagraphStyle("Body Text", bodyTextSize, scaleFactor);
// Get the currently active spread and page
var activeSpread = app.activeWindow.activeSpread;
var activePage = app.activeWindow.activePage;
// Define a margin value (adjust this if needed)
var margin = 12; // You can adjust this value
// Calculate the bounds within the margins for the active page
var textFrameBounds = [
activePage.bounds[1] + activePage.marginPreferences.top + margin, // Top
activePage.bounds[0] + activePage.marginPreferences.left + margin, // Left
activePage.bounds[2] - activePage.marginPreferences.bottom - margin, // Bottom
activePage.bounds[3] - activePage.marginPreferences.right - margin // Right
];
// Create a text frame within the adjusted bounds on the active page
var textFrame = activePage.textFrames.add({
geometricBounds: textFrameBounds,
fillColor: "None",
strokeColor: "None",
autoSizingReferencePoint: AutoSizingReferenceEnum.TOP_LEFT_POINT,
autoSizingType: AutoSizingTypeEnum.OFF // Prevent text frame resizing
});
// Define sample text in the desired order with Unicode escape sequences for quotes and larger line gaps
var sampleText =
"Header: Sample Header Text\r\r" + // Add two "\r" for larger line gap
"Subheader: Sample Subheader Text\r\r" + // Add two "\r" for larger line gap
"Body Text: Sample Body Text\r\r" + // Add two "\r" for larger line gap
"Quote: \u201CSample Quote Text\u201D\r\r" + // Add two "\r" for larger line gap and Unicode escape sequences for smart quotes
"Caption: Sample Caption Text\r\r" + // Add two "\r" for larger line gap
"Label: Sample Label 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 Text: " + bodyTextSize + "pt\nHeader: " + bodyTextSize * scaleFactor * scaleFactor + "pt\nSubheader: " + bodyTextSize * scaleFactor * 1.2 + "pt\nQuote: " + bodyTextSize + "pt\nCaption: " + bodyTextSize + "pt\nLabel: " + bodyTextSize + "pt");
} else {
alert("Invalid input. Please enter a valid body text size and either pick a preset ratio or enter a custom scale factor.");
}
// Close the dialog box
dialog.destroy();
} else {
// User canceled the dialog
dialog.destroy();
}
}
// Use app.doScript() to make the script undoable
app.doScript(myScript, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Create or Update Styles");