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

Bridge talk for InDesign CS4/CS5 js

Participant ,
Dec 02, 2010 Dec 02, 2010

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

22.5K

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
Guide ,
Dec 03, 2010 Dec 03, 2010

Copy link to clipboard

Copied

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…

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
Participant ,
Dec 03, 2010 Dec 03, 2010

Copy link to clipboard

Copied

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

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
Community Expert ,
Dec 03, 2010 Dec 03, 2010

Copy link to clipboard

Copied

Mark was probably thinking about Illustrator. Indesign has a PDFPlacePreferences which addresses the pdfCrop which can be set to a number of options. Look in Jongware's html version of the OMV.

http://www.jongware.com/idjshelp.html

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
Guru ,
Dec 04, 2010 Dec 04, 2010

Copy link to clipboard

Copied

function ResaveInPS(myImagePath, myNewPath) {
     try {
                if (myImagePath.match(/\.pdf$/) != null) {
                    var pdfOpenOptions = new PDFOpenOptions;
                    pdfOpenOptions.cropPage = CropToType.MEDIABOX;
                    pdfOpenOptions.resolution = 72;
                    pdfOpenOptions.usePageNumber = true;
               }
           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) {}
     }
}

PDFOpenOptions can be found in Photoshop JavaScript reference (or OMV) -- not in InDesign OMV -- and it has several properties  corresponding to the settings in the Photoshop's "Import PDF" dialog box. The property you are interested in is called cropPage:

var pdfOpenOptions = new PDFOpenOptions;
pdfOpenOptions.cropPage = CropToType.MEDIABOX;

Kasyan

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
Guide ,
Dec 05, 2010 Dec 05, 2010

Copy link to clipboard

Copied

Kasyan, you have the correct property for the PDF open options which you create… but then you don't pass those options as parameter open command? You can also as I said choose the opening mode…

// Record then change user prefs var uDD = app.displayDialogs; app.displayDialogs = DialogModes.NO; var pdfOpenOptions = new PDFOpenOptions(); pdfOpenOptions.antiAlias = true; pdfOpenOptions.bitsPerChannel = BitsPerChannelType.EIGHT; pdfOpenOptions.cropPage = CropToType.MEDIABOX; pdfOpenOptions.mode = OpenDocumentMode.RGB; pdfOpenOptions.page = '1'; pdfOpenOptions.resolution = 72; pdfOpenOptions.suppressWarnings = true; pdfOpenOptions.usePageNumber = true; var myPsDoc = app.open(new File('~/Desktop/Testing.pdf'), pdfOpenOptions); // The rest… save, close, etc… // Put back prefs app.displayDialogs = uDD;

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
Guru ,
Dec 05, 2010 Dec 05, 2010

Copy link to clipboard

Copied

Hi Muppet Mark,

This function is a part of a larger script that was discussed in this thread. I set dialog mode there but in a separate function.

but then you don't pass those options as parameter open command?

Ouch! I know this but made a typo. Thank you for pointing this out.

Of course, this line should be:

var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);

Kasyan

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
Guide ,
Dec 05, 2010 Dec 05, 2010

Copy link to clipboard

Copied

Kasyan, I presumed this was just a typo error… Didn't look at the other post regarding the dialogs. More trying to point out the 'OpenDocumentMode.RGB' then you don't need to check n change… May as well use the option…

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
Guru ,
Dec 05, 2010 Dec 05, 2010

Copy link to clipboard

Copied

But this script opens and resaves other formats too -- for example JPEGs --  not only PDFs. And the OP wants them to be resaved to PNG format, but this is impossible if the image is in CMYK. That's why the script checks the color mode for all images it opens instead of using the 'OpenDocumentMode.RGB' which would work only for PDFs.

Kasyan

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
Participant ,
Dec 06, 2010 Dec 06, 2010

Copy link to clipboard

Copied

Thank you, guys.

I added some my personal modifications to the piece to adjusted to what I need, mostly that I need it to be saved as tif this time, and that part works, but the art piece still looses its white space around, I don't think it engages the pdfOpenOptions yet. Could you check it:

function ResaveInPS(myImagePath, myNewPath) {
    try {
               if (myImagePath.match(/\.pdf$/) != null) {
                   var pdfOpenOptions = new PDFOpenOptions;
                   pdfOpenOptions.cropPage = CropToType.MEDIABOX;
                   pdfOpenOptions.resolution = 300;
                   pdfOpenOptions.usePageNumber = true;
              }
          var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
            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) {}
    }
}

Thank you very much,

Yulia

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
Guide ,
Dec 07, 2010 Dec 07, 2010

Copy link to clipboard

Copied

Yulia, 'interlaced' is NOT a property of the TIFF save options you have switched the save options object PNG to TIFF but NOT the required properties of that format…

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
Guru ,
Dec 07, 2010 Dec 07, 2010

Copy link to clipboard

Copied

Yulia, I don't quite understand what the white space around the art piece is. What setting in Photoshop influences it?

Kasyan

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
Participant ,
Dec 07, 2010 Dec 07, 2010

Copy link to clipboard

Copied

The white space is when a business card is set up on a white background with black text on it, if Media Box is not selected, the photoshop makes document size the size of the text and crops out all the white space around it, so the actual size of the card is lost and when the new file is placed again into InDesign, it becomes enlarged to the size of the original file and I need them to look the same.

I did removed the 'interlaced' from the code, but still not getting in full size:

function ResaveInPS(myImagePath, myNewPath) {
    try {
               if (myImagePath.match(/\.pdf$/) != null) {
                   var pdfOpenOptions = new PDFOpenOptions;
                   pdfOpenOptions.cropPage = CropToType.MEDIABOX;
                   pdfOpenOptions.resolution = 300;
                   pdfOpenOptions.usePageNumber = true;
              }
          var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
            if (myPsDoc.mode != DocumentMode.CMYK) {
                   myPsDoc.changeMode(ChangeMode.CMYK);
              }
          var docName = myPsDoc.name;

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

And I am not sure why, but when I select PDFOpenOptions in OMV, nothing changes, no new info appears in the Properties and Methods window, but it keeps whatever was there beforehand.

Thank you for you help.

Yulia

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
Guru ,
Dec 08, 2010 Dec 08, 2010

Copy link to clipboard

Copied

It's difficult for me to give a definite advice without seeing what's going on in your file. I tried to recreate you problem but couldn't. May be flattening the image would solve the issue?

var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
myPsDoc.flatten();

Kasyan

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
Guide ,
Dec 08, 2010 Dec 08, 2010

Copy link to clipboard

Copied

Kasyan, I too think the movement on replace is caused by a difference in the bounds… The PDF using pre-defined boxes. I suspect the TIF is NOT flat and that ID is using the non-transparent area as the new bounds… on replace? You could either flatten in the document or using the TIF options…

function ResaveInPS(myImagePath, myNewPath) {      try {           if (myImagePath.match(/\.pdf$/) != null) {                var pdfOpenOptions = new PDFOpenOptions;                pdfOpenOptions.cropPage = CropToType.MEDIABOX;                pdfOpenOptions.resolution = 300;                pdfOpenOptions.usePageNumber = true;           }           var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);           if (myPsDoc.mode != DocumentMode.CMYK) {                myPsDoc.changeMode(ChangeMode.CMYK);           }           myPsDoc.flatten(); // or layers false…                     var myTiffSaveOptions  = new TiffSaveOptions();                     myTiffSaveOptions.alphaChannels = false;           myTiffSaveOptions.byteOrder = ByteOrder.MACOS;           myTiffSaveOptions.embedColorProfile = true;           myTiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;           myTiffSaveOptions.layers = true; // Or false here…           myTiffSaveOptions.spotColors = true;           myTiffSaveOptions.transparency = true;                     myPsDoc.saveAs(new File(myNewPath), myTiffSaveOptions);           app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);      } catch (err) {} }

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
Participant ,
Dec 08, 2010 Dec 08, 2010

Copy link to clipboard

Copied

I added the flattening and other options, but it didn't resolve the issue. I think, Kasyan, the reason you don't see it might be that if dialog box in Ph set up to Media already, it will remember the settings until ph is restarted. But if you have it preset to Bounding box, let's say, does it change it for you to Media box. If it does, than it might be something else I have added tot he whole code. Here is the whole piece:

#target indesign

var myDoc = app.activeDocument;
var myDocPath = myDoc.filePath;
var myLivePath = myDoc.filePath;
var myFolder = new Folder(myLivePath);

SetDisplayDialogs("NO");
OpenFiles();
SetDisplayDialogs("ALL");
UpdateAllOutdatedLinks();
myLink.editOriginal();
//alert("Done");


function OpenFiles(){
   
   
    var myFile= app.selection[0];
    try{
        if(myFile.isValid == true){}
    }
    catch (e){
    alert ("Please choose PDF and rerun the script");
        exit();
        }

    if(myFile.constructor.name == "PDF"){
        myLink = myFile.itemLink;
    }
    else{if(myFile.constructor.name == "Rectangle"){
        myLink = myFile.graphics[0].itemLink;
    }
    else{
        alert ("The artwork has to be PDF file. Please choose correct artwork and rerun the script");
        exit();
    }
    }

           
          if (myLink.name.toLowerCase().indexOf(".pdf") > -1){
               var myImage = myLink.parent;
               var myImagePath = myLink.filePath;
               var myImageFile = new File(myImagePath);
               var myNewPath =  myFolder.absoluteURI + "/" + GetFileNameOnly(myImageFile.name) + ".tif";
               CreateBridgeTalkMessage(myImagePath, myNewPath);
               Relink(myLink, myNewPath);
          }
}
//--------------------------------------------------------------------------------------------------------------
function CreateBridgeTalkMessage(myImagePath, myNewPath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = ResaveInPS.toString() + "\r";
     myScript += "ResaveInPS(\"" + myImagePath + "\", \"" + myNewPath + "\");";
     bt.body = myScript;
     bt.onResult = function(resObj) {}
     bt.send(100);
}
//--------------------------------------------------------------------------------------------------------------
function ResaveInPS(myImagePath, myNewPath) {
    try {
               if (myImagePath.match(/\.pdf$/) != null) {
                   var pdfOpenOptions = new PDFOpenOptions;
                   pdfOpenOptions.cropPage = CropToType.MEDIABOX;
                   pdfOpenOptions.resolution = 300;
                   pdfOpenOptions.usePageNumber = true;
              }
          var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
            if (myPsDoc.mode != DocumentMode.CMYK) {
                   myPsDoc.changeMode(ChangeMode.CMYK);
              }
          myPsDoc.flatten(); // or layers false…
         
          var docName = myPsDoc.name;

         var myTiffSaveOptions  = new TiffSaveOptions();
         myTiffSaveOptions.alphaChannels = false;
          myTiffSaveOptions.byteOrder = ByteOrder.MACOS;
          myTiffSaveOptions.embedColorProfile = true;
          myTiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
          myTiffSaveOptions.layers = true; // Or false here…
          myTiffSaveOptions.spotColors = false;
          myTiffSaveOptions.transparency = true;

         myPsDoc.saveAs(new File(myNewPath), myTiffSaveOptions);
         myPsDoc.close(SaveOptions.DONOTSAVECHANGES);
    }
    catch (err) {
         try {
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
         }
         catch (err) {}
    }
}
//--------------------------------------------------------------------------------------------------------------
function Relink(myLink, myNewPath){
     var newFile = new File (myNewPath);
     if (newFile.exists) {
          var originalLinkFile = new File(myLink.filePath);
          myLink.relink(newFile);
         
          try { // for versions prior to 6.0.4
               var myLink = myLink.update();
          }
          catch(err) {}
     }
}
//--------------------------------------------------------------------------------------------------------------
function SetDisplayDialogs(Mode) { // turn on-off DialogModes in PS -- don't want to see the open file dialog while the script is running
     var bt = new BridgeTalk;
     bt.target = "photoshop";
     var myScript = "app.displayDialogs = DialogModes." + Mode + ";";
     bt.body = myScript;
     bt.send();
}
//--------------------------------------------------------------------------------------------------------------
function UpdateAllOutdatedLinks() {
     for (var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--) {
          var myLink = myDoc.links[myCounter];
          if (myLink.status == LinkStatus.linkOutOfDate) {
               myLink.update();
          }
     }
}
//--------------------------------------------------------------------------------------------------------------
function GetFileNameOnly(myFileName) {
     var myString = "";
     var myResult = myFileName.lastIndexOf(".");
     if (myResult == -1) {
          myString = myFileName;
     }
     else {
          myString = myFileName.substr(0, myResult);
     }
     return myString;
}

Thank you for the help.

Yulia

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
Guru ,
Dec 09, 2010 Dec 09, 2010

Copy link to clipboard

Copied

I think, Kasyan, the reason you don't see it might be that if dialog box in Ph set up to Media already, it will remember the settings until ph is restarted. But if you have it preset to Bounding box, let's say, does it change it for you to Media box.

The Crop To is set to Bounding Box by default and the script changes it to Media box, as expected. I tested your script but the white space is not removed for me. Could you post sample files (or send them to my e-mail address: askoldich [at] yahoo [dot] com) so I could see what's going on in your document?

Kasyan

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
Guru ,
Dec 11, 2010 Dec 11, 2010

Copy link to clipboard

Copied

Hi Yulia,

I adjusted the script:

#target indesign
var myDoc = app.activeDocument;
var myDocPath = myDoc.filePath;
var myLivePath = myDoc.filePath;
var myFolder = new Folder(myLivePath);

OpenFiles();
UpdateAllOutdatedLinks();
//~ myLink.editOriginal();
//alert("Done");

function OpenFiles(){
    var myFile= app.selection[0];
    try{
        if(myFile.isValid == true){}
    }
    catch (e){
    alert ("Please choose PDF and rerun the script");
        exit();
     }

    if(myFile.constructor.name == "PDF"){
        myLink = myFile.itemLink;
    }
    else{if(myFile.constructor.name == "Rectangle"){
        myLink = myFile.graphics[0].itemLink;
    }
    else{
        alert ("The artwork has to be PDF file. Please choose correct artwork and rerun the script");
        exit();
    }
    }
     
          if (myLink.name.toLowerCase().indexOf(".pdf") > -1){
               var myImage = myLink.parent;
               var myImagePath = myLink.filePath;
               var myImageFile = new File(myImagePath);
               var myNewPath =  myFolder.absoluteURI + "/" + GetFileNameOnly(myImageFile.name) + ".tif";
               CreateBridgeTalkMessage(myImagePath, myNewPath);
               Relink(myLink, myNewPath);
          }
}
//--------------------------------------------------------------------------------------------------------------
function CreateBridgeTalkMessage(myImagePath, myNewPath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     
     var myScript = 'app.displayDialogs = DialogModes.NO;\r';
     myScript += 'try {\r';
     myScript += 'if ("' + myImagePath + '".match(/\.pdf$/) != null) {\r';
     myScript += '    var pdfOpenOptions = new PDFOpenOptions();\r';
     myScript += '    pdfOpenOptions.cropPage = CropToType.MEDIABOX;\r';
     myScript += '    pdfOpenOptions.resolution = 300;\r';
     myScript += '    pdfOpenOptions.usePageNumber = true;\r';
     myScript += '    var myPsDoc = app.open(new File("' + myImagePath  + '"), pdfOpenOptions);\r';     
     myScript += '}\r';
     myScript += 'else {\r';
     myScript += '    var myPsDoc = app.open(new File(myImagePath));\r';
     myScript += '}\r';
     myScript += 'if (myPsDoc.mode != DocumentMode.CMYK) {\r';
     myScript += '    myPsDoc.changeMode(ChangeMode.CMYK);\r';
     myScript += '}\r';
     myScript += 'myPsDoc.flatten();\r';
     myScript += 'var docName = myPsDoc.name;\r';
     myScript += 'var myTiffSaveOptions  = new TiffSaveOptions();\r';
     myScript += 'myTiffSaveOptions.alphaChannels = false;\r';
     myScript += 'myTiffSaveOptions.byteOrder = ByteOrder.MACOS;\r';
     myScript += 'myTiffSaveOptions.embedColorProfile = true;\r';
     myScript += 'myTiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;\r';
     myScript += 'myTiffSaveOptions.layers = true;\r';
     myScript += 'myTiffSaveOptions.spotColors = false;\r';
     myScript += 'myTiffSaveOptions.transparency = true;\r';
     myScript += 'myPsDoc.saveAs(new File("' + myNewPath + '"), myTiffSaveOptions);\r';
     myScript += 'myPsDoc.close(SaveOptions.DONOTSAVECHANGES);\r';
     myScript += '}\r';
     myScript += 'catch (err) {\r';
     myScript += '    try {\r';
     myScript += '        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);\r';
     myScript += '    }\r';
     myScript += '    catch (err) {}\r';
     myScript += '}\r';
     myScript += 'app.displayDialogs = DialogModes.ALL;\r';
//~      $.writeln(myScript);
     bt.onResult = function(resObj) {}
     bt.send(100);
     }
//--------------------------------------------------------------------------------------------------------------
function ResaveInPS(myImagePath, myNewPath) {
    try {
               if (myImagePath.match(/\.pdf$/) != null) {
                   var pdfOpenOptions = new PDFOpenOptions;
                   pdfOpenOptions.cropPage = CropToType.MEDIABOX;
                   pdfOpenOptions.resolution = 300;
                   pdfOpenOptions.usePageNumber = true;
              }
          var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
            if (myPsDoc.mode != DocumentMode.CMYK) {
                   myPsDoc.changeMode(ChangeMode.CMYK);
              }
          myPsDoc.flatten(); // or layers false…
         
          var docName = myPsDoc.name;

         var myTiffSaveOptions  = new TiffSaveOptions();
         myTiffSaveOptions.alphaChannels = false;
          myTiffSaveOptions.byteOrder = ByteOrder.MACOS;
          myTiffSaveOptions.embedColorProfile = true;
          myTiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
          myTiffSaveOptions.layers = true; // Or false here…
          myTiffSaveOptions.spotColors = false;
          myTiffSaveOptions.transparency = true;

         myPsDoc.saveAs(new File(myNewPath), myTiffSaveOptions);
         myPsDoc.close(SaveOptions.DONOTSAVECHANGES);
    }
    catch (err) {
         try {
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
         }
         catch (err) {}
    }
}
//--------------------------------------------------------------------------------------------------------------
function Relink(myLink, myNewPath){
     var newFile = new File (myNewPath);
     if (newFile.exists) {
          var originalLinkFile = new File(myLink.filePath);
          myLink.relink(newFile);
         
          try { // for versions prior to 6.0.4
               var myLink = myLink.update();
          }
          catch(err) {}
     }
}
//--------------------------------------------------------------------------------------------------------------
function UpdateAllOutdatedLinks() {
     for (var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--) {
          var myLink = myDoc.links[myCounter];
          if (myLink.status == LinkStatus.linkOutOfDate) {
               myLink.update();
          }
     }
}
//--------------------------------------------------------------------------------------------------------------
function GetFileNameOnly(myFileName) {
     var myString = "";
     var myResult = myFileName.lastIndexOf(".");
     if (myResult == -1) {
          myString = myFileName;
     }
     else {
          myString = myFileName.substr(0, myResult);
     }
     return myString;
}

Kasyan

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
Participant ,
Dec 11, 2010 Dec 11, 2010

Copy link to clipboard

Copied

Kasyan, thank you, this is great, it was missing one little line:

     bt.body = myScript;

after line 84:

     bt.onResult = function(resObj) {} 
     bt.body = myScript;
     bt.send(100);

but now it works perfectly.

Thank you very-very much.

Yulia

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
Guru ,
Dec 12, 2010 Dec 12, 2010

Copy link to clipboard

Copied

...it was missing one little line...

Oops! I just wanted to remove some unnecessary stuff after testing to clean up the script and took out the line by chance.

But I've discovered one interesting fact while solving the problem: when the Photoshop script is send as a string it works as expected, but when I'm wrapping the main code in a function via toString() or toSource(), it doesn't.

Let's make a little test: download this sample file to the desktop, start InDesign and Photoshop and run the code below.

This works as expected — Crop To: Media Box is applied in Photoshop.

2.png

#target indesign
CreateBridgeTalkMessage("~/Desktop/toysForUs.pdf");

function CreateBridgeTalkMessage(myImagePath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = 'var pdfOpenOptions = new PDFOpenOptions;\r';
     myScript += 'pdfOpenOptions.cropPage = CropToType.MEDIABOX;\r';
     myScript += 'pdfOpenOptions.resolution = 300;\r';
     myScript += 'pdfOpenOptions.usePageNumber = true;\r';
     myScript += 'app.displayDialogs = DialogModes.NO;\r';
     myScript += 'var myPsDoc = app.open(new File("' + myImagePath + '"), pdfOpenOptions);\r';
     myScript += 'app.displayDialogs = DialogModes.ALL;\r';
     bt.body = myScript;
     bt.onResult = function(resObj) {}
     bt.send(100);
}

But the following code doesn't — the CropToType.MEDIABOX setting is ignored:

1.png

#target indesign
CreateBridgeTalkMessage("~/Desktop/toysForUs.pdf");

function CreateBridgeTalkMessage(myImagePath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = ResaveInPS.toString() + "\r";
     myScript += "ResaveInPS(\"" + myImagePath + "\");";
     bt.body = myScript;
     bt.onResult = function(resObj) {}
     bt.send(100);
}

function ResaveInPS(myImagePath) {
        if (myImagePath.match(/\.pdf$/) != null) {
             var pdfOpenOptions = new PDFOpenOptions;
             pdfOpenOptions.cropPage = CropToType.MEDIABOX;
               pdfOpenOptions.resolution = 300;
             pdfOpenOptions.usePageNumber = true;
       }
              
     app.displayDialogs = DialogModes.NO;
     var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
     app.displayDialogs = DialogModes.ALL;
}

It doesn't work with toSource() as well:

#target indesign
CreateBridgeTalkMessage("~/Desktop/toysForUs.pdf");

function CreateBridgeTalkMessage(myImagePath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = "ResaveInPS = " + ResaveInPS.toSource() + "\r";
     myScript += "ResaveInPS(\"" + myImagePath + "\");";
     bt.body = myScript;
     bt.send(100);
}

function ResaveInPS(myImagePath) {
        if (myImagePath.match(/\.pdf$/) != null) {
             var pdfOpenOptions = new PDFOpenOptions;
             pdfOpenOptions.cropPage = CropToType.MEDIABOX;
               pdfOpenOptions.resolution = 300;
             pdfOpenOptions.usePageNumber = true;
       }
              
     app.displayDialogs = DialogModes.NO;
     var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
     app.displayDialogs = DialogModes.ALL;
}

I found this post by Jongware, as far as I understand, explaining the cause of this phenomenon — but couldn’t get the meaning of it (I am not a programmer by background have no idea what "single static buffer" is)

Could anybody shed light on this issue?

Does it mean that using a plain string in body parameter is more reliable than a function with toString()/toSource()?

Kasyan

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
Participant ,
Dec 13, 2010 Dec 13, 2010

Copy link to clipboard

Copied

Thank you, Kasyan, this is very helpful.

Yulia

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
LEGEND ,
Mar 13, 2011 Mar 13, 2011

Copy link to clipboard

Copied

Hey, Kasyan!

I had meant to look into this for quite a while, because it made me very suspicious.

It shouldn't be the case that the .toString() method gives you different results, and it's bad practice to write functions in strings instead of writing them out properly and then converting to a string. It's much better to let the compiler check your code for you, much more readable, etc., etc.

Anyhow, your two examples don't do the same thing! One of them does a regexp match for /\.pdf$/ and the other doesn't. And it turns out the regexp match fails, when you would not expect it to.

It turns out there's an Adobe bug there!

Try this:

     var bt = new BridgeTalk();
     bt.target = "photoshop";
     bt.body = "alert(/\\.pdf$/)";
     alert(bt.body);
     bt.onResult = function(resObj) {}
     bt.send(100);

There are two backslashes because inside a quoted ("x") string, two backslashes represent one backslash.

The first alert, in InDesign, shows that the backslash was properly removed:

id.png

but then when it gets transmitted via BridgeTalk to PhotoShop, the backslash gets doubled!:

ps.png

That's not supposed to happen. It appears that regular expression literals get munged in BridgeTalk.

If instead you used

     bt.body = "alert(new RegExp('\\.pdf'))";

Then it works fine.

Incidently, this has nothing to do with the single static buffer concern. That is simply the idea that if you try to run two instances of some method that uses a single buffer at the same time, they scramble over each other's workspace and get confused. Allegedly .toSource() is such a method, so if you tried to call .toSource() twice in the same variable assignment where they could conceivably happen at the same time, then you might have this problem.

Something like:

var result = one.toSource() + two.toSource();

But I don't believe it has any applicability here.

All the above tests under OSX with CS5.

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
Guru ,
Mar 22, 2011 Mar 22, 2011

Copy link to clipboard

Copied

Hi John,

I am sorry for the late reply, I have been denied access to the forum for about a week. Thanks Harbs for the advice to trash cookies — now I am back at last.

Thank you very much for making the investigation. It turns out that solution to this mysterious enigma is so simple!

I often write inter-application scripts using BridgeTalk so your discovery is very important to me.

Thank you again.

Kasyan

P.S. Below I am posting corrected code in case somebody is interested.

toSource()

#target indesign
CreateBridgeTalkMessage("~/Desktop/toysForUs.pdf");

function CreateBridgeTalkMessage(myImagePath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = "ResaveInPS = " + ResaveInPS.toSource() + "\r";
     myScript += "ResaveInPS(\"" + myImagePath + "\");";
     bt.body = myScript;
     bt.send(100);
}

function ResaveInPS(myImagePath) {
        if (myImagePath.match(new RegExp('\.pdf')) != null) {
               var pdfOpenOptions = new PDFOpenOptions;
               pdfOpenOptions.cropPage = CropToType.MEDIABOX;
               pdfOpenOptions.resolution = 300;
               pdfOpenOptions.usePageNumber = true;
       }
               
     app.displayDialogs = DialogModes.NO;
     var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
     app.displayDialogs = DialogModes.ALL;
}

toString()

#target indesign
CreateBridgeTalkMessage("~/Desktop/toysForUs.pdf");

function CreateBridgeTalkMessage(myImagePath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = ResaveInPS.toString() + "\r";
     myScript += "ResaveInPS(\"" + myImagePath + "\");";
     bt.body = myScript;
     bt.onResult = function(resObj) {}
     bt.send(100);
}

function ResaveInPS(myImagePath) {
        if (myImagePath.match(new RegExp('\.pdf')) != null) {
             var pdfOpenOptions = new PDFOpenOptions;
             pdfOpenOptions.cropPage = CropToType.MEDIABOX;
               pdfOpenOptions.resolution = 300;
             pdfOpenOptions.usePageNumber = true;
       }
               
     app.displayDialogs = DialogModes.NO;
     var myPsDoc = app.open(new File(myImagePath), pdfOpenOptions);
     app.displayDialogs = DialogModes.ALL;
}

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
LEGEND ,
Mar 22, 2011 Mar 22, 2011

Copy link to clipboard

Copied

Whoops, I had meant to file a bug on this...has anybody done so yet?

Anyhow, I don't think this code is quite right:

if (myImagePath.match(new RegExp('\.pdf')) != null) {

because inside a quoted string, you need to double the backslash. Otherwise it only escapes the next character (such as \n), and a period does not need escaping. So the above is the same as Regexp('.pdf') which isn't what you want. And without a terminal dollar-sign (whoops -- it looks like I lost the $ somewhere along the way in my last example), that case will actually happen, say a file called "mypdfproblem.jpg."

So I think you really want RegExp('\\.pdf$').

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
Guru ,
Mar 22, 2011 Mar 22, 2011

Copy link to clipboard

Copied

whoops -- it looks like I lost the $ somewhere along the way in my last example

I forgot about it too.

because inside a quoted string, you need to double the backslash.

At first I tried to use two backslashes before the period but it didn't work.

For example:

#target indesign
CreateBridgeTalkMessage("~/Desktop/toysForUs.pdf");

function CreateBridgeTalkMessage(myImagePath) {
     var bt = new BridgeTalk();
     bt.target = "photoshop";
     var myScript = "Test = " + Test.toSource() + "\r";
     myScript += "Test(\"" + myImagePath + "\");";
     bt.body = myScript;
     bt.send(100);
}

function Test(myImagePath) {
      $.writeln( myImagePath.match(new RegExp('\\.pdf$')) != null );
}

It writes false to console.

If I change new RegExp('\.pdf$') to new RegExp('\\.pdf$') or to new RegExp('.pdf$'), it writes true.

Kasyan

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