Copy link to clipboard
Copied
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");
Instead of
var paragraphs = textFrame.paragraphs;
use this:
var paragraphs = textFrame.parentStory.paragraphs;
Hi @SmythWharf , Also, define your measurement units—this is what I get when the doc is set to inches:
I think this works but it seems to vary a bit regarding being the full size of inside the margins
You should be able to eliminate the unit conversion code (and variations) by setting the scripting units to points, and the margin variable, just after your doc variable, and reset at the end:
// 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 dial
...
Copy link to clipboard
Copied
Instead of
var paragraphs = textFrame.paragraphs;
use this:
var paragraphs = textFrame.parentStory.paragraphs;
Copy link to clipboard
Copied
Hello Peter,
Thank you very much for your insight.
Will take a look shortly,
Best,
Smyth
Copy link to clipboard
Copied
Hi @SmythWharf , Also, define your measurement units—this is what I get when the doc is set to inches:
Copy link to clipboard
Copied
// 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 both preset and custom scale factors are entered
if (selectedOption !== 0 && !isNaN(customScaleFactor)) {
alert("Please pick either a preset ratio or enter a custom scale factor, not both.");
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 rounded font size and leading (line height)
function createOrUpdateParagraphStyle(styleName, pointSize, scaleFactor) {
try {
var style = doc.paragraphStyles.itemByName(styleName);
// Set the font size after rounding it to the nearest 0.5
style.pointSize = roundToNearestHalf(pointSize);
// Set the leading (line height) after rounding it to the nearest 0.5
style.leading = roundToNearestHalf(pointSize);
style.hyphenation = false; // Disable hyphenation
} catch (e) {
style = doc.paragraphStyles.add({
name: styleName,
// Set the font size after rounding it to the nearest 0.5
pointSize: roundToNearestHalf(pointSize),
// Set the leading (line height) after rounding it to the nearest 0.5
leading: roundToNearestHalf(pointSize),
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;
// Get the document's measurement units (inches, millimeters, points, etc.)
var docUnits = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
// Define the margin value (in inches for consistency)
var margin = 0.16667; // 0.16667 inches is approximately 12 points
// Convert margin to points based on the document's units
if (docUnits === MeasurementUnits.MILLIMETERS) {
// Convert millimeters to points (1 millimeter = 2.83465 points)
margin *= 2.83465;
}
// 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.parentStory.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: " + roundToNearestHalf(bodyTextSize) + "pt\nHeader: " + roundToNearestHalf(bodyTextSize * scaleFactor * scaleFactor) + "pt\nSubheader: " + roundToNearestHalf(bodyTextSize * scaleFactor * 1.2) + "pt\nQuote: " + roundToNearestHalf(bodyTextSize) + "pt\nCaption: " + roundToNearestHalf(bodyTextSize) + "pt\nLabel: " + roundToNearestHalf(bodyTextSize) + "pt");
} else if (!isNaN(bodyTextSize) && selectedOption === 0) {
// No preset selected, use the custom scale factor
createOrUpdateParagraphStyle("Header", roundToNearestHalf(bodyTextSize * 1.5 * scaleFactor * 2), scaleFactor);
createOrUpdateParagraphStyle("Subheader", roundToNearestHalf(bodyTextSize * scaleFactor * 1.35), scaleFactor);
createOrUpdateParagraphStyle("Quote", roundToNearestHalf(bodyTextSize * 1.35), scaleFactor);
createOrUpdateParagraphStyle("Caption", roundToNearestHalf(bodyTextSize * 0.76), scaleFactor);
createOrUpdateParagraphStyle("Label", roundToNearestHalf(bodyTextSize), scaleFactor);
createOrUpdateParagraphStyle("Body Text", roundToNearestHalf(bodyTextSize), scaleFactor);
// Get the currently active spread and page
var activeSpread = app.activeWindow.activeSpread;
var activePage = app.activeWindow.activePage;
// Get the document's measurement units (inches, millimeters, points, etc.)
var docUnits = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
// Define the margin value (in inches for consistency)
var margin = 0.16667; // 0.16667 inches is approximately 12 points
// Convert margin to points based on the document's units
if (docUnits === MeasurementUnits.MILLIMETERS) {
// Convert millimeters to points (1 millimeter = 2.83465 points)
margin *= 2.83465;
}
// 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.parentStory.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: " + roundToNearestHalf(bodyTextSize) + "pt\nHeader: " + roundToNearestHalf(bodyTextSize * scaleFactor * scaleFactor) + "pt\nSubheader: " + roundToNearestHalf(bodyTextSize * scaleFactor * 1.2) + "pt\nQuote: " + roundToNearestHalf(bodyTextSize) + "pt\nCaption: " + roundToNearestHalf(bodyTextSize) + "pt\nLabel: " + roundToNearestHalf(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();
}
}
// Function to round a number to the nearest 0.5
function roundToNearestHalf(number) {
return Math.round(number * 2) / 2;
}
// Use app.doScript() to make the script undoable
app.doScript(myScript, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Create or Update Styles");
I think this works but it seems to vary a bit regarding being the full size of inside the margins
not as big a deal but its so so
Copy link to clipboard
Copied
I think this works but it seems to vary a bit regarding being the full size of inside the margins
You should be able to eliminate the unit conversion code (and variations) by setting the scripting units to points, and the margin variable, just after your doc variable, and reset at the end:
// 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 both preset and custom scale factors are entered
if (selectedOption !== 0 && !isNaN(customScaleFactor)) {
alert("Please pick either a preset ratio or enter a custom scale factor, not both.");
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;
var margin = 25;
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
// Function to create or update a paragraph style with rounded font size and leading (line height)
function createOrUpdateParagraphStyle(styleName, pointSize, scaleFactor) {
try {
var style = doc.paragraphStyles.itemByName(styleName);
// Set the font size after rounding it to the nearest 0.5
style.pointSize = roundToNearestHalf(pointSize);
// Set the leading (line height) after rounding it to the nearest 0.5
style.leading = roundToNearestHalf(pointSize);
style.hyphenation = false; // Disable hyphenation
} catch (e) {
style = doc.paragraphStyles.add({
name: styleName,
// Set the font size after rounding it to the nearest 0.5
pointSize: roundToNearestHalf(pointSize),
// Set the leading (line height) after rounding it to the nearest 0.5
leading: roundToNearestHalf(pointSize),
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;
// 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.parentStory.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: " + roundToNearestHalf(bodyTextSize) + "pt\nHeader: " + roundToNearestHalf(bodyTextSize * scaleFactor * scaleFactor) + "pt\nSubheader: " + roundToNearestHalf(bodyTextSize * scaleFactor * 1.2) + "pt\nQuote: " + roundToNearestHalf(bodyTextSize) + "pt\nCaption: " + roundToNearestHalf(bodyTextSize) + "pt\nLabel: " + roundToNearestHalf(bodyTextSize) + "pt");
} else if (!isNaN(bodyTextSize) && selectedOption === 0) {
// No preset selected, use the custom scale factor
createOrUpdateParagraphStyle("Header", roundToNearestHalf(bodyTextSize * 1.5 * scaleFactor * 2), scaleFactor);
createOrUpdateParagraphStyle("Subheader", roundToNearestHalf(bodyTextSize * scaleFactor * 1.35), scaleFactor);
createOrUpdateParagraphStyle("Quote", roundToNearestHalf(bodyTextSize * 1.35), scaleFactor);
createOrUpdateParagraphStyle("Caption", roundToNearestHalf(bodyTextSize * 0.76), scaleFactor);
createOrUpdateParagraphStyle("Label", roundToNearestHalf(bodyTextSize), scaleFactor);
createOrUpdateParagraphStyle("Body Text", roundToNearestHalf(bodyTextSize), scaleFactor);
// Get the currently active spread and page
//var activeSpread = app.activeWindow.activeSpread;
var activePage = app.activeWindow.activePage;
// 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.parentStory.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: " + roundToNearestHalf(bodyTextSize) + "pt\nHeader: " + roundToNearestHalf(bodyTextSize * scaleFactor * scaleFactor) + "pt\nSubheader: " + roundToNearestHalf(bodyTextSize * scaleFactor * 1.2) + "pt\nQuote: " + roundToNearestHalf(bodyTextSize) + "pt\nCaption: " + roundToNearestHalf(bodyTextSize) + "pt\nLabel: " + roundToNearestHalf(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.");
}
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
// Close the dialog box
dialog.destroy();
} else {
// User canceled the dialog
dialog.destroy();
}
}
// Function to round a number to the nearest 0.5
function roundToNearestHalf(number) {
return Math.round(number * 2) / 2;
}
// Use app.doScript() to make the script undoable
app.doScript(myScript, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Create or Update Styles");
Copy link to clipboard
Copied
Hi Rob,
Many thanks for staring at this, seems super consistent at the moment.
appreciate the time taken,
Best,
Smyth
Find more inspiration, events, and resources on the new Adobe Community
Explore Now