How to pass all parameters in a custom object via BridgeTalk
From time to time I have to write quite complex scripts using BridgeTalk. Long ago I new that it's possible to pass complex objects -- arrays, DOM and custom objects -- via BridgeTalk, but only recently I applied this in practice. Here is an example:
#target indesign var file = new File('~/Desktop/Sample.pdf'); if (!file.exists) { exit(); } CreateBridgeTalkMessage({ file: file, cropPage: 'CropToType.MEDIABOX', resolution: 300, usePageNumber: true, regExp: encodeURI(/\.pdf$/.source) }); function CreateBridgeTalkMessage(obj) { var bt = new BridgeTalk(); bt.target = "photoshop"; bt.body = ResaveInPS.toSource()+"("+obj.toSource()+");"; bt.onError = function(errObj) { $.writeln("Error: " + errObj.body); }; bt.send(100); } function ResaveInPS(serializedObject) { var obj = eval(serializedObject), regExp = new RegExp(decodeURI(obj.regExp)), pdfOpenOptions, doc; if (obj.file.name.match(regExp)) { $.writeln( "This is PDF" ); pdfOpenOptions = new PDFOpenOptions(); pdfOpenOptions.cropPage = eval(obj.cropPage); pdfOpenOptions.resolution = obj.resolution; pdfOpenOptions.usePageNumber = obj.usePageNumber; } else { $.writeln( "This is not PDF" ); } app.displayDialogs = DialogModes.NO; doc = app.open(obj.file, pdfOpenOptions); app.displayDialogs = DialogModes.ALL; }
This approach, in my opinion, has at least two advantages:
- You can pass all parameters in a single custom object instead of using a dozens of arguments
- You can pass DOM objects like files and folders directly. So this way you can avoid making some extra steps: e.g. get an image's file path in InDesign, pass it to Photoshop, then in Photoshop create the File object from this path.
It is important to note that in the function to be send via BridgeTalk we should use eval() for enumerations.
Thanks John Hawkinson from the Adobe InDesign scripting forum for the help with the code above.