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

How to move select anchored objects

Explorer ,
Apr 29, 2019 Apr 29, 2019

Copy link to clipboard

Copied

I have a script (bottom) that I created to move the anchored object insertion points to after the first tab. It works as intended. Here are example lines before and after running the script—(~a1,2,3) would be the anchored objects, (\\t) is a tab.

Before: (~a1) foo (\\t) bar (~a2) fizzbuzz (~a3).

After: foo (\\t) (~a1) (~a2) (~a3) bar fizzbuzz.

However, occasionally there are anchored objects at the beginning of lines that i would like to omit from being moved. Here is the desired format, having moved only the anchors after the first anchored object occurrence.

Desired: (~a1) foo (\\t) (~a2) (~a3) bar fizzbuzz.

My question is how to isolate only the text and children thereof after the first occurrence of an anchored object? So in other words how can I skip the first anchored object and still move the rest?

Thanks in advance for your time.

var myanchors = app.documents[0].stories.everyItem().pageItems.everyItem().getElements();

for ( x=0; x<myanchors.length;x++)

     {

        var thistext = myanchors.parent.paragraphs.item(0).contents;

        var tablocation=thistext.indexOf('\t');

        var myIP=myanchors.parent.paragraphs.item(0).insertionPoints.item(tablocation+1) ;

        myanchors.anchoredObjectSettings.insertAnchoredObject(myIP, AnchorPosition.INLINE_POSITION);

        }

TOPICS
Scripting

Views

1.1K

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

Hi spicyDoge ,

some infos that could help:

the parent of a pageItem in a text is a character.

For characters (or other texts ) we have method move().

What does "occassionally" eaxctly mean?

If you do not want to move all anchor characters that are the first character in a line (?) or in a paragraph (?) what are the exact criteria to not move one, but the other?

Here a little sample where I test for first character in a line ( not in a paragraph ).

Selected text frame with some anchored objects. I want

...

Votes

Translate

Translate
Community Expert ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

Hi spicyDoge ,

some infos that could help:

the parent of a pageItem in a text is a character.

For characters (or other texts ) we have method move().

What does "occassionally" eaxctly mean?

If you do not want to move all anchor characters that are the first character in a line (?) or in a paragraph (?) what are the exact criteria to not move one, but the other?

Here a little sample where I test for first character in a line ( not in a paragraph ).

Selected text frame with some anchored objects. I want to give the one that is not first in a text line the fillColor "Magenta":

AnchoredObjectBeforeTestRun.PNG

Code snippet that is working on the story of the selected text frame:

var story = app.selection[0].parentStory;

var anchoredItems = story.pageItems.everyItem().getElements();

var testColor = app.documents[0].colors.itemByName( "Magenta" );

for( var n=0; n<anchoredItems.length; n++ )

{

    // If anchored object is first in a text line, do nothing:

    if( anchoredItems.parent == anchoredItems.parent.lines[0].characters[0] ){ continue };

  

    anchoredItems.fillColor = testColor;

};

After the script run:

AnchoredObjectsAfterTestRun.PNG

Method move() of character documented:

Adobe InDesign CS6 (8.0) Object Model JS: Character

Regards,
Uwe

// SOME EDITS, DID NOT HAD MY MORNING COFFEE YET

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
Explorer ,
Apr 30, 2019 Apr 30, 2019

Copy link to clipboard

Copied

thanks one million times for this reply, it was exactly what i couldn't figure out regarding how to skip the first occurrence of the anchor. i appreciate this immensely.

I've changed my code to reflect your suggestions and it works as desired for now.

var anchoredItems = app.documents[0].stories.everyItem().pageItems.everyItem().getElements();

for( var n=0; n<anchoredItems.length; n++ ) 

// If anchored object is first in a text line, do nothing: 

if( anchoredItems.parent == anchoredItems.parent.lines[0].characters[0] ){ continue }; 

//get contents of paragraph - cant use just lines because the text is multiline

var thistext = anchoredItems.parent.paragraphs.item(0).contents;

//grab my index location for insertion

var tablocation=thistext.indexOf('\t');

//set up insertion

var myIP=anchoredItems.parent.paragraphs.item(0).insertionPoints.item(tablocation+1) ; 

//move anchor

anchoredItems.anchoredObjectSettings.insertAnchoredObject(myIP, AnchorPosition.INLINE_POSITION);

   }; 

However i now have another question, which is: is there a better way to move the anchors? You mentioned the move() method but the anchors I deal with are all in-line; move() requires 2 coordinate inputs if I remember correctly.

You mentioned the anchor resides as a child of the (Â¥) character in the line? I believe I've moved it previously but once it moves, say via grep, the anchored object, in my case a rectangle object, fails to retain the image placed inside.

Whats the best way to handle moving where the anchored object is inserted at while still retaining the contents inside? The way I have it above seems to work fine, but I've been doing this long enough to know there's got to be a better way.

As always, thanks in advance for your time, it is greatly appreciated.

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

Copy link to clipboard

Copied

LATEST

spicyDoge  wrote

…However i now have another question, which is: is there a better way to move the anchors? You mentioned the move() method but the anchors I deal with are all in-line; move() requires 2 coordinate inputs if I remember correctly. …

Hi,

simply look into the link to method move() for a character ( or text ).

There are other arguments required.

A location option as first argument and e.g. a text as second argument.

Since text covers also insertion points you could move() a character before or after an insertion point.

Could be the same story, could be a different story, a text celll or a footnote text. Doesn't matter.

You could even target a story in a different open document.

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