Copy link to clipboard
Copied
I have a script that is opening a multi-Page .pdf in Photoshop and I need to finish the process by having it save them out as Flattened .tif files... I have this script working well... just need it to repeat the process until all pages of the .pdf (that were opened) have been saved. Any assistance is appreciated.
#target photoshop
var theFile = File.openDialog();
app.displayDialogs = DialogModes.NO;
var check = true;
var page = 1;
// define pdfopenoptions;
var pdfOpenOpts = new PDFOpenOptions;
pdfOpenOpts.antiAlias = true;
pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
pdfOpenOpts.cropPage = CropToType.MEDIABOX;
pdfOpenOpts.mode = OpenDocumentMode.RGB;
pdfOpenOpts.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
while (check == true) {
try {
pdfOpenOpts.page = page;
// open;
var thePdf = app.open(theFile, pdfOpenOpts);
page++;
} catch (e) {
check = false
};
};
if (app.documents.length > 0) {
var thedoc = app.activeDocument;
var docName = thedoc.name;
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {var basename = docName};
try {var docPath = thedoc.path}
catch (e) {var docPath = "~/Desktop/Illustrator Hot Folder/11 x 8.5 Zund-Proof-Hi-Res/PS"};
tifOpts = new TiffSaveOptions() ;
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.IBM;
tifOpts.layers = false;
thedoc.saveAs((new File(docPath+"/"+basename+".tif")), tifOpts, false)
};
Copy link to clipboard
Copied
This may help. The script I wrote does not save any files. It just get all Pages into a single Photoshop document as layers. You Open all the PDF pages into Photoshop with a single open. Photoshop open each page as a document. Then you run my script. It will open a new document and make a layer of all the open document the close all the open documents but the new document the script created. You could then save a layered PSD or Tiff or export layers as files,.
An open page of a PDF may be empty I use the clipboard copy merge and paste and catch that. I wanted to be able to handle any open document layered or not or empty, I did not know how duplicate merge layer would work if a document was empty.
Re: Combine all your open documents in a single document
Or change the script to save the document rather then creating a layered document. You just seem to want to save and close the open documents. The Open document may also certain a transparent background you may want to add a white layer under or just flatten the documents first to get rid of the transparency.
I'm dyslectics I just hack at scripting and do not even try to decipher regular expression my brain can't hack them (/(.*)\.[^\.]+$/) greek to me...
Here the stack without transparency
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// Stack the open Document Visible layer composite into a new 300 DPI document
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
// ======================================================= */
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
var theFile = File.openDialog("Select PDF", "Select:*.pdf");
app.displayDialogs = DialogModes.NO;
var check = true;
var page = 1;
// define pdfopenoptions;
var pdfOpenOpts = new PDFOpenOptions;
pdfOpenOpts.antiAlias = true;
pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
pdfOpenOpts.cropPage = CropToType.MEDIABOX;
pdfOpenOpts.mode = OpenDocumentMode.RGB;
pdfOpenOpts.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
app.togglePalettes(); // toggle off palette so Photoshop will not need to update them
while (check == true) {
try {
pdfOpenOpts.page = page;
// open;
var thePdf = app.open(theFile, pdfOpenOpts);
page++;
}
catch (e) { check = false };
};
if (documents.length >= 2) { main(); }
else {
app.togglePalettes(); // toggle on
alert("multiple Document are not open in Photoshop");
}
///////////////////////////////////////////////////////////////////////////////
// main function //
///////////////////////////////////////////////////////////////////////////////
function main() {
var orig_ruler_units = app.preferences.rulerUnits;
var orig_display_dialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS; // set the ruler units to PIXELS
app.displayDialogs = DialogModes.NO; // Set Dialogs off
//var closeDocs = prompt("Should documents be Close without saving?", "Yes");
var closeDocs = "Yes";
var canvasSize = new Array(); // what size canvas is needed
canvasSize=getDocDementions(); // get max width and height of open documents
//app.togglePalettes(); // toggle off palette so Photoshop will not need to update them
var doc = app.documents.add( canvasSize[0], canvasSize[1], 300, "Stacked Opened Documents", NewDocumentMode.RGB, DocumentFill.TRANSPARENT ); // create a new document
//for (var i=0;i<documents.length-1;i++) { // stack a layer for the open document into the new document
for (var i=documents.length-2;i>-1;i--) { // stack a layer for the open document into the new document
app.activeDocument = documents; // switch active document
var layerName = app.activeDocument.name; // get document name
try{executeAction( charIDToTypeID( "FltI" ), undefined, DialogModes.NO );}
catch(e){}
app.activeDocument.selection.selectAll(); // select all
try {app.activeDocument.selection.copy(true);} // copy composite of visible layers to clipboard
catch (e) {try {app.activeDocument.selection.copy();} // copy composite of visible layers to clipboard
catch (e) { } // empty
}
app.activeDocument.selection.deselect(); // deselect
app.activeDocument = documents[documents.length-1]; // switch to newly created document
try { app.activeDocument.paste(); } // paste on composite of visible layers
catch (e) { app.activeDocument.artLayers.add(); } // add an empty layer
app.activeDocument.activeLayer.name=layerName; // label layer with Document name
}
if ( closeDocs == "Yes" ) closeAllButOne(); // close all opened document except the newly created document
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
app.displayDialogs = orig_display_dialogs; // Reset display dialogs
app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings
}
///////////////////////////////////////////////////////////////////////////////
// main function end //
///////////////////////////////////////////////////////////////////////////////
function getDocDementions(){
width=0;
height=0;
for (var i=0;i<documents.length;i++) { // look at open document sizes
if (documents.width > width) width=documents.width;
if (documents.height > height) height=documents.height;
}
return new Array(width, height); // return width and height
}
function closeAllButOne(){ // close all opened document except the newly created document
while (documents.length>1) {
app.activeDocument = documents[0];
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
Copy link to clipboard
Copied
This on I create a cleaner stack removed transparency and did not use the clipboard I duplicated the layers into the new document and cleaned up the file name and layer names.
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// Stack the open Document Visible layer composite into a new 300 DPI document
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
// ======================================================= */
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
var theFile = File.openDialog("Select your PDF file", "Select:*.pdf");
app.displayDialogs = DialogModes.NO;
var check = true;
var page = 1;
// define pdfopenoptions;
var pdfOpenOpts = new PDFOpenOptions;
pdfOpenOpts.antiAlias = true;
pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
pdfOpenOpts.cropPage = CropToType.MEDIABOX;
pdfOpenOpts.mode = OpenDocumentMode.RGB;
pdfOpenOpts.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them
while (check == true) {
try {
pdfOpenOpts.page = page;
// open;
var thePdf = app.open(theFile, pdfOpenOpts);
page++;
}
catch (e) { check = false };
};
if (documents.length >= 2) { stackpages(); }
else {
app.togglePalettes(); // toggle on palettes
alert("multiple Document are not open in Photoshop");
}
///////////////////////////////////////////////////////////////////////////////
// stack pages function //
///////////////////////////////////////////////////////////////////////////////
function stackpages() {
var orig_ruler_units = app.preferences.rulerUnits;
var orig_display_dialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS; // set the ruler units to PIXELS
app.displayDialogs = DialogModes.NO; // Set Dialogs off
newFile = theFile.name.substr(0,theFile.name.indexOf(".pdf")); //New File name
var doc = app.documents.add(app.activeDocument.width.value, app.activeDocument.height.value, app.activeDocument.resolution, newFile, NewDocumentMode.RGB, DocumentFill.TRANSPARENT ); // create a new document
//for (var i=0;i<documents.length-1;i++) { // stack a layer for the open document into the new document page 1 on botton
for (var i=documents.length-2;i>-1;i--) { // stack a layer for the open document into the new document page 1 on top
app.activeDocument = documents; // switch active document
var layerName = app.activeDocument.name; // get document name
executeAction( charIDToTypeID( "FltI" ), undefined, DialogModes.NO ); // Flatten remove transparency
var idDplc = charIDToTypeID( "Dplc" );
var desc259 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref24 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref24.putEnumerated( idLyr, idOrdn, idTrgt );
desc259.putReference( idnull, ref24 );
var idT = charIDToTypeID( "T " );
var ref25 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
ref25.putName( idDcmn, documents[documents.length-1].name );
desc259.putReference( idT, ref25 );
var idNm = charIDToTypeID( "Nm " );
desc259.putString( idNm, layerName );
var idVrsn = charIDToTypeID( "Vrsn" );
desc259.putInteger( idVrsn, 5 );
executeAction( idDplc, desc259, DialogModes.NO ); // Copy Layer to document
app.activeDocument = documents[documents.length-1]; // switch to newly created document
layerName = layerName.substr(0,layerName.indexOf(".pdf"));
app.activeDocument.activeLayer.name=layerName; // label layer with Document name
}
app.activeDocument = documents[documents.length-1]; // switch to newly created document
var layers = activeDocument.layers; // get layers
activeDocument.activeLayer = layers[layers.length-1] // Target Bottom Layer
activeDocument.activeLayer.remove(); // Remove original layer
while (documents.length>1) { // close all opened document except the newly created document
app.activeDocument = documents[0];
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
app.togglePalettes(); // toggle user's palettes back on
app.runMenuItem(charIDToTypeID(("FtOn"))); // fit the document to the screen
app.displayDialogs = orig_display_dialogs; // Reset display dialogs
app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings
}
Copy link to clipboard
Copied
That work's fine to create a single file with all of them in layers.. but I'm still not figuring out how to save them all as individual .tif files... this is a script that I will set up as a Hot folder... so I don't want to have to interact with it... just drop the file and it perform per my preset script...
Any ideas on how to do this... I looked at the default script that comes with PSD for exporting layers.. but it's over complicated with tons of options... I need a simpler version...
Thanks
Copy link to clipboard
Copied
You simply change the script to instead of creating a new layered document you use the for loop simply flatten to remove the transparency save your tiff to your hot folder and close nosave till all the open document are saved and closed.
Note: The way you open one page at a time will take forever when you process a PDF with a lots of pages. The more pages a PDF has the longer it take Photoshop to create the open page list. and when you open one page one at a time that list is built with each open. When you open a pdf via Photoshop UI you can select all the pages and Photoshop will open all selected pages much quicker. Each will still be s separate document. However If the PDF has hundreds of pages Photoshop seems to enter la la land after opening something like 75 Pages.
Copy link to clipboard
Copied
JJMack I understand what your saying about the time invloved with the multiple page .pdf... However, in my particular case.. I won't be dealing with more than 6-12 pages at most...
Also I'm a rookie at scripting... so Unfortunately, I don't follow what your suggesting for the repeat function... Can you give an example? - Thanks
Copy link to clipboard
Copied
I do not even know even JavaScript I just hack at it.
/* ==========================================================
// 2017 John J. McAssey (JJMack)
// save the open Document as tif
// The author accepts no liability for any problems arising from its use.
// ======================================================= */
// enable double-clicking from Mac Finder or Windows Explorer
#target photoshop // this command only works in Photoshop CS2 and higher
// bring application forward for double-click events
app.bringToFront();
var docPath = "~/Desktop/Illustrator Hot Folder/11 x 8.5 Zund-Proof-Hi-Res/PS";
if (documents.length >= 2) {
app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them
savepages(); // Opening via Photoshop UI is faster than doint it ona at a time
}
else {
var theFile = File.openDialog("Select your PDF file", "Select:*.pdf");
app.displayDialogs = DialogModes.NO;
var check = true;
var page = 1;
// define pdfopenoptions;
var pdfOpenOpts = new PDFOpenOptions;
pdfOpenOpts.antiAlias = true;
pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
pdfOpenOpts.cropPage = CropToType.MEDIABOX;
pdfOpenOpts.mode = OpenDocumentMode.RGB;
pdfOpenOpts.resolution = 300;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber = true;
app.togglePalettes(); // toggle off palettes so Photoshop will not need to update them
while (check == true) {
try {
pdfOpenOpts.page = page; // open a page at it slower the using Photoshop UI ans selection all the pages you want
var thePdf = app.open(theFile, pdfOpenOpts);
page++;
}
catch (e) { check = false };
};
if (documents.length >= 2) { savepages(); }
else {
app.togglePalettes(); // toggle on palettes
alert("multiple Document are not open in Photoshop");
}
}
///////////////////////////////////////////////////////////////////////////////
// save pages function //
///////////////////////////////////////////////////////////////////////////////
function savepages() {
var orig_ruler_units = app.preferences.rulerUnits;
var orig_display_dialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS; // set the ruler units to PIXELS
app.displayDialogs = DialogModes.NO; // Set Dialogs off
if(!(new Folder(docPath)).exists){ new Folder(docPath).create();}
tifOpts = new TiffSaveOptions() ;
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.IBM;
tifOpts.layers = false;
for (var i=documents.length-1;i>-1;i--) { // stack a layer for the open document into the new document page 1 on top
app.activeDocument = documents; // switch active document
var layerName = app.activeDocument.name; // get document name
newFile = app.activeDocument.name.substr(0,app.activeDocument.name.lastIndexOf(".pdf")); //New File name
executeAction( charIDToTypeID( "FltI" ), undefined, DialogModes.NO ); // Flatten remove transparency
app.activeDocument.saveAs((new File(docPath+"/"+newFile+".tif")), tifOpts, false)
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
app.togglePalettes(); // toggle user's palettes back on
app.displayDialogs = orig_display_dialogs; // Reset display dialogs
app.preferences.rulerUnits = orig_ruler_units; // reset units to original settings
}