Extend script not auto refreshing the missing link.
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.");
}

