Copy link to clipboard
Copied
I have found all images in the file:
var images = app.activeDocument.stories[0].allGraphics;
Now I want to cycle through them and do some stuff including possibly moving them to the next or previous paragraph. I know the parent of an image is a Rectangle and the parent of the Rectangle is a character. Right now I have:
for (i=0; i<number_of_images; i++)
{
image = images.parent;
myPar=image.parent.paragraphs[0];
}
This gives me the actual paragraph the image is in, but I need to know the paragraphs index within the document (or story). How can I get that?
Copy link to clipboard
Copied
Hi,
I you want to find out the next and previous paragraph, you can find using the below method.
var images = app.activeDocument.stories[0].allGraphics;
for (i=0; i<images.length; i++)
{
image = images.parent;
myPar=image.parent.paragraphs[0];
myPar.select()
alert("Currecnt Para" + " - "+ myPar.index)
var nxtPara = myPar.paragraphs.nextItem(myPar);
nxtPara.select()
alert("NEXT PARA :: " + app.selection[0].contents)
var prevPara = myPar.paragraphs.previousItem(myPar);
prevPara.select()
alert("Previous Para :: "+ app.selection[0].contents)
}
Copy link to clipboard
Copied
Hi Daniel,
allGraphics is tricky, but I'm glad it does exist!
You say: "The parent of the image is a rectangle and the parent of that rectangle is a character."
Test always for that, because allGraphics will contain all graphics independently how much they are nested in a structure like groups etc.pp.
Consider a table where an image is anchored to:
A. Insertion point of a text cell
B. Is part of a graphic cell
Or the container frame of the image is:
a. Part of a group that is anchored
b. Pasted inside a graphic frame that is anchored
c. Is part of a Button object
d. Is part of the active state* of an anchored Multistate Object (MSO)
e. …
In all the cases above story.allGraphics will contain the images.
* Not active states are not considered!
So the length of allGraphics could change depending which state of an anchored MSO is active!
Regrads,
Uwe
Copy link to clipboard
Copied
danielg98770948 wrote
…This gives me the actual paragraph the image is in, but I need to know the paragraphs index within the document (or story). How can I get that?
Let's break this task down to:
A character and let's ask: What is the paragraph that character is in ?
The answer:
myAnchorCharacter.paragraphs[0]
If that answers the question, good.
You now have direct access to that paragraph.
If you want to know which index that paragraph has inside its parent story, you have some options.
1. You could loop all the paragraphs in the story and try to find identity with the paragraph of that character.
If my character's paragraph is paragraphs
2. You could look for paragraph.index. But that will give you the index of the first character of the paragraph in scope of its story. Not the index value you hoped for. Just consider the following line that will return true:
// Anchored rectangle selected:
app.selection[0].parent.paragraphs[0].characters[0].index == app.selection[0].parent.paragraphs[0].index
But we could turn that to our advantage and loop all paragraphs of the story and compare every single index value of every paragraph with the index value of our found paragraph. And then store the value of our iteration number:
// Anchored rectangle selected:
var rect = app.selection[0];
var anchorChar = rect.parent;
var story = anchorChar.parentStory;
var allParagraphsInStory = story.paragraphs.everyItem().getElements();
var paraLength = allParagraphsInStory.length;
var paragraphOfAnchor = anchorChar.paragraphs[0];
var indexOfFoundParagraph = paragraphOfAnchor.index;
var allIndexNumbers = story.paragraphs.everyItem().index;
var allIndexLength = allIndexNumbers.length;
var result;
for( var n=0; n< allIndexLength; n++ )
{
if( indexOfFoundParagraph == allIndexNumbers
) {
result = n ;
break
}
};
story.paragraphs[ result ].fillColor = app.documents[0].colors.itemByName("Magenta");
Regards,
Uwe
Find more inspiration, events, and resources on the new Adobe Community
Explore Now