Skip to main content
Lloydie J
Participating Frequently
May 17, 2023
Answered

Release All Anchored Objects at Once

  • May 17, 2023
  • 3 replies
  • 1302 views

Hello. I'm working on a publication that has hundrds of anchored images within the text. I need to release them from anchoring and place them where they are but not anchored. I found some scripts that worked on earlier versions of InDesign, but I'm using v18.2.1 on a Mac. I'm not sure if the structure of InDesign changed or I'm setting up the script incorrectly because I keep getting a syntax error when I try to run the scripts (I'm not a scripter so I might be using the scripts incorrectly).

 

Anu suggestions?

 

Thanks,

Lloyd

Correct answer m1b

Hi @Lloydie J, I wrote this just now and it seems to work okay (Indesign 18.3 MacOS).

- Mark

 

 

/**
 * Release All Anchored Objects.
 * @discussion https://community.adobe.com/t5/indesign-discussions/release-all-anchored-objects-at-once/m-p/13797933#M527123
 */
function main() {

    var doc = app.activeDocument,
        items = doc.allPageItems,
        counter = 0,
        oversetCounter = 0;

    for (var i = items.length - 1; i >= 0; i--) {

        if (
            items[i].parent.constructor.name == 'Character'
            && items[i].hasOwnProperty('anchoredObjectSettings')
        ) {
            if (items[i].parent.parentTextFrames.length > 0) {
                items[i].anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
                items[i].anchoredObjectSettings.releaseAnchoredObject();
                counter++;
            }

            else {
                oversetCounter++
            }
        }

    };

    alert('Released ' + counter + ' anchored objects.' + (oversetCounter > 0 ? ' Skipped ' + oversetCounter + ' overset objects.' : ''));

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Release All Anchored Objects');

 

Edit 2023-05-19: added check for overset text (thanks @Laubender!) and also a counter, because I like to know what happened when I run a script. 🙂

3 replies

Community Expert
May 18, 2023

Hi @Lloydie J ,

you said:

"I'm not sure if the structure of InDesign changed or I'm setting up the script incorrectly because I keep getting a syntax error when I try to run the scripts (I'm not a scripter so I might be using the scripts incorrectly)."

 

It's highly unlikely that the failure of the script has to do with a change in that particular version of InDesign.

The document object model's methods and properties that have to do with releasing anchored objects did not change since InDesign CS5.5 version 7.5 that was released about 12 years ago. For example, the parent of an anchored object is still a character. Still one could gather all anchored objects of a particular story with story.allPageItems.

 

What could be different is the situation the script is facing inside a particular document.

That the script is not written to handle a particular situation. A case the author of the script did not think about.

Perhaps there is overset text that contains anchored objects. Maybe other cases as well...

 

FWIW: Releasing anchored objects could be done in two or more ways.

[1] Use method releaseAnchoredObject(). A method that is not working with inline or above line anchored objects. Hm. But in both cases no error is thrown; it just does not work, the anchored object will still be anchored after running the code. Even if you try use the method of on anchored objects, not inline or above line anchored, inside overset text, no error is thrown when the method fails.

 

[2] Duplicate the anchored object with method duplicate(); remove the anchored original one and work on with the duplicate. However, trying to duplicate an anchored object that is inside overset text will literally crash InDesign without any error message.

 

Without seeing the script's code plus having the document at hand I cannot tell what's going wrong.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
May 18, 2023

I said: "Still one could gather all anchored objects of a particular story with story.allPageItems."

Ok. That was a bit sloppy. Still one has to check what the parent of every object is in that resulting array of items.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
May 18, 2023

Hi @Lloydie J, I wrote this just now and it seems to work okay (Indesign 18.3 MacOS).

- Mark

 

 

/**
 * Release All Anchored Objects.
 * @discussion https://community.adobe.com/t5/indesign-discussions/release-all-anchored-objects-at-once/m-p/13797933#M527123
 */
function main() {

    var doc = app.activeDocument,
        items = doc.allPageItems,
        counter = 0,
        oversetCounter = 0;

    for (var i = items.length - 1; i >= 0; i--) {

        if (
            items[i].parent.constructor.name == 'Character'
            && items[i].hasOwnProperty('anchoredObjectSettings')
        ) {
            if (items[i].parent.parentTextFrames.length > 0) {
                items[i].anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
                items[i].anchoredObjectSettings.releaseAnchoredObject();
                counter++;
            }

            else {
                oversetCounter++
            }
        }

    };

    alert('Released ' + counter + ' anchored objects.' + (oversetCounter > 0 ? ' Skipped ' + oversetCounter + ' overset objects.' : ''));

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Release All Anchored Objects');

 

Edit 2023-05-19: added check for overset text (thanks @Laubender!) and also a counter, because I like to know what happened when I run a script. 🙂

Lloydie J
Lloydie JAuthor
Participating Frequently
May 20, 2023

Perfect. It took me a few minutes but I got this to work. I appreciate your help!

Peter Spier
Community Expert
Community Expert
May 17, 2023

Try putting the script into a subfolder of the Scripts folder named "Version 17.0 Scripts" (without the quote marks, but it is case sensitive).