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

Script keeps lopping and nothing happens

Contributor ,
Jun 18, 2024 Jun 18, 2024

Hey guys,

Creating a new post to see if anyone can help me to sort out an issue with a script. Just to give you all a bit of context - @m1b helped me to put together a script that reduces font size and I added in some more codes to redefine Paragraph and Character styles as well, but only if there are no manual overrides on text. So, in case of any override, none of the styles are redefined to preserve formattting. So far so good. The problem is that, in case of no overrides found in the document, the script keeps lopping and nothing happens. Could you please help me to figure out why? Any help would be greatly apreacitated! 🙂 Thanks, Rogerio.

var doc  = app.activeDocument;

      // search for overrides on paragraphs and characters
      // turn on Style Override Highlighter for visibility
      doc.textPreferences.enableStylePreviewMode = true;

for (s = 0; s < doc.stories.length; s++) {          
    var myStory = doc.stories[s]; 
     for (p = 0; p < myStory.characters.length; p++) { 
        var myCharacter = myStory.characters[p];
    for (p = 0; p < myStory.paragraphs.length; p++) { 
        var myParagraph = myStory.paragraphs[p];
        if(myCharacter.styleOverridden == true){
        if(myParagraph.styleOverridden == true){
            alert("This document contains manual overrides on text. Styles will not be redefined to preserve formatting.");
         
        // turn off Style Override Highlighter
        doc.textPreferences.enableStylePreviewMode = false;  
            
function main() {

    // adjust this function to suit your needs
    function myTextAdjuster(text) {
        
        if (text.pointSize <= 6)
            return;

        // reduce point size by 1 point
        text.pointSize -= 1;

        /* remove auto leading */
        if (Leading.AUTO === text.leading)
            text.leading = (text.pointSize * text.autoLeading / 100);

        /* reduce leading by 2 points - only do this if you already removed auto leading! */
        text.leading -= 2;

        /* turn autoleading on and set auto value to 120% */
        // text.leading = Leading.AUTO;
        // text.autoLeading = 120;

    };

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    var everyStory = app.activeDocument.stories.everyItem();

    var textStyleRanges = everyStory.paragraphs.everyItem().textStyleRanges.everyItem().getElements();

    try{
    if (everyStory.tables.everyItem().cells.everyItem().paragraphs.length)
         }catch(e){}
             try{
        textStyleRanges = textStyleRanges.concat(everyStory.tables.everyItem().cells.everyItem().paragraphs.everyItem().textStyleRanges.everyItem().getElements())
         }catch(e){}
             try{
    if (everyStory.tables.everyItem().cells.everyItem().paragraphs.length)
             }catch(e){}
                          try{
        textStyleRanges = textStyleRanges.concat(everyStory.footnotes.everyItem().paragraphs.everyItem().textStyleRanges.everyItem().getElements());
         }catch(e){}
    for (var i = textStyleRanges.length - 1; i >= 0; i--)
        myTextAdjuster(textStyleRanges[i]);

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Reduce Document Text Sizes');

        // redefine styles if no overrides were found in the document
        if(myCharacter.styleOverridden == true){
        if(myParagraph.styleOverridden == true){
        exit();
        }
    }

for (s = 0; s < app.documents[0].stories.length; s++) {          
    var myStory = app.activeDocument.stories[s]; 
    
    // loop through each paragraph style in the document
    for (var i = 0; i < myStory.paragraphs.length; i++) {
        var para = myStory.paragraphs[i];
        var paraStyle = para.appliedParagraphStyle;
        paraStyle.properties = para.properties;
    }
    }

for (s = 0; s < app.documents[0].stories.length; s++) {          
    var myStory = app.activeDocument.stories[s]; 
    
    // loop through each character style in the document
    for (var i = 0; i < myStory.characters.length; i++) {
        var chara = myStory.characters[i];
        var charStyle = chara.appliedCharacterStyle;
        charStyle.properties = chara.properties;
    }
    }

alert("Done!");

            exit();
        }
    }
}
}
}

 

TOPICS
Bug , Scripting
503
Translate
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 1 Correct answer

Community Expert , Jul 02, 2024 Jul 02, 2024

Hi @Rogerio5C09, see if this is right. It worked on your test documents. Please follow along in the code to see what I am doing, and hopefully I have understood you right.

- Mark

 

/**
 * Reduce point size of all text in document by 1 pt,
 * and then redefine paragraph styles ONLY IF
 * no paragraph style overrides are found in document.
 *
 * CAUTION: script doesn't consider "based-on" paragraph
 * styles and I'm not sure what will happen in some cases
 * when redefining styles.
 * @author m1b
...
Translate
Community Expert ,
Jun 20, 2024 Jun 20, 2024

Hi @Rogerio5C09, I had a quick look at your code, but unfortunately I can't work out what you are really trying to achieve.

 

It might be good to supply two things:

(a) an Indesign demo document that we could run your script on to see the failure, and

(b) a rigorous explanation of what you expected to happen to that demo document if the script hadn't failed.

 

Scripts (and indesign documents!) get very complicated very quickly, so we need to make our forum questions very clear and complete—do not assume anybody knows what's in your head.

- Mark

 

P.S. some bonus coding advice: return early—makes code much better!

Translate
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 ,
Jun 21, 2024 Jun 21, 2024

Actually I just noticed that your code is malformed in places. Have a look at this:

Screenshot 2024-06-21 at 18.27.38.png

See the pink boxes. They are both errors because the if statement has no code. You can't break up an if statement with a try/catch. So those lines will instantly trigger the outer try/catch. Also both those if predicates are exactly the same, so not sure why you need both. Also check what errors you are getting by, eg. putting alert('a: '+e) into your catch blocks (the "a" should be replaced with a,b,c etc to identify which block is triggered). Also you are probably getting errors when there are no tables in the document, but it is neater to just check for this, eg. if (0 === everyStory.tables.length) continue; or something like that. Or I think you can do if (everyStory.tables.length && everyStory.tables.everyItem().cells.everyItem().paragraphs.length) which will check if there are any tables and if there aren't any it won't ask for the tables' cells and therefore won't throw error.

- Mark

Translate
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 ,
Jun 25, 2024 Jun 25, 2024

Hi @m1b,

Thanks for checking and my apologies if I wasn't clear enough. Please find the InDesign demo documents attached (with and without overrides). I'm also adding here the find Overrides and Redefine Styles scripts (see below). Is it possible to "merge" them with your script from this post? I'm asking you because this exactely what I'm trying to achieve. If not, maybe we could use those just as reference then? Just to clarify, the script should do the following steps:

  1. Find overrides on text. If any override is found in the document, skip step 3.
  2. Reduce point size (your script)
  3. Redefine Styles. If any override was found before step 2, no styles are redefined.
    4. Done!


The codes below are only for Paragraphs, but if you see my script, you will notice that I already included in the codes for Characters as well. I don't doubt they can be simplified though. Also, I added at the beginning doc.textPreferences.enableStylePreviewMode = true just to have an overview of where the overrides are present in the document (in case of any).

Answering "(b)", I expect that the script will meet the requirements described in the provided demo documents, reducing font size and redefining styles when applicable.

Find Overrides by loopping through all Stories and Paragraphs (original source)

 

 

for (s = 0; s < app.documents[0].stories.length; s++) {          
    var myStory = app.activeDocument.stories[s]; 
    for (p = 0; p < myStory.paragraphs.length; p++) { 
        var myParagraph = myStory.paragraphs[p];
        if(myParagraph.styleOverridden == true){
            alert("This document contains overrides on text");
            exit();
        }
    }
}

 

 


Redefine Paragraph Styles script (original source)

 

 

for (s = 0; s < app.documents[0].stories.length; s++) {          
    var myStory = app.activeDocument.stories[s]; 
    
    // Loop through each paragraph style in the document
    for (var i = 0; i < myStory.paragraphs.length; i++) {
        var para = myStory.paragraphs[i];
        var paraStyle = para.appliedParagraphStyle;
        paraStyle.properties = para.properties;
    }
    alert("Paragraph styles updated successfully for each paragraph.");
    }

 

 


Thanks again for your help and do let me know if anything is still unclear.

Regards,
Rogerio

Translate
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 ,
Jul 02, 2024 Jul 02, 2024

Hi @Rogerio5C09, see if this is right. It worked on your test documents. Please follow along in the code to see what I am doing, and hopefully I have understood you right.

- Mark

 

/**
 * Reduce point size of all text in document by 1 pt,
 * and then redefine paragraph styles ONLY IF
 * no paragraph style overrides are found in document.
 *
 * CAUTION: script doesn't consider "based-on" paragraph
 * styles and I'm not sure what will happen in some cases
 * when redefining styles.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/decrease-font-size-of-all-stories-by-1-or-2-points/m-p/14506004
 */
function main() {

    // adjust this function to suit your needs
    function myTextAdjuster(text) {

        if (text.pointSize <= 6)
            return;

        // reduce point size by 1 point
        text.pointSize -= 1;

        /* remove auto leading */
        if (Leading.AUTO === text.leading)
            text.leading = (text.pointSize * text.autoLeading / 100);

        /* reduce leading by 2 points - only do this if you already removed auto leading! */
        text.leading -= 2;

        /* turn autoleading on and set auto value to 120% */
        // text.leading = Leading.AUTO;
        // text.autoLeading = 120;

    };

    if (0 === app.documents.length)
        return alert('Please open a document and try again.');

    var doc = app.activeDocument;

    // 1. check to see if there are overrides in document
    var everyParagraph = doc.stories.length && doc.stories.everyItem().paragraphs.everyItem(),
        hasParagraphStyleOverrides = /true/.test(everyParagraph.styleOverridden);

    if (!everyParagraph)
        return alert('Document has no paragraphs.');

    // 2. make the adjustments
    adjustTextStyleRanges(doc, myTextAdjuster)

    if (hasParagraphStyleOverrides)
        return;

    // 3. redefine styles
    var paras = everyParagraph.getElements();

    for (var i = 0, style; i < paras.length; i++) {
        style = paras[i].appliedParagraphStyle;
        style.properties = paras[i].properties;
    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Reduce Document Text Sizes');


/**
 * Performs an adjustment to all
 * TextStyleRanges of a document.
 * @author m1b
 * @version 2024-07-02
 * @param {Document} doc - an Indesign Document.
 * @param {Function} adjuster - a function, given a TextStyleRange.
 */
function adjustTextStyleRanges(doc, adjuster) {

    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;

    var textStyleRanges = getTextStyleRanges(doc) || [];

    for (var i = textStyleRanges.length - 1; i >= 0; i--)
        adjuster(textStyleRanges[i], i);

};

/**
 * Returns array of TextStyleRanges
 * found in document.
 * @author m1b
 * @version 2024-07-02
 * @param {Document} doc - an Indesign Document.
 * @returns {Array<TextStyleRange>}
 */
function getTextStyleRanges(doc) {

    if (0 === doc.stories.length)
        return;

    var everyStory = app.activeDocument.stories.everyItem();

    if (
        0 === everyStory.length
        || 0 === everyStory.paragraphs.length
    )
        return;

    var textStyleRanges = everyStory.paragraphs.everyItem().textStyleRanges.everyItem().getElements();

    if (
        everyStory.tables.length
        && everyStory.tables.everyItem().cells.everyItem().paragraphs.length
    )
        textStyleRanges = textStyleRanges.concat(everyStory.tables.everyItem().cells.everyItem().paragraphs.everyItem().textStyleRanges.everyItem().getElements())

    if (everyStory.footnotes.everyItem().paragraphs.length)
        textStyleRanges = textStyleRanges.concat(everyStory.footnotes.everyItem().paragraphs.everyItem().textStyleRanges.everyItem().getElements());

    return textStyleRanges;

};

 

Translate
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 ,
Jul 17, 2024 Jul 17, 2024

Hi @m1b, you got that right! - the script worked just as expected. Thanks again for your help 🙂

Translate
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 ,
Jul 17, 2024 Jul 17, 2024
LATEST

Great! Hope you learned from the code, too. 🙂

Translate
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