4 Replies Latest reply on Oct 7, 2016 9:21 PM by DevilsChariot

    PDFOpenOptions issue with open(file, options)

    gus r2s

      Hi guys,

       

      I have a script to open JPGs and now I need to adapt it to open PDFs.

      I'm getting a "wrong type of enumerated value" error exactly because of the way I'm working the PDFOpenOptions (line "open(fileList[i], pdfOpenOptions);").

      I found quite a few scripts that define pdf open options just like I did, but in all of them the root path was hard coded (I need it dynamic).

       

      So, where am I going wrong? Too much time on it, can't see errors anymore... (mac, cs4)

       

      #target photoshop
      
      app.preferences.rulerUnits = Units.PIXELS;
      app.displayDialogs = DialogModes.NO;
      app.backgroundColor.rgb.red = 255;
      app.backgroundColor.rgb.green = 255;
      app.backgroundColor.rgb.blue = 255;
      
      var pdfOpenOptions = new PDFOpenOptions();
      
      pdfOpenOptions.antiAlias = true;
      pdfOpenOptions.mode = DocumentMode.RGB;
      pdfOpenOptions.bitsPerChannel = BitsPerChannelType.EIGHT;
      pdfOpenOptions.resolution = 72;
      pdfOpenOptions.supressWarnings = true;
      pdfOpenOptions.cropPage = CropToType.TRIMBOX;
      
      var fileList = File.openDialog("Select files to batch",true);
      
      if (fileList != null) {
           for (var i in fileList) {
                if (fileList[i] instanceof File && fileList[i].hidden == false) {
                     open(fileList[i], pdfOpenOptions);
                }
           }
      alert("Done");
      }
      
      else {
           alert("No files selected");
      }
      

       

      Thanks in advance!

        • 1. Re: PDFOpenOptions issue with open(file, options)
          Muppet Mark-QAl63s Level 4

          Replace 'DocumentMode.RGB' with 'OpenDocumentMode.RGB'

          • 3. Re: PDFOpenOptions issue with open(file, options)
            gus r2s Level 1

            Cool!

             

            I had to figure some other things out like extracting the path from openDialog
            which I didn't find anywhere else.

             

            So it's finely time to share a decent and commented script (with the invaluable
            help of Muppet Mark, thanks man!):

             

            //This script is set to open pdf files,
            //make 256x256px JPG duplicates,
            //create a folder named zJPGs relative to the doc's path
            //and save the files there
            
            //Gustavo Soares,
            //with the great help of Muppet Mark and many others
            
            #target photoshop
            
            //set units to pixels
            app.preferences.rulerUnits = Units.PIXELS;
            
            //avoid displaying dialog boxes
            app.displayDialogs = DialogModes.NO;
            
            //set background color to white
            app.backgroundColor.rgb.red = 255;
            app.backgroundColor.rgb.green = 255;
            app.backgroundColor.rgb.blue = 255;
            
            //define the pdf options for opening the document
            var pdfOpenOptions = new PDFOpenOptions();
            
            pdfOpenOptions.antiAlias = true;
            pdfOpenOptions.mode = OpenDocumentMode.RGB;
            pdfOpenOptions.bitsPerChannel = BitsPerChannelType.EIGHT;
            pdfOpenOptions.resolution = 72;
            pdfOpenOptions.supressWarnings = true;
            pdfOpenOptions.cropPage = CropToType.TRIMBOX;
            //pdfOpenOptions.page = 1
            
            //Select files to batch ('true' is for aloowing multiple seleciont)
            var fileList = File.openDialog("Select files to batch", true);
            
            //Returns the dynamic path from the openDialog –
            //there's certainly a better way for doing this
            var fullPath = fileList[0];
            var fullPathTemp = fullPath.fsName;
            var sliceIndex = fullPathTemp.lastIndexOf ("/");
            var docPath = fullPathTemp.slice(0, sliceIndex); //'0' means the beginning of the string
            
            //Read the functions below to understand the loop
            if (fileList != null) {
                 for (var i in fileList) {
                      if (fileList[i] instanceof File && fileList[i].hidden == false) {
                           open(fileList[i], pdfOpenOptions);
                           jpg256();
                           app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                      }
                 }
            alert("Done");
            }
            
            else {
                 alert("No files selected");
            }
            
            //––––––––––––––––––––
            //Functions start here
            //––––––––––––––––––––
            
            function jpg256() {     
                 
                 //make a 256 x 256px image of the original document
                 var myDoc = app.activeDocument
                 var docName = myDoc.name;
                 //can use this for opening files with extension
                 //var docNameChange = docName.slice(0, -4);
                 var myNewDocName = docName + ".jpg";
                 var myNewDoc = myDoc.duplicate(myNewDocName, true);
            
                 var width = myNewDoc.width;
                 var height = myNewDoc.height;
                 
                 //resize
                 if (height >= width) {
                      myNewDoc.resizeImage (null, 256, 72, ResampleMethod.BICUBICSHARPER); 
                      // 'null' resizes proportionally
                      myNewDoc.resizeCanvas (256, 256, AnchorPosition.MIDDLECENTER);
                 } 
                 else {
                      myNewDoc.resizeImage (256, null, 72, ResampleMethod.BICUBICSHARPER); 
                      // 'null' resizes proportionally
                      myNewDoc.resizeCanvas (256, 256, AnchorPosition.MIDDLECENTER);
                 }
                 
                 //save the file in a folder named 256x256
                 //check if it exists, if it doesn't then create
                 var jpegOptions = new JPEGSaveOptions();
                 jpegOptions.quality = 7;
                 
                 var folder256 = docPath + "/zJPGs";
            
                 if (Folder(folder256).exists) {
                      save256();
                 }
                 else {
                      var fldr = new Folder(folder256);
                      fldr.create();
                      save256();
                 }
            
                 function save256() {
                      myNewDoc.saveAs(new File(folder256), jpegOptions);
                      myNewDoc.close(SaveOptions.DONOTSAVECHANGES);
                 }
            } 
            

            • 4. Re: PDFOpenOptions issue with open(file, options)
              DevilsChariot Level 1

              I wanted to use the dynamic file opening code you wrote in one of my own scripts. However, I also need to open EPS, TIff, PSD, PNG, and jpeg files.

               

              There is something in the code that disallows or doesn't specify whcih files can be opened.

               

              Could you explain or give me a sample to get me started in the right direction?

               

              Thanks for your consideration.