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

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

Guide ,
Aug 14, 2025 Aug 14, 2025

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 }

1010.png

TOPICS
How to , Scripting
810
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 , Aug 14, 2025 Aug 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.selec
...
Translate
Community Expert , Aug 19, 2025 Aug 19, 2025

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, t

...
Translate
Community Expert ,
Aug 14, 2025 Aug 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) {}  
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 ,
Aug 14, 2025 Aug 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=

 

dublove_1-1755178965722.jpeg

 

 

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 ,
Aug 14, 2025 Aug 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) {} 
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 ,
Aug 14, 2025 Aug 14, 2025
quote

looks like there is some kind of corruption with your bottom 2 images?

 
By @rob day

 

There is no problem with those two pictures.
You can find two pictures and paste them inside the framework, and the test results will be the same.

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

There is no problem with those two pictures.

 

You have an image inside of a frame inside of another frame—a good example of why selections can return unexpected results:

 

Screen Shot 40.png

 

 

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

That's because I was in the same box before.
Replaced with another image.
I frequently paste images into the same box, and the old box is retained.
I couldn't find a better way to operate, so I had to manually delete the extra frames.

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

I frequently paste images into the same box

 

Use Paste Into.

 

This is why scripting selections is so diffucult—you can’t assume what is selected.

 

Screen Shot 41.png

 

Screen Shot 42.png

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

I used paste into.
I simply replaced the original image with the second image in the same box.
With this operation, the original image box may still be there.
I should delete the first image, along with its box, and then paste in the new image.

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 ,
Aug 15, 2025 Aug 15, 2025

Copy the image not the image and its frame--use the white arrow tool to direct select the image without its frame.

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 ,
Aug 19, 2025 Aug 19, 2025

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

 

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 ,
Aug 19, 2025 Aug 19, 2025

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.

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 ,
Aug 19, 2025 Aug 19, 2025

I tried to modify it based on the original code.
I tried many times.
alert(link.filePath); exists.
Why is this value still 0?
alert(“B:”+Rectangle.length);

 

(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.');

    // collect paths for each selected item
    var paths = [],
        links = [];

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

        var item = items[i];
        alert(item.constructor.name);
        // maybe user selected the image, not the frame
        if ('Image' === item.constructor.name) {
            item = item.parent;
            var link = item.graphics[0].itemLink;
            alert("A");
            alert(item);
            alert(link);
            alert(link.filePath);
            alert(item.graphics.length);

        }
        else if (('Rectangle' === item.constructor.name)) {
            item = item.allGraphics[0];
            var link = item.itemLink;
            alert("B");
            alert(item);
            alert(link);
            alert(link.filePath);
            alert("B:"+Rectangle.length);
            item = item
        }
})

 

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 ,
Aug 19, 2025 Aug 19, 2025

@dublove I have given you a function `getGraphics`. Use it to solve your confusion. It will always return an array of graphics.

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 ,
Aug 19, 2025 Aug 19, 2025

Hi ,m1b

I tested it, and the script above works fine. I'm thinking about how to add it to the original code.

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 ,
Aug 19, 2025 Aug 19, 2025

To add to your code above, dated 2025-06-16?

 

Simplest way to incorporate getGraphics function is

var selectedImages = getGraphics(app.selection);
if (0 === selectedImages.length)
    return alert('No images selected.');

// your code only works with the first image
var firstImageFile = File(selectedImages[0].itemLink.filePath);

 

But note that your code only handles the first selected image. If you want to handle them all, put the handling inside a for loop:

for (var i = 0; i < selectedImages.length; i++) {
   var imageFile = File(selectedImages[i].itemLink.filePath);

   // ... do something here

}

 

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 ,
Aug 19, 2025 Aug 19, 2025

Thank you.

I solved it last night, yes, I used a loop.

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 ,
Aug 19, 2025 Aug 19, 2025

Nice! Well done. 🙂

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 ,
Aug 20, 2025 Aug 20, 2025

The only regret at present is that span rows cannot be converted to table header rows.

My story can come to a temporary conclusion here.

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 ,
Aug 19, 2025 Aug 19, 2025

It worked, but there's one thing I don't get:

First:

alert(LinkStatus.LINK_OUT_OF_DATE);

Pop-up: LINK_OUT_OF_DATE
Why is the result below is False again?

alert(LinkStatus.LINK_OUT_OF_DATE == “LINK_OUT_OF_DATE”);

 

Second:
I am reminded of an old issue.

Strange, why does the image open in this way,  modify, and saved.

it will  can automatically update.

dublove_0-1755625658295.jpeg

 

 

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 ,
Aug 28, 2025 Aug 28, 2025

Hi @Laubender 

In this thread, we discovered that when opening images in Photoshop for editing via script, the images in the ID do not automatically update.
However, when opening images using the method shown below, they will update automatically.

Can the “menu operation” you mentioned in another post accomplish these steps?

When I select an image (the link window does not need to be displayed), it should open in the manner shown in the image below.

 

But there's one issue: the Photoshop version might be uncertain.

dublove_0-1755625658295.jpeg

 

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 ,
Aug 28, 2025 Aug 28, 2025

@dublove said: "… Can the “menu operation” you mentioned in another post accomplish these steps?"

Please post an URL of my post you refer to…

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

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 ,
Aug 28, 2025 Aug 28, 2025

Hi @Laubender 

Here.
Good night.

https://community.adobe.com/t5/indesign-discussions/brainstorming-with-script-how-to-transform-sprea...

app.menuActions.itemByName( "$ID/To Header" ).invoke();

 

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 ,
Aug 28, 2025 Aug 28, 2025

Ah. You mean if an menu action in general when visible from a menu can be invoked?

Well, when available and activated, yes. But this way of doing things is only reasonable if the action functions without a dialog popping up that one has to answer to get the action done.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

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 ,
Aug 29, 2025 Aug 29, 2025
LATEST

Having a Link window might be a bit of a hassle.
Can't you just use a script to execute the right-click?

dublove_0-1756482086248.jpeg

 

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