Skip to main content
Known Participant
September 30, 2024
Answered

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

  • September 30, 2024
  • 1 reply
  • 623 views

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 has been closed for replies.
Correct answer rob day

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:

 

 

1 reply

rob day
Community Expert
Community Expert
September 30, 2024

I have the following:

 

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

 

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

Known Participant
September 30, 2024

Hello, 


Oh aces, will check it out shortly, 

 

Best

Known Participant
September 30, 2024

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.");
    }
}