Skip to main content
piligrimi
Participant
July 14, 2025
Answered

Anchoring an object to a 2-column text frame depending on which column the anchor is in

  • July 14, 2025
  • 1 reply
  • 707 views

Hello All,

 

I need to anchor an object to a 2-column text frame so that the object appears to the left of the left column or to the right of the right column depending on which column the anchor is in? And if the anchor is moved to a different column when editing the text, the anchored object moves to the correct location. Is it possible to do this using a single object style?

 

The image shows a sample layout that I want to get.

 

Thanks!

Correct answer Peter Kahrel

I see what you mean, I hadn't thought of that. Here's an updated script to deal with that. It's a bit verbose but it'll keep me sane.

 

Note that instead of your paragraph style names "left anchor" and "right anchor" I used 'anchor left align' and 'anchor right align', which make more sense.

 

(function () {

  var d = app.activeDocument;

  var leftAlign = d.paragraphStyles.item ('anchor left align');
  var rightAlign = d.paragraphStyles.item ('anchor right align');
  
  var insideStyle = d.objectStyles.item ('anchor inside');
  var outsideStyle = d.objectStyles.item ('anchor outside');

  //--------------------------------------------------------
  
  function positionFrame (frame) {
    
    var side = frame.parentPage.side === PageSideOptions.LEFT_HAND 
      ? 'left' : 'right';

    var column = frame.parent.textColumns[0] == frame.parent.parentTextFrames[0].textColumns[0] 
    ? 1 : 2;
    
    if (side === 'left' && column === 1) {
      frame.appliedObjectStyle = outsideStyle;
      frame.texts[0].appliedParagraphStyle = rightAlign;
      return;
    }
  
    if (side === 'left' && column === 2) {
      frame.appliedObjectStyle = insideStyle;
      frame.texts[0].appliedParagraphStyle = leftAlign;
      return;
    }
  
    if (side === 'right' && column === 1) {
      frame.appliedObjectStyle = insideStyle;
      frame.texts[0].appliedParagraphStyle = rightAlign;
      return;
    }
  
    if (side === 'right' && column === 2) {
      frame.appliedObjectStyle = outsideStyle;
      frame.texts[0].appliedParagraphStyle = leftAlign;
      return;
    }
  
  }

  //--------------------------------------------------------
  
  var frames = d.stories.everyItem().textFrames.everyItem().getElements();
  for (var i = 0; i < frames.length; i++) {
    positionFrame (frames[i]);
  }

}());

1 reply

Peter Kahrel
Community Expert
Community Expert
July 14, 2025

That's not possible. You'd have to create an object style for that places the anchors in the outside margins and apply that style to all anchored frames. Then create an object style that places frames in the inside margins. Then you'd need a script that applies that inside style to anchored frames in column 2 on versos and in column 1 on rectos.

 

Here's a script that does that. It assumes two object styles, 'anchor inside' and 'anchor outside'. You can use other names if you prefer. You need to run it every time when anchors may have moved to a different column.

 

(function () {

  var d = app.activeDocument;
  var insideStyle = d.objectStyles.item ('anchor inside');
  var outsideStyle = d.objectStyles.item ('anchor outside');

  function inside (frame) {
    
    if (frame.parentPage.side === PageSideOptions.LEFT_HAND
      && frame.parent.textColumns[0] == frame.parent.parentTextFrames[0].textColumns[1]) {
        return true;
    }

    if (frame.parentPage.side === PageSideOptions.RIGHT_HAND
      && frame.parent.textColumns[0] == frame.parent.parentTextFrames[0].textColumns[0]) {
        return true;
    }

    return false;
  }


  var frames = d.stories.everyItem().textFrames.everyItem().getElements();
  for (var i = 0; i < frames.length; i++) {
    if (inside (frames[i])) {
      frames[i].appliedObjectStyle = insideStyle;
    } else {
      frames[i].appliedObjectStyle = outsideStyle;
    }
  }

}());

 

 

 

Peter Spier
Community Expert
Community Expert
July 14, 2025

@Peter Kahrel I presume this would need to be re-run after any edits...

Peter Kahrel
Community Expert
Community Expert
July 15, 2025

Yep, and I mentioned that 🙂