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

Extend script not auto refreshing the missing link.

Engaged ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

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

Views

455

Translate

Translate

Report

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

...

Votes

Translate

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

...

Votes

Translate

Translate
Community Expert ,
Mar 18, 2024 Mar 18, 2024

Copy link to clipboard

Copied

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)

 

 

Votes

Translate

Translate

Report

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