Copy link to clipboard
Copied
Hi folks,
Something that bothers me for quite some is when I have a collection of say PageItems and I want to for example resize the image that a PageItem may contain, do I really need to check first if that PageItem is a rectangle? Since "textFrames.images[0]" isn't allowed, I always have to make an extra if-statement:
myPageItems = app.activeDocument.allPageItems;
if (myPageItems instanceof Rectangle)
{
if (myPageItems.images.length == 1)
{
further code
Is there a way how to check if the PageItem holds an image without verifying the PageItem first? I know that I can just check for images and use images[0].parent to get to the Rectangle, but I feel that I'm not knowing a very basic method of checking the right way.
Thanks!
> I was just wondering if there is a way to avoid a 2nd if-statement.
if (myPageItems instanceof Rectangle && myPageItems.images.length == 1)
JavaScript uses a so-called short-circuit evaluation of coordinated if-statements: if the first test fails the whole statement fails. So in this case, if myPageItems is not a rectangle, the script doesn't do the second text.
Peter
Copy link to clipboard
Copied
Hi,
if you want to have access to all graphics (or their parents), simply loop the allGraphics array.
The scope of allGraphics could be e.g. the document, a spread, a page, a story, a text frame etc.pp.
Regards,
Uwe
Copy link to clipboard
Copied
If you loop allPageItems—also an array, no collection—you could do this:
var allPageItems = app.documents[0].allPageItems;
for(var n=0;n<allPageItems.length;n++)
{
if(allPageItems
.hasOwnProperty("graphics")) {
if(allPageItems
.graphics.length == 1) {
// Do something:
}
}
}
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
Thanks for the input. I know how to access the image, I was just wondering if there is a way to avoid a 2nd if-statement.
Thanks,
Frank
Copy link to clipboard
Copied
> I was just wondering if there is a way to avoid a 2nd if-statement.
if (myPageItems instanceof Rectangle && myPageItems.images.length == 1)
JavaScript uses a so-called short-circuit evaluation of coordinated if-statements: if the first test fails the whole statement fails. So in this case, if myPageItems is not a rectangle, the script doesn't do the second text.
Peter
Copy link to clipboard
Copied
Hi Peter,
already tried something like this, but it failed with an error message from my German ESTK on one of my test documents:
"Objekt unterstützt Eigenschaft oder Methode images nicht"
The speciality here:
All images in the document were anchored or nested in anchored structures.
There were no images that were not anchored.
Tried it again after changing the document slightly:
This time with a single image not anchored (all others were still anchored):
Your if statement with && worked as expected!
Hm. How strange is that?
Tested with InDesign CS6 v8.1.0 on Mac OSX 10.6.8.
Here my code:
var allPageItems = app.documents[0].allPageItems;
for(var n=0;n<allPageItems.length;n++)
{
//~ if(allPageItems
.hasOwnProperty("graphics")) //~ {
//~ if(allPageItems
.graphics.length == 1) //~ {
//~ // Do something:
//~ }
//~ }
try{
if (allPageItems
instanceof Rectangle && allPageItems.images.length == 1) {
$.writeln(n+"\t"+allPageItems
); }
}catch(e){$.writeln(n+"\t"+e.message)};
};
Regards,
Uwe
Copy link to clipboard
Copied
> How strange is that?
Very strange! If you're a Rectangle, you have the images property. Should always work.
> Tried it again after changing the document slightly . . . and && worked as expected
What did you change?
Copy link to clipboard
Copied
Thank you so much guys!
I had tried this before, Peter, and thought it didn't work which is also in line with that I learned from another language. Didn't know about the short-circuit evaluation in JavaScript. I'll pay attention to the order now and test it again. Would be very useful to shorten code – thanks!
Uwe, maybe that's the reason why it didn't work for me, since I'm often working with anchored objects throughout the document. Interesting findings in any case.
Frank
Copy link to clipboard
Copied
So yeah, works like a charm – thanks again!
Copy link to clipboard
Copied
Just placed a tiff image on the page.
Did not place it to an insertion point…
Regards,
Uwe
Copy link to clipboard
Copied
pkahrel wrote:
> How strange is that?
Very strange! If you're a Rectangle, you have the images property. Should always work.
> Tried it again after changing the document slightly . . . and && worked as expected
What did you change?
Hi Peter,
here my test documents as IDML zipped:
Dropbox - FindPageItemsWithImages-Example-doc-1-2.zip
FindPageItemsWithImages-Example-doc-1-2.zip
Images all anchored:
FindPageItemsWithImages-Example-doc-1-CS6.idml
Images all anchored but one:
FindPageItemsWithImages-Example-doc-2-CS6.idml
Regards,
Uwe