Skip to main content
Inspiring
December 3, 2010
Question

Bridge talk for InDesign CS4/CS5 js

  • December 3, 2010
  • 1 reply
  • 24361 views

Hi,

Have this code that opens file in Ph and resaves it in different format. In this case I need to adjust it to open pdf file in photoshop and set crop to media box. It works with pdf files as is, but if there is white space around artwork, it gets removed. How can I set it to open with media box:

function ResaveInPS(myImagePath, myNewPath) {
     try {
           var myPsDoc = app.open(new File(myImagePath));
             if (myPsDoc.mode == DocumentMode.CMYK) {
                    myPsDoc.changeMode(ChangeMode.RGB);
               }
           var docName = myPsDoc.name;

          var myPNGSaveOptions = new PNGSaveOptions();
          myPNGSaveOptions.interlaced = false; // or true
          myPsDoc.saveAs(new File(myNewPath), myPNGSaveOptions, true);
          myPsDoc.close(SaveOptions.DONOTSAVECHANGES);     
     }
     catch (err) {
          try {
               app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
          }
          catch (err) {}
     }
}

Thank you for your help.

Yulia

This topic has been closed for replies.

1 reply

Muppet_Mark-QAl63s
Inspiring
December 3, 2010

In the same manner as you have made PNG save options object for saving. You need to have Photoshop make a PDF open options object to open the file in the required way else you will get default using just File and open. Color Space is one of the properties so you could then remove your mode check too by doing this…

YuliaartAuthor
Inspiring
December 3, 2010

I am trying the following quite blindly, to be honest, because there is no properties and methods for PDFOpenOptions in OMV, and so far not getting any result, so I am not sure how to specify MediaBox:

function ResaveInPS(myImagePath, myNewPath){
     try{
           var myPDFOpenOptions  = new PDFOpenOptions(MediaBox);
           myPDFOpenOptions.interlaced = true; // or true
           var myPsDoc = app.open(new File(myImagePath), myPDFOpenOptions, true);
          
             if (myPsDoc.mode != DocumentMode.CMYK){
                    myPsDoc.changeMode(ChangeMode.CMYK);
               }
           var docName = myPsDoc.name;

          var myTiffSaveOptions  = new TiffSaveOptions();
          myTiffSaveOptions.interlaced = false; // or true
          myPsDoc.saveAs(new File(myNewPath), myTiffSaveOptions, true);
          myPsDoc.close(SaveOptions.DONOTSAVECHANGES);
     }
     catch(err){
          try{
               app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
          }
          catch(err){}
     }
}

Do I need 'interlaced' line for PDFOpenOptions, or it's not related to it.

Thank you very much for your help.

Yulia

Kasyan Servetsky
Legend
March 27, 2011

In that example, the code throws a JavaScript error and does not let you continue if the function contains a literal backslash

This is intentional, because literal backslashes cannot possibly work correctly, so the only solution if one is detected is to give up and complain.

(Yes, both decodeURI() and unescape() were examples of shorter ways to write the same thing as String.fromCharCode()).


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. Below is an example: a new version of the code from a previous post.

#target indesign

var file = new File('~/Desktop/toysForUs.pdf');

if (!file.exists) exit();

var customObject = {}
customObject.file = file;
customObject.cropPage = 'CropToType.MEDIABOX';
customObject.resolution = 300;
customObject.usePageNumber = true;
customObject.regExp = '\\.pdf$';
var serializedObject = customObject.toSource();

CreateBridgeTalkMessage(serializedObject);

function CreateBridgeTalkMessage(serializedObject) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var script = "ResaveInPS = " + ResaveInPS.toSource() + "\r";
     script += "ResaveInPS(" + serializedObject + ");";
     bt.body = script;

     bt.onError = function(errObj) {
          $.writeln("Error: " + errObj.body);
     }

     bt.send(100);
}

function ResaveInPS(serializedObject) {
     var reconstructedObject = eval(serializedObject);
     var regExp = new RegExp(reconstructedObject.regExp.replace(decodeURI("%5C%5C"), decodeURI("%5C")));
     
      if (reconstructedObject.file.name.match(regExp) != null) {
          $.writeln( "This is PDF" );
          var pdfOpenOptions = new PDFOpenOptions;
          pdfOpenOptions.cropPage = eval(reconstructedObject.cropPage);
          pdfOpenOptions.resolution = reconstructedObject.resolution;
          pdfOpenOptions.usePageNumber = eval(reconstructedObject.usePageNumber);
     }
     else {
          $.writeln( "This is not PDF" );
     }
               
     app.displayDialogs = DialogModes.NO;
     var doc = app.open(reconstructedObject.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, boolean values and may be some other stuff.