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
for(var g = 0; g < selection
var item = selection
if(item instanceof TextFrame) {
for(var p = 0; p < item.paragraphs.length; ++p) {
if(item.paragraphs
.appliedParagraphStyle == "kvc") {
myObj.push(item)
}
}
}
}
}
}
alert(myObj.length)
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
Copy link to clipboard
Copied
You forgot to match name there
if(item.paragraphs
.appliedParagraphStyle.name.toString() == "kvc") {
Best
Sunil
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
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
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
Copy link to clipboard
Copied
this link idml and script
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
Copy link to clipboard
Copied
I unsuccessful
if you edit code I'm glad
Copy link to clipboard
Copied
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