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

How to find out which paragraph of the document an image is in?

New Here ,
Apr 04, 2019 Apr 04, 2019

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?

TOPICS
Scripting
697
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 ,
Apr 04, 2019 Apr 04, 2019

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)  

}

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 ,
Apr 04, 2019 Apr 04, 2019

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

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 ,
Apr 05, 2019 Apr 05, 2019
LATEST

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 of the story, store the value of n in a variable.

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

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