Skip to main content
Known Participant
September 7, 2023
Question

Script for auto Typescales - paragraph styles applied to some sample text but not all?

  • September 7, 2023
  • 1 reply
  • 284 views

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();
}

 

This topic has been closed for replies.

1 reply

Known Participant
September 7, 2023

Okay slightly better but still not working  - as later I will make it so that a user can add more than 5 styles so there could be 7 for example so the names will be added etc

this code now styles up to 4 but not 5 

There must be something I am doing wrong 

Many thanks 

Smyth

// Define your script as a function
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 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 text = paragraph.contents;

                // Extract style name and text content
                var match = text.match(/^(.*?):(.*)/);

                if (match && match.length === 3) {
                    var styleName = match[1].replace(/^\s+|\s+$/g, ''); // Remove leading and trailing spaces
                    var content = match[2].replace(/^\s+|\s+$/g, ''); // Remove leading and trailing spaces

                    var style = doc.paragraphStyles.itemByName(styleName);

                    if (style.isValid) {
                        paragraph.appliedParagraphStyle = style;
                        paragraph.contents = content;
                    }
                }
            }

            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();
    }
}

// Use app.doScript() to make the script undoable
app.doScript(myScript, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Create or Update Styles");

 

 

Known Participant
September 7, 2023

Seems like it could work now

// Define your script as a function
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 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, 5, 6]; // 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 * 3);
createOrUpdateParagraphStyle("Style 4", bodyTextSize * scaleFactor * 4);
createOrUpdateParagraphStyle("Style 5", bodyTextSize * scaleFactor * 5);
createOrUpdateParagraphStyle("Style 6", bodyTextSize * scaleFactor * 6);

// 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\r\r" +
"Style 6: Sample Style 6 Text\r\r";

// 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 text = paragraph.contents;

// Extract style name and text content
var match = text.match(/^(.*?):(.*)/);

if (match && match.length === 3) {
var styleName = match[1].replace(/^\s+|\s+$/g, ''); // Remove leading and trailing spaces
var content = match[2].replace(/^\s+|\s+$/g, ''); // Remove leading and trailing spaces

var style = doc.paragraphStyles.itemByName(styleName);

if (style.isValid) {
paragraph.appliedParagraphStyle = style;
paragraph.contents = content;
}
}
}

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();
}
}

// Use app.doScript() to make the script undoable
app.doScript(myScript, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Create or Update Styles");