/*************************************************************************************
Save a file as PDF - Michel Rohan - 30/12/2022
*************************************************************************************/
// ************************* BEGIN PROG ***************************************
#target photoshop // MAC Finder or WINDOWS Explorer
displayDialogs = DialogModes.NO; // Disables dialog boxes
var outFolder = Folder("");
var saveOptions = new PDFSaveOptions();
saveOptions.PDFCompatibility = PDFCompatibility.PDF15;
saveOptions.encoding = PDFEncoding.PDFZIP;
var fileFound = false;
// Go !
main();
if (fileFound) {
alert("End of script. Your files were saved in " + outFolder);
}
else {
alert("No photos found. Bye !");
}
function main() {
// The folder where the JSX file is
var myJSX = $.fileName;
var x1 = myJSX.length;
var script = myJSX.replace(/^.*[\\\/]/, '');
var x2 = script.length + 1;
// Creation of a save folder
var dossier = Folder(myJSX.substr(0,x1-x2));
var dossierIn = Folder.selectDialog("Sélectionnez le dossier contenant les photos à traiter. La sauvegarde se fera dans le dossier " + outFolder);
outFolder = Folder(dossier + "/PDF");
if (!outFolder.exists) outFolder.create();
var fileList = dossierIn.getFiles(/\.(jpg|)$/i);
// No photos found : stop
if (fileList.length==0)
{
fileFound = false;
return;
}
// Traitement
for (var i= 0;i<fileList.length;i++) {
// Sauvegarde au format PSD
var docRef = open(fileList);
var docName = docRef.name.substring( 0, docRef.name.indexOf('.') );
convertPDF();
docRef.saveAs(File(outFolder + '/' + docName + ".pdf"),saveOptions, true );
docRef.close(SaveOptions.DONOTSAVECHANGES);
}
fileFound = true;
}
function convertPDF() {
// =======================================================
var idsave = charIDToTypeID( "save" );
var desc9 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc10 = new ActionDescriptor();
var idpdfPresetFilename = stringIDToTypeID( "pdfPresetFilename" );
desc10.putString( idpdfPresetFilename, "Qualité supérieure" );
var idpdfCompatibilityLevel = stringIDToTypeID( "pdfCompatibilityLevel" );
var idpdfCompatibilityLevel = stringIDToTypeID( "pdfCompatibilityLevel" );
var idpdfonethree = stringIDToTypeID( "pdf13" );
desc10.putEnumerated( idpdfCompatibilityLevel, idpdfCompatibilityLevel, idpdfonethree );
var idpdfCompressionType = stringIDToTypeID( "pdfCompressionType" );
desc10.putInteger( idpdfCompressionType, 7 );
var idpdfIncludeProfile = stringIDToTypeID( "pdfIncludeProfile" );
desc10.putBoolean( idpdfIncludeProfile, false );
var idPhtP = charIDToTypeID( "PhtP" );
desc9.putObject( idAs, idPhtP, desc10 );
}