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

Extend script not auto refreshing the missing link.

Engaged ,
Mar 15, 2024 Mar 15, 2024

I made a script that takes the selected image in indesign, sends it to photoshop, uses the select subject command, masks it and saves it as a tiff with transparancy. This is just for rough cut outs and it speed up my workflow quite a bit but I'm running into one snag. Indesign isn't updating the link. I tried onResult with a delay but thats not working. I end up having to refresh the link manually which isn't the end of the world but it would be nice to not have to do that. Any one know how to get that working?

 

if (app.documents.length > 0 && app.selection.length > 0 && app.selection[0].constructor.name == "Rectangle") {
    var selectedFrame = app.selection[0];

    if (selectedFrame.images.length > 0) {
        var imageFile = new File(selectedFrame.images[0].itemLink.filePath);

        if (imageFile.exists) {
            app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

            var photoshop = BridgeTalk.getSpecifier("photoshop");
            if (photoshop) {

                var openScript = "var openFile = new File(\"" + imageFile.fsName + "\");\r";
                openScript += "app.open(openFile);\r";

                var photoshopScript = "if (app.documents.length > 0) {\r";
                photoshopScript += "    var docRef = app.activeDocument;\r";

                // Select Subject
                photoshopScript += "    try {\r";
                photoshopScript += "        var desc = new ActionDescriptor();\r";
                photoshopScript += "        executeAction(stringIDToTypeID('autoCutout'), desc, DialogModes.NO);\r";
                photoshopScript += "    } catch (e) {\r";
                photoshopScript += "        alert(\"Error during autoCutout: \" + e.message);\r";
                photoshopScript += "    }\r";

                // Mask
                photoshopScript += "maskSelection(\"revealSelection\");\r\n";
                photoshopScript += "function maskSelection(maskParameter) {\r";
                photoshopScript += "    var s2t = function (s) { return app.stringIDToTypeID(s); };\r";
                photoshopScript += "    var descriptor = new ActionDescriptor();\r";
                photoshopScript += "    var reference = new ActionReference();\r";
                photoshopScript += "    descriptor.putClass( s2t( \"new\" ), s2t( \"channel\" ));\r";
                photoshopScript += "    reference.putEnumerated( s2t( \"channel\" ), s2t( \"channel\" ), s2t( \"mask\" ));\r";
                photoshopScript += "    descriptor.putReference( s2t( \"at\" ), reference );\r";
                photoshopScript += "    descriptor.putEnumerated( s2t( \"using\" ), s2t( \"userMaskEnabled\" ), s2t( maskParameter ));\r";
                photoshopScript += "    executeAction( s2t( \"make\" ), descriptor, DialogModes.NO );\r";
                photoshopScript += "}\r\n";

                // Save
                photoshopScript += "    var saveOptions = new TiffSaveOptions();\r";
                photoshopScript += "    saveOptions.transparency = true;\r";
                photoshopScript += "    saveOptions.layers = true;\r";
                photoshopScript += "    saveOptions.alphaChannels = true;\r";
                photoshopScript += "    saveOptions.embedColorProfile = true;\r";
                photoshopScript += "    saveOptions.imageCompression = TIFFEncoding.NONE;\r";
                photoshopScript += "    try {\r";
                photoshopScript += "        docRef.saveAs(docRef.fullName, saveOptions, true, Extension.LOWERCASE);\r";
                photoshopScript += "    } catch (e) {\r";
                photoshopScript += "        alert(\"Failed to save the file. Error: \" + e.message);\r";
                photoshopScript += "    }\r";
                photoshopScript += "}\r";

                photoshopScript += "app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);\r";

                // Bridgetalk
                var bt = new BridgeTalk();
                bt.target = photoshop;
                bt.body = openScript + photoshopScript;

                // Refresh selection
                bt.onResult = function(res) {
                    setTimeout(function() {
                        var link = selectedFrame.images[0].itemLink;
                        link.relink(new File(imageFile.fsName));
                        link.update();
                    }, 2000); // Delay of 2 seconds
                };

                bt.send();

            } else {
                alert("Photoshop is not available.");
            }

        } else {
            alert("The image file associated with the selected frame does not exist.");
        }

    } else {
        alert("The selected frame does not contain an image.");
    }

} else {
    alert("Please select a frame that contains an image in an InDesign document.");
}
TOPICS
Scripting
2.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 , Mar 16, 2024 Mar 16, 2024

Hi @davidn5918184 (and @Eugene Tyson) I actually couldn't get the bt.onResult function to execute at all. Is that a known thing? I've hardly done any BridgeTalk so I have no idea. I'm on MacOS, so maybe on Windows onResult works?

 

@davidn5918184 I've made some adjustments to your code, mostly for your interest, and my learning, and I've got it working. It's not super pretty, but has improved a bit I hope.

 

Some notes:

1. You don't have to write the bridgeTalk body out as strings. It's more con

...
Translate
Community Expert , Mar 16, 2024 Mar 16, 2024

I tried onResult with a delay but thats not working.

 

Hi @davidn5918184 , Bridgetalk can be difficult. Rather than editing your code, here is a example of template I use where I write a regular Photoshop function and retrieve it as a string for the BridgeTalk body object—I find it’s a lot easier to debug the Photoshop function doing it this way.

 

I don’t think a delay will help—you have to return the new tif file path via resObject.body to InDesign after the Photoshop code is complete. In this

...
Translate
Engaged ,
Mar 17, 2024 Mar 17, 2024

I was thinking of adding a loop to handle multiple images at once, so thanks for the suggestion. This might use more resources, but it shouldn't be a problem for just a few images. To overwrite the original file, I changed the line to: var cutoutFilePath = docRef.fullName.fsName; This achieves what I want, but now when the image updates in InDesign, it shows transparency so it is reloading correctly and then indicates the link was modified, needing a refresh. It's interesting that this issue only occurs when overwriting the file, not with the original code you wrote. Any idea why this is?

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
Engaged ,
Mar 18, 2024 Mar 18, 2024

I just force it by adding link.update(); to the bt.onResult function. Thanks again for all the help! 

    bt.onResult = function (obj) {

        var f = File(obj.body);

        if (!f.exists)
            return;

        var link = item.images[0].itemLink;

        try {

            link.relink(f);

            if (LinkStatus.NORMAL === link.status)
                success.value = true;

                link.update();

        } catch (error) { }

    };

    bt.send(timer);
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 ,
Mar 18, 2024 Mar 18, 2024

Good solution! So the relink method doesn't actually update the graphic if it is the same filename? A tiny bug I guess.

 

I would suggest changing the order like this:

try {

    link.relink(f);

    if (LinkStatus.LINK_OUT_OF_DATE === links[i].status)
        link.update();

    if (LinkStatus.NORMAL === link.status)
        success.value = true;

} catch (error) { }

so that the update happens before the script marks the success flag (otherwise I don't think it will be counted).

- Mark

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
LEGEND ,
Mar 18, 2024 Mar 18, 2024

Just as a general comment - not to any particular reply of anybody - and I don't know if JS is the same or not ...

 

... but in VBA - when you update link - it "ceases to exists" - so you need to do relink via a reference to parent...

 

Call myImgContainer.AllGraphics.Item(1).ItemLink.Relink(myPath & myNewFileName)

 

 

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 ,
May 27, 2025 May 27, 2025

@Robert at ID-Tasker said: "… .. but in VBA - when you update link - it "ceases to exists" - so you need to do relink via a reference to parent..."

 

Hi @Robert at ID-Tasker ,

do you see that with all kinds of placed file types or perhaps only with placed Illustrator files and PDF files where layers' visibility of the placed objects have been changed?

 

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 ,
Jun 21, 2025 Jun 21, 2025
LATEST

@davidn5918184 

I'm guessing that InDesign is conditioning the judgment update on triggering the ID window as the current window again.
You could try using the traditional way of double-clicking on the image by pressing Alt in ID, then modifying it in PS and saving it.
When you return to ID within a certain amount of time, the moment the ID window opens, the image is updated.

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