Skip to main content
dublove
Legend
August 14, 2025
Answered

How to obtain the path of an image inserted into a frame? I hope both selection tools work.

  • August 14, 2025
  • 1 reply
  • 1586 views

Using the Selection Tool and Direct Selection Tool
How do I obtain the path of an image pasted inside?

I tried using .parent, but it didn't work.

 var image = app.selection[0].parent;
var imagePath = image.itemLink.filePath;

 

The test file is here:

https://musetransfer.com/s/hlryv6cor 

 

    try {
        //alert(app.selection[0].constructor.name);
        if (app.selection[0].constructor.name == "Image") {
            //alert("here");
            var image = app.selection[0].parent;
            var imagePath = image.itemLink.filePath;
            //alert(imagePath);
        }
        else {
            var image = app.selection[0].images[0];
            var imagePath = image.itemLink.filePath;
            //alert(imagePath);
        }

    } catch (e) { return }

Correct answer m1b

Hi @rob day  @m1b 

With a slight change, I am stuck again.
How can I modify it so that I can open the image pasted inside the frame?
Whether using the selection tool or the direct selection tool.

Thank you very much.

/**
 *  author m1b
 *  2025-06-16
 *  discussion:
Current address:
https://community.adobe.com/t5/indesign-discussions/how-to-get-the-image-path-without-switching-the-selection-tool/m-p/15385688#M629189
**/
(function () {
    if (0 === app.documents.length)
        return alert('Please open a document and try again');
    var doc = app.activeDocument,
        items = doc.selection;
    if (0 === items.length)
        return alert('Please select a picture frame');
    var selectedFrame = app.selection[0];
    var imageFile = new File(selectedFrame.images[0].itemLink.filePath);
    if (!imageFile.exists)
        alert("Current file does not exist", 600);
    //update
    var selectedFrame = app.selection[0];
    var link = selectedFrame.images[0].itemLink;
    if (LinkStatus.LINK_OUT_OF_DATE === link.status)
        link.update();
    // collect paths for each selected item
    var paths = [],
        links = [];
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        // maybe user selected the image, not the frame
        if ('Image' === item.constructor.name)
            item = item.parent;
        if (
            !item.hasOwnProperty('graphics')
            || 0 === item.graphics.length
            || !item.graphics[0].itemLink
            || LinkStatus.NORMAL !== item.graphics[0].itemLink.status
        )
            // ignore anything we can't use
            continue;
        // first linked image is all we need right now
        paths.push(item.graphics[0].itemLink.filePath);
        links.push(item.graphics[0].itemLink);
    }
    if (0 === paths.length)
        return alert('Please select a linked graphic frame and try again.');
    // open the paths in photoshop
    openPathsInPhotoshop(paths);
    if (!confirm('Are you ready to update the selected graphics?'))
        return;
    var counter = 0;
    for (var i = links.length - 1; i >= 0; i--) {

        if (LinkStatus.LINK_OUT_OF_DATE !== links[i].status)
            continue;
        links[i].update();
        counter++;
    }
    alert('Updated ' + counter + ' graphics.');

})();
/**
 * Opens the given `paths` in Photoshop.
 *  m1b
 *  2025-03-08
 *  {Array<String>} paths - the paths to open.
 */
function openPathsInPhotoshop(paths) {
    const DELIMITER = '###';
    var photoshop = BridgeTalk.getSpecifier("photoshop");
    if (!photoshop)
        return alert("BridgeTalk couldn\'t find Photoshop.");
    // send script via BridgeTalk
    var bt = new BridgeTalk();
    bt.target = photoshop;
    // arrange the function inside an IIFE string,
    // and perform string replacements to insert
    // our variables
    bt.body = '(#FN#)("#P#","#D#");'
        .replace('#D#', DELIMITER)
        .replace('#P#', paths.join(DELIMITER))
        .replace('#FN#', openPaths);
    bt.onResult = undefined;
    bt.send(1000);
};
/**
 * Photoshop function to open the given `paths`.
 * Note: cannot use // style comments here because
 * of the way I stringify it.
 *  {String} paths - the paths, delimited.
 *  {String} delimiter - the string used to delimit `paths`.
 */
function openPaths(paths, delimiter) {
    app.bringToFront();
    /* make aray by splitting paths */
    paths = paths.split(delimiter);
    for (var i = 0; i < paths.length; i++)
        if (File(paths[i]).exists)
            app.open(File(paths[i]));
};

 


Hi @dublove you seem to be struggling with this idea of "select tool" vs "direct selection tool". This is a false idea because with the selection tool we can select the frame OR the graphic (by clicking on the circle widget in the frame center). Instead, try to have a better understanding of what you have selected.

 

For scripting, this is my approach: I make a function that does all the heavy lifting. I use it and never have to think twice about it (until I find a bug!).

 

Here is an example, the `getGraphics` function:

/**
 * @file Get Graphics.js
 *
 * @author m1b
 * @version 2025-08-20
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-obtain-the-path-of-an-image-inserted-into-a-frame-i-hope-both-selection-tools-work/m-p/15465583#M633874
 */
function main() {

    var doc = app.activeDocument;

    // collect the graphics from the selection
    var graphics = getGraphics(doc.selection);

    // get the paths of each graphic
    var paths = [];
    for (var i = 0; i < graphics.length; i++) {
        if (graphics[i].hasOwnProperty('itemLink'))
            paths.push(graphics[i].itemLink.filePath);
    }

    alert(
        'Selected graphics paths:\n'
        + paths.join('\n')
    );

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Get Graphics Demo');

/**
 * Returns graphics derived from `items`.
 * @author m1b
 * @version 2025-08-20
 * @param {Array|PageItem} items - array or collection or page item.
 */
function getGraphics(items) {

    var graphics = [];

    if ('Array' === items.constructor.name) {

        for (var i = 0; i < items.length; i++)
            graphics = graphics.concat(getGraphics(items[i]));

    }

    else if (
        items.hasOwnProperty('allGraphics')
        && items.allGraphics.length > 0
    )
        graphics = graphics.concat(items.allGraphics);

    else if (
        items.hasOwnProperty('allPageItems')
        && items.allPageItems.length > 0
    )
        graphics = graphics.concat(getGraphics(items.allPageItems));

    else if ('Image' === items.constructor.name) {
        graphics.push(items);
    }

    return graphics;

};

 

You should be able to feed those paths to my previous example function like this:

openPathsInPhotoshop(paths);

and it should open them all.

- Mark

 

Edit 2025-08-20: fixed typo in code.

1 reply

rob day
Community Expert
Community Expert
August 14, 2025

If the image is selected you don’t want its parent:

 

try {
    if (app.selection[0].constructor.name == "Image") {
        var image = app.activeDocument.selection[0];
        var imagePath = image.itemLink.filePath;
    }
    else {
        var image = app.activeDocument.selection[0].images[0];
        var imagePath = image.itemLink.filePath;
    }
    $.writeln(imagePath)
}catch(e) {}  
dublove
dubloveAuthor
Legend
August 14, 2025

Hi rob day.

Choosing these two images using the selection tool is invalid.

 

The meaning is that no matter how s changes,
The image and imagePath must be consistent.

var s=
var image=
var magePath=

 

 

 

rob day
Community Expert
Community Expert
August 14, 2025

Right, your if else statement trys to get the image inside of the Rectangle when it is selected.

 

Also, looks like there is some kind of corruption with your bottom 2 images—images[0] throws an error when the bottom image/frame is selected. Try allGraphics[0] — it works

 

try {
    if (app.selection[0].constructor.name == "Image") {
        var image = app.activeDocument.selection[0];
        var imagePath = image.itemLink.filePath;
    }
    else {
        var image = app.activeDocument.selection[0].allGraphics[0];
        var imagePath = image.itemLink.filePath;
    }
    $.writeln(imagePath)
}catch(e) {}