Skip to main content
dublove
Legend
June 15, 2025
Question

How to solve: images don't update automatically after opening and modifying them in ps with a script

  • June 15, 2025
  • 2 replies
  • 701 views

Holle! m1b everyone

This script opens the image in Ps, but the image in the ID is not updated after the changes are saved. It's a bit of a workload to update hundreds of images manually.
Is there any way to solve it?

Thank you very much.

I came across this posting and it's a bit too long to read.
another post

/**
 *  Open Selected Links In Photoshop.js
 *
 * Opens the selected linked file(s) in Photoshop.
 *
 *  m1b
 *  2025-03-08
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-open-images-with-different-default-applications-in-indesign-and-resource-manager-windows/m-p/15197180
 */
(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 linked graphic frame and try again.');

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

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

    }

    if (0 === paths.length)
        return alert('Please select a linked graphic frame and try again.');

    // open the paths in photoshop
    openPathsInPhotoshop(paths);

})();

/**
 * 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("Photoshop is not available.");

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

};

 

2 replies

m1b
Community Expert
Community Expert
June 15, 2025

@dublove I would do as Uwe suggests, using relink.

 

For your learning, I have written a script based on my original "open in photoshop" script but now it opens, edits (adds "DEMO" text) saves as .psd, then relinks.

- Mark

 

/**
 * @file Edit Selected Graphics In Photoshop.js
 *
 * Does the following:
 *   1. opens the selected graphic(s) in Photoshop
 *   2. adds a "DEMO" watermark text
 *   3. saves them as psd files to the same folder
 *   4. back in Indesign, relinks to the new psd files.
 *
 * @author m1b
 * @version 2025-06-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-solve-images-don-t-update-automatically-after-opening-and-modifying-them-in-ps-with-a-script/m-p/15370844#M628200
 */
function main() {

    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 linked graphic frame and try again.');

    // collect paths for each selected item
    var sourcePaths = [],
        destinationPaths = [],
        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;

        sourcePaths.push(item.graphics[0].itemLink.filePath);
        destinationPaths.push(item.graphics[0].itemLink.filePath.replace(/\.[^\.]+$/, '_demo.psd'),);
        links.push(item.graphics[0].itemLink);

    }

    if (0 === sourcePaths.length)
        return alert('Please select a linked graphic frame and try again.');

    // open the paths in photoshop
    editImages(sourcePaths, destinationPaths, updateLinks(links));

    /**
     * Returns a callback function that updates `links`.
     * @param {Array<Link>} links - the links to update.
     */
    function updateLinks(links) {

        return function callback() {

            // update each link
            for (var i = 0; i < links.length; i++)
                links[i].relink(File(destinationPaths[i]));

        }; // end returned callback

    };

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

/**
 * Edits the graphics found at `sourcePaths` in Photoshop,
 * and saves them as layered psd documents.
 * @author m1b
 * @version 2025-06-15
 * @param {Array<String>} sourcePaths - the paths to open.
 * @param {Array<String>} destinationPaths - the paths to save into.
 */
function editImages(sourcePaths, destinationPaths, callback) {

    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 = '(#FUNC#)("#SRC#","#DST#","#DLM#");'
        .replace('#FUNC#', editImagesInPhotoshop)
        .replace('#SRC#', sourcePaths.join(DELIMITER))
        .replace('#DST#', destinationPaths.join(DELIMITER))
        .replace('#DLM#', DELIMITER);

    bt.onResult = callback;
    bt.send(1000);

};

/**
 * Photoshop function to open the `sourcePaths`, add a
 * demo watermark, and save them as layered .psd files.
 * Note: we cannot use // style comments here because
 * of the way I stringify it.
 * @author m1b
 * @version 2025-06-15
 * @param {String} sourcePaths - the paths, delimited.
 * @param {String} delimiter - the string used to delimit `paths`.
 */
function editImagesInPhotoshop(sourcePaths, destinationPaths, delimiter) {

    // app.bringToFront();

    /* make aray by splitting paths */
    sourcePaths = sourcePaths.split(delimiter);
    destinationPaths = destinationPaths.split(delimiter);

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

        if (!File(sourcePaths[i]).exists)
            continue;

        var doc = app.open(File(sourcePaths[i]));

        /* just for a demo, adds the word "demo" */
        demo(doc);

        /* save as psd */
        var psdSaveOptions = new PhotoshopSaveOptions();
        doc.saveAs(File(destinationPaths[i]), psdSaveOptions, true);
        doc.close(SaveOptions.DONOTSAVECHANGES);

    }

    /**
     * Add a centered "DEMO" text layer, sized to document width.
     * @param {Document} doc - a photoshop document.
     */
    function demo(doc) {

        var targetWidth = doc.width.as('px') * 0.9;

        var layer = doc.artLayers.add();
        layer.kind = LayerKind.TEXT;
        layer.opacity = 30;

        var text = layer.textItem;
        text.contents = 'DEMO';
        text.position = [doc.width.as('px') / 2, doc.height.as('px') / 2];
        text.justification = Justification.CENTER;
        text.size = 100;

        var widthA = layer.bounds[2] - layer.bounds[0];
        text.size = 1000;
        var widthB = layer.bounds[2] - layer.bounds[0];
        text.size = 10 + ((widthA - targetWidth) * (1000 - 10) / (widthA - widthB));
        text.position = [doc.width.as('px') / 2, (doc.height.as('px') + (layer.bounds[3] - layer.bounds[1])) / 2];

    };

};
dublove
dubloveAuthor
Legend
June 15, 2025

I tried to just add the following before the function openPath() in the script I posted:

From this
var linkURI = link.linkResourceURI;
an error sound is prompted.

There is no text message about the error.

 

var selectedFrame = app.selection[0];
var link = selectedFrame.images[0].itemLink;
var linkURI = link.linkResourceURI;
link.reinitLink(linkURI);
var done = false,
counter = 0;

var selectedFrame = app.selection[0];
var link = selectedFrame.images[0].itemLink;
while (!done && counter++ < 10) {
try {
$.sleep(1000);
if (LinkStatus.LINK_OUT_OF_DATE === link.status) {
link.update();
done = true;
};
} catch (error) { }
}

 

 

m1b
Community Expert
Community Expert
June 24, 2025

Hi  m1b~

Thanks so much.

I seem to be stuck in the mud again.

 

In ID, pressing Alt and double clicking to open the image really does update it automatically after modifying it in ps.
I guess it works like this: when the Indesign window on the taskbar is activated again, it updates the image that has just been modified.

 

It would also be very nice to keep the following dialog box from popping up.
Make it select "Yes" by default.

if (!confirm('Are you ready to update the selected graphics?'))
return;


Keep only the last prompt.

 

Also, is it possible to get the unchanged path so that the image will open without updating the link.


> It would also be very nice to keep the following dialog box from popping up. Make it select "Yes" by default.

 

This shows that you did not carefully read the order of operations. Without this confirm dialog pausing the script there is no point to check if the images need updating, because you won't have had time to edit them yet.

Community Expert
June 15, 2025

Hi @dublove ,

why do you not relink the placed image after changing it in PhotoShop?

 

Regards,
Uwe Laubender
( Adobe Community Expert )