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
  • 687 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

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;
    }
  }

}());

 

 

 

piligrimi
piligrimiAuthor
Participant
July 15, 2025

Thanks @Peter Kahrel

 

I also have this feature - in anchors that are on the left (on even and odd page), the text should be formatted on the right side, and in anchors on the right side - the text on the left side. Therefore, the internal anchors on the right and left pages have different styles.
I made the styles "left anchor" and "right anchor" with the desired text style in them, and I apply them manually.

Peter KahrelCommunity ExpertCorrect answer
Community Expert
July 15, 2025

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]);
  }

}());