Skip to main content
Known Participant
Sep 30, 2024 4:57 PM UTC
Answered

Script help: Quick way to check if a paragraph style has been moded

Hello 

 

is there method or something alreay within indesign API to check if there are paragraph style overrides which can product a boole result?

 

I have the following: 

// Function to check if the paragraph style has been overridden
function isParagraphStyleOverridden(paragraph) {
return paragraph.styleOverrides.length > 0; // Returns true if there are overrides
}



The reason being I had this stupid bit of code from a few months ago which really is terrible and would prefer a dynamic approach 

 

// Function to check if the paragraph matches its base paragraph style
function isStyleUnmodified(paragraph) {
    // Get the paragraph style applied to the paragraph
    var paragraphStyle = paragraph.appliedParagraphStyle;

    // If there's no style applied, return false
    if (!paragraphStyle) {
        return false;
    }

    // Get the properties to check
    var styleProperties = ['appliedFont', 'pointSize', 'fillColor', 'fillTint', 'justification', 
                           'leading', 'firstLineIndent', 'baselineShift', 'tracking', 
                           'capitalization', 'alignToBaseline', 'ruleAbove', 'ruleBelow', 
                           'spaceBefore', 'spaceAfter'];

    // Check each property against the paragraph style
    for (var i = 0; i < styleProperties.length; i++) {
        var prop = styleProperties[i];
        if (paragraph[prop] !== paragraphStyle[prop]) {
            return false; // If any property is different, return false
        }
    }
    
    // If all properties match, return true
    return true;
}

 

But not sure if there are other ways of doing this?

 

Thank you, 

 

Smyth 

This topic is closed to new replies. Start a new post to keep the conversation going.
rob day
rob dayCorrect answer
Community Expert
Sep 30, 2024 9:46 PM UTC

Quick script below will print out yes or no if there is paragaph style overrides 

 

// Check if there is a selection
if (app.selection.length === 0 || !(app.selection[0] instanceof Text)) {
    alert("Please select some text.");
} else {
    var selectedText = app.selection[0];

    // Check if the selected text has paragraphs
    if (selectedText.paragraphs.length > 0) {
        var paragraph = selectedText.paragraphs[0];
        var paragraphStyle = paragraph.appliedParagraphStyle.name;
        var hasOverrides = paragraph.styleOverridden; // Corrected property name

        // Create a message to display
        var message = "The applied paragraph style is: " + paragraphStyle + "\n";
        message += "Overrides: " + (hasOverrides ? "Yes" : "No");

        alert(message);
    } else {
        alert("The selected text does not contain any paragraphs.");
    }
}

Also, the object.hasOwnProperty() method might be useful for something like this, where there are a lot of objects that can contain paragraphs—textFame, insertionPoint, character, word, line, etc. You might be able to shorten your code to something like this:

 

if (app.selection.length>0 && app.selection[0].hasOwnProperty("paragraphs")) {
	if (app.selection[0].paragraphs[0].styleOverridden) {
        alert("The first paragraph of your selection has a style override");
    } else {
        alert("The first paragraph of your selection has no style overrides.");
    }
} else {
    alert("Select some text")
}

 

The paragraphs collection for an insertion point or character is the single paragraph containing the objects:

 

 

4 replies

rob day
Community Expert
Community Expert
Sep 30, 2024 6:54 PM UTC

I have the following:

 

Hi @SmythWharf , the property is .styleOverridden not .styleOverrides.

 

Other text objects also have the property— text, word, character, textStyleRange, etc.

Translate
Known Participant
Sep 30, 2024 7:07 PM UTC

Hello, 


Oh aces, will check it out shortly, 

 

Best

Translate
Known Participant
Sep 30, 2024 9:13 PM UTC

Quick script below will print out yes or no if there is paragaph style overrides 

 

// Check if there is a selection
if (app.selection.length === 0 || !(app.selection[0] instanceof Text)) {
    alert("Please select some text.");
} else {
    var selectedText = app.selection[0];

    // Check if the selected text has paragraphs
    if (selectedText.paragraphs.length > 0) {
        var paragraph = selectedText.paragraphs[0];
        var paragraphStyle = paragraph.appliedParagraphStyle.name;
        var hasOverrides = paragraph.styleOverridden; // Corrected property name

        // Create a message to display
        var message = "The applied paragraph style is: " + paragraphStyle + "\n";
        message += "Overrides: " + (hasOverrides ? "Yes" : "No");

        alert(message);
    } else {
        alert("The selected text does not contain any paragraphs.");
    }
}
Translate