• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Contributor ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

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 

TOPICS
How to , Print , Scripting , SDK , UXP Scripting

Views

223

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Sep 30, 2024 Sep 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.

Votes

Translate

Translate
Community Expert , Sep 30, 2024 Sep 30, 2024

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
...

Votes

Translate

Translate
Community Expert ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

I have the following:

 

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

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

Hello, 


Oh aces, will check it out shortly, 

 

Best

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2024 Sep 30, 2024

Copy link to clipboard

Copied

LATEST

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:

 

 

Screen Shot.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines