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

GREP : searching for a specific anchored object

Explorer ,
Oct 13, 2025 Oct 13, 2025

Hello, do you know if it is possible to search for a specific anchored object? I have a text in which several icons are inserted, but one of them is higher than the others. I would like to apply a vertical offset to it... but only to that one. Thank you for your help! InDesign2024

TOPICS
Experiment , How to , Scripting
1.2K
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

correct answers 2 Correct answers

Community Expert , Oct 13, 2025 Oct 13, 2025

Hi @99drine, Here is an example script, that matches anchored items to the selected anchored item, and applies a -2 baseline shift to each. You can adjust this to suit your needs. Let me know if it's useful!

 

@Eugene Tyson, @brian_p_dts  for cases like this—matching an object—I often create my own "stringify" function customized just for that purpose. Here I compare lengths of paths and pathpoints, which in OP's case should be enough.

- Mark

 

demo.gif

 

/**
 * @file Adjust Matching Anchored Items.js
 
...
Translate
Guide , Oct 14, 2025 Oct 14, 2025

Simplistic Script for a simplistic matter:

 

Select the icon you want to correct (here, I just want to modify the "Décalage sur Y" (in french) :

 

Capture d’écran 2025-10-14 à 13.52.51.png

 

and play this Script:

 

var myDoc = app.activeDocument;

// Select An Icon With The Right Inline AnchorYoffset You Want:
var mySel = app.selection[0];
// Various Checks:
var mySelInline = mySel.anchoredObjectSettings.anchoredPosition;
var mySelHeight = (mySel.geometricBounds[2]-mySel.geometricBounds[0]).toFixed(10);
var mySelAnchorYOffset = mySel.anchoredO
...
Translate
Community Expert ,
Oct 13, 2025 Oct 13, 2025

Perhaps this can be scripted but you can’t do that with GREP. You can search for an anchored object using ~a but that will fint any anchored object. There is no way using GREP to speciy a specific object.

 

Perhaps you can apply an object style to each instance then modify the object style.

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
Explorer ,
Oct 13, 2025 Oct 13, 2025

Thank you very much for your prompt reply! Knowing that this option is not possible will save me a lot of time! I have attached an illustrated example. If you can see a possible solution to my problem, I would be delighted to hear it! Thank you again!

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 ,
Oct 13, 2025 Oct 13, 2025

@Scott Falkner is correct

 

If you give us a sample of your document with what you're trying to do and see if a script can work for you. 

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
Explorer ,
Oct 13, 2025 Oct 13, 2025

Thank you very much for your prompt reply and your help! Do you mean an idml file ?

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 ,
Oct 13, 2025 Oct 13, 2025

Literally just occured to me - the anchored objects are just text like anything else, which can be found with ~a

 

So if you need to find the gloves, the 3rd item in - then you can 
(~a.*?){2}\K~a

 

Considering if all the icons are the 3rd one in the paragraph and all need the same offset

 

You can then change the Change settings to offset the baseline and move it down. 

 

 

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
Explorer ,
Oct 13, 2025 Oct 13, 2025

Unfortunately, gloves are not always in third position, but for all the cases where they are, it will already save me a lot of time! A huge thank you!!!

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 ,
Oct 13, 2025 Oct 13, 2025

Are the icons placed graphics, or built natively in InDesign (or other CC app)?

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
Explorer ,
Oct 13, 2025 Oct 13, 2025

Thank you for your help! English is not my native language, so I'm not sure I fully understand your question or know the correct terms, but I've attached a screenshot that might answer your question. The block does not contain an imported image; I think these are lines created in InDesign or Illustrator...

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 ,
Oct 13, 2025 Oct 13, 2025

As they're drawn in InDesign with a Path and not linked, I can't see a way to find them. 

 

If they were linked through the Links panel it might be possible. 

 

Where the gloves are - are there any common text that would be searchable to find before or after the gloves - if there's common text it can probably be found. 

 

 

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
Explorer ,
Oct 13, 2025 Oct 13, 2025

Unfortunately, there is no recurring context I can use to find the icon... so I will use your great formula and do the rest by hand, consoling myself with the idea that sometimes it takes a human being to do the work manually...! Thank you again for your help and your excellent idea (I'll keep it in mind the next time I'm thinking about how to solve a problem!). Thank you!

 

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 ,
Oct 13, 2025 Oct 13, 2025

Hi @99drine, Here is an example script, that matches anchored items to the selected anchored item, and applies a -2 baseline shift to each. You can adjust this to suit your needs. Let me know if it's useful!

 

@Eugene Tyson, @brian_p_dts  for cases like this—matching an object—I often create my own "stringify" function customized just for that purpose. Here I compare lengths of paths and pathpoints, which in OP's case should be enough.

- Mark

 

demo.gif

 

/**
 * @file Adjust Matching Anchored Items.js
 *
 * Usage:
 *   1. Edit script to suit, especially the `adjuster` function.
 *   2. In Indesign, select an anchored item.
 *   3. Run Script.
 *
 * Script will apply the `adjuster` function to each matching anchored item.
 *
 * @author m1b
 * @version 2025-10-14
 * @discussion https://community.adobe.com/t5/indesign-discussions/grep-searching-for-a-specific-anchored-object/m-p/15544328
 */
function main() {

    /**
     * Makes an adjustment to an item.
     * @param {PageItem} item - the item to adjust.
     * @returns {Boolean} - return true if you want to collect this item (optional).
     */
    function adjuster(item) {

        // edit this to suit your needs
        item.baselineShift = -2;

        // this is just for reporting purposes
        counter++;

    };

    var doc = app.activeDocument;
    var anchoredItemCharacter = doc.selection[0];

    if (
        anchoredItemCharacter
        && 'Character' !== anchoredItemCharacter.constructor.name
        && 'Character' === anchoredItemCharacter.parent.constructor.name
    )
        // we want the character, not the item itself
        anchoredItemCharacter = anchoredItemCharacter.parent;

    if (
        !anchoredItemCharacter
        || 'Character' !== anchoredItemCharacter.constructor.name
    )
        return alert('Please select an anchored item and try again.');

    // find all anchored items in document
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.findWhat = '~a';
    var allAnchoredItems = doc.findGrep();

    var counter = 0;

    // perform the matching and adjusting
    matchItems(anchoredItemCharacter, allAnchoredItems, adjuster);

    alert('Adjusted ' + counter + ' anchored items.');

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Adjust Matching Anchored Items');

/**
 * Matches `items` with `matchMe` using a custom stringify function and applies a filter function
 * @author m1b
 * @version 2025-10-14
 * @param {PageItem} matchMe - the item to match.
 * @param {Array<PageItem>} items - the items to search through.
 * @param {Function} [filter] - a filter function (default: no filter).
 * @returns {Array<PageItems>} - the matched (and filtered) items.
 */
function matchItems(matchMe, items, filter) {

    var id = stringify(matchMe);
    var matching = [];

    for (var i = 0; i < items.length; i++) {

        if (
            stringify(items[i]) === id
            && (!filter || filter(items[i]))
        )
            matching.push(items[i]);

    }

    return matching;

};

/**
 * Generate a string just for the purpose
 * of identifying a particular item.
 * @author m1b
 * @version 2025-10-14
 * @param {*} thing - an Indesign DOM thing.
 * @returns {String}
 */
function stringify(thing) {

    var str = '';

    addStringFor('contents', undefined, 40);
    addStringFor('length');
    addStringFor('itemLink|filePath');
    addChildrenOf('allPageItems');
    addChildrenOf('paths');
    addChildrenOf('pathPoints');
    addChildrenOf('graphics');

    return str;

    /** Stringifies `key` property. */
    function addStringFor(key, otherThing, len) {

        var _str;
        var target = otherThing || thing;
        var keys = key.split('|');
        key = keys.shift();

        if (
            target.hasOwnProperty(key)
            && 'undefined' !== typeof target[key]
        ) {
            _str = String(target[key]);

            if (len)
                _str = _str.slice(0, len);

            str += _str;

            if (keys.length)
                addStringFor(keys.join('|'), target[key]);

        }

    };

    /** Stringifies each element of `key` property. */
    function addChildrenOf(key) {

        if (
            !thing.hasOwnProperty(key)
            || 0 === thing[key].length
        )
            return;

        str += key + thing[key].length;

        for (var i = 0; i < thing[key].length; i++)
            str += stringify(thing[key][i]);

    };

};

 

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 ,
Oct 13, 2025 Oct 13, 2025

By the way, this will do the same thing, but instead of baseline shift, it uses anchored object settings. This might be a cleaner approach depending on whether you prefer to break your text style or your object style, if any. 🙂

function adjuster(item) {

    // edit this to suit your needs
    item.allPageItems[0].anchoredObjectSettings.anchorYoffset = '-2pts';

    // this is just for reporting purposes
    counter++;

};
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 ,
Oct 13, 2025 Oct 13, 2025

That's incredible, I'm gonna save this one. Never knew you could do this... interesting.

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 ,
Oct 13, 2025 Oct 13, 2025

Oh and one more way—@Scott Falkner's idea—applying an object style. But this would mean that all the inline objects shared the same style, which won't be the case here because the gloves need adjusting differently. But you could do this:

function adjuster(item) {

    // edit this to suit your needs
    item.allPageItems[0].appliedObjectStyle = inlineIconStyle;
    item.allPageItems[0].anchoredObjectSettings.anchorYoffset = '-2pts';

    // this is just for reporting purposes
    counter++;

};

var doc = app.activeDocument;
var inlineIconStyle = doc.objectStyles.itemByName('Inline Icons');
var anchoredItemCharacter = doc.selection[0];

 This would apply the style, then override just the Y offset. Note that you must create the `inlineIconStyle` variable in scope of the `adjuster` function.

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
Guide ,
Oct 14, 2025 Oct 14, 2025

Simplistic Script for a simplistic matter:

 

Select the icon you want to correct (here, I just want to modify the "Décalage sur Y" (in french) :

 

Capture d’écran 2025-10-14 à 13.52.51.png

 

and play this Script:

 

var myDoc = app.activeDocument;

// Select An Icon With The Right Inline AnchorYoffset You Want:
var mySel = app.selection[0];
// Various Checks:
var mySelInline = mySel.anchoredObjectSettings.anchoredPosition;
var mySelHeight = (mySel.geometricBounds[2]-mySel.geometricBounds[0]).toFixed(10);
var mySelAnchorYOffset = mySel.anchoredObjectSettings.anchorYoffset;
// Loop:
app.findGrepPreferences = app.findChangeGrepOptions = null;
app.findGrepPreferences.findWhat = '~a';
var myFound = myDoc.findGrep();
for ( var i = 0; i < myFound.length; i++ ) if ( (myFound[i].pageItems[0].anchoredObjectSettings.anchoredPosition === mySelInline) && ((myFound[i].pageItems[0].geometricBounds[2]-myFound[i].pageItems[0].geometricBounds[0]).toFixed(10) === mySelHeight) ) myFound[i].pageItems[0].anchoredObjectSettings.anchorYoffset = mySelAnchorYOffset;
app.findGrepPreferences = app.findChangeGrepOptions = null;

alert("Done! …")

 

(^/)  The Jedi 

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 ,
Oct 14, 2025 Oct 14, 2025

@FRIdNGE that's a great idea using the Y offset value *that already exists*, which saves the user fiddling with the script. I didn't use the width and height for my matching out of habit, because I always make icons in a same size bounding box so it would match with all of them. But I suspect in this case it would work fine because there are no other icons that are the same size.

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
Explorer ,
Oct 14, 2025 Oct 14, 2025

Oh ! Wow! It worked perfectly!!! A huge thank you!… and my utmost admiration!!

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 ,
Oct 14, 2025 Oct 14, 2025
LATEST

@99drine nice to hear! 🙂

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