Skip to main content
May 17, 2011
Answered

PDFOpenOptions issue with open(file, options)

  • May 17, 2011
  • 1 reply
  • 2942 views

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, 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 instanceof File && fileList.hidden == false) {

               open(fileList, pdfOpenOptions);

          }

     }

alert("Done");

}

else {

     alert("No files selected");

}

Thanks in advance!

Correct answer Muppet_Mark-QAl63s

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

1 reply

Muppet_Mark-QAl63s
Muppet_Mark-QAl63sCorrect answer
Inspiring
May 17, 2011

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

May 17, 2011

Bingo! Thanks!!!

May 17, 2011

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 instanceof File && fileList.hidden == false) {

               open(fileList, 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);

     }

}