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

why count wrong

Participant ,
Jul 05, 2018 Jul 05, 2018

Copy link to clipboard

Copied

when I select a grouped object

I want to count kvc paragraph styles in text frame

but the result is wrong why

This code;

var myDoc = app.activeDocument;

var selection = app.activeDocument.selection;

var myObj = [];

     for ( j = 0; j<selection.length; ++j) {

           if(selection instanceof Group){

                for(var g = 0; g < selection.allPageItems.length; ++g) {

                          var item = selection.allPageItems;

                               if(item instanceof TextFrame) {

                                    for(var p = 0; p < item.paragraphs.length; ++p) {

                                                if(item.paragraphs

.appliedParagraphStyle == "kvc") {

                                                 myObj.push(item)

                           }

                        }

                    }

             }

        }

  }

alert(myObj.length)

TOPICS
Scripting

Views

872

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 1 Correct answer

Community Expert , Jul 06, 2018 Jul 06, 2018

Hi,

what's the result of your "counting"?

What is the number of paragraphs you'd expect?

Note:

If you are looking for an applied paragraph style always compare a paragraph style and not a string.

That's more save, even if from time to time your method is working.

Declare the paragraph style before you do your compare at the start of the script and check if it is valid.

var paraStyle = myDoc.paragraphStyles.itemByName("kvc");

if( !paraStyle.isValid ){ alert("Paragraph Style not found."); exit() }; // ins

...

Votes

Translate

Translate
Advocate ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

You forgot to match name there

if(item.paragraphs

.appliedParagraphStyle.name.toString() == "kvc") {

Best

Sunil

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 ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

Hi,

what's the result of your "counting"?

What is the number of paragraphs you'd expect?

Note:

If you are looking for an applied paragraph style always compare a paragraph style and not a string.

That's more save, even if from time to time your method is working.

Declare the paragraph style before you do your compare at the start of the script and check if it is valid.

var paraStyle = myDoc.paragraphStyles.itemByName("kvc");

if( !paraStyle.isValid ){ alert("Paragraph Style not found."); exit() }; // instead of exit() use return when running this inside a funtion!

Later in the script you could refer to variable paraStyle like this:

if( item.paragraphs

.appliedParagraphStyle == paraStyle )

{

myObj.push(item);

break;

}

Important Note:

You need break to end the inner loop at the moment the first paragraph is found.

Otherwise you'd push your frame more than one time to the array in case a second or a third paragraph is in the frame with the "kvc" applied.

Perhaps you meant that with "wrong count"?

Regards,
Uwe

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
Participant ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

4 text frames grouped

within these 3 text frames

have "kvc" paragraph style applied word

alert(item.paragraphs

.contents)

break;

3 alert ok

but

there are 2 of them

one of them is empty

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 ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

I can only guess, because you did not state if you want to add a text frame to your array that is empty and the first insertion point is formatted with paragraph style "kvc".

You should prepare for a case where no paragraphs are in a text frame.

Or where all text is overset.

Show a sample document, best upload one to a service like Dropbox and give us the link.

Regards,
Uwe

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
Participant ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

this link idml and script

WeTransfer

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 ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

Do you want to add a text frame to your array that is empty and the first insertion point is formatted with paragraph style "kvc"?

Regards,
Uwe

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
Participant ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

I unsuccessful

if you edit code I'm glad

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 ,
Jul 06, 2018 Jul 06, 2018

Copy link to clipboard

Copied

LATEST

Hi hamdifem ,

ok, let's be cautious. Instead of break use continue in the loop.

If that will not work or will work one time and not another time try to resolve all the paragraphs of a particular text frame by using getElements() with everyItem():

var currentParagraphs = item.paragraphs.everyItem().getElements();

and loop the array currentParagraphs where you test for the applied paragraph style.

If a text frame is empty that will enforce an error then.

So first test for item.paragraphs.length in the outer loop.

If length is 0, loop on with continue to the next text frame before doing everyItem().getElements().

Regards,
Uwe

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