Java Script to Save ai to PDF with password...
Hi folks. My goal is to have a script carry out the following functions:
1. grab the active document name
2. save as a PDF using the active doc name with password lock in the same directory the *.ai file is located
I have started fiddling with some code but I don't know what I'm doing...newbie.
Here is the code I'm currently using.
var curDoc = app.activeDocument;
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;
//var destName = "~/Desktop/Testpassword1.pdf";
var destName = targetFile
saveFileToPDF(destName);
//sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PDF', '~' );
//saveFileToPDF(destName);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if ( sourceFolder != null )
{
//files = new Array();
//fileType = '*.ai';
//fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
// Get all files matching the pattern
//files = sourceFolder.getFiles( fileType );
if ( files.length > 0 )
{
// Get the destination to save the files
destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PDF files.', '~' );
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files); // returns the document object
// Call function getNewName to get the name and file to save the pdf
targetFile = getNewName();
// Call function getPDFOptions get the PDFSaveOptions for the files
//pdfSaveOpts = getPDFOptions( );
// Save as pdf
sourceDoc.saveAs( targetFile, pdfSaveOpts );
sourceDoc.close();
}
alert( 'Saved' + destFolder );
}
else
{
alert( 'No matching files found' );
}
}
///++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function getNewName()
{
var ext, docName, newName, saveInFile, docName;
docName = sourceDoc.name;
ext = '.pdf'; // new extension for pdf file
newName = "";
for ( var i = 0 ; docName != "." ; i++ )
{
newName += docName;
}
newName += ext; // full pdf name of the file
// Create a file object to save the pdf
saveInFile = new File( destFolder + '/' + newName );
return saveInFile;
}
///++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function saveFileToPDF (dest) {
var doc = app.activeDocument;
if ( app.documents.length > 0 ) {
var saveName = new File ( dest );
saveOpts = new PDFSaveOptions();
saveOpts.compatibility = PDFCompatibility.ACROBAT5;
saveOpts.generateThumbnails = true;
saveOpts.optimization = true;
saveOpts.preserveEditability = true;
saveOpts.bleedOffsetRect = [2,2,2,2];
saveOpts.trimMarks = true;
//======================= COMPRESSION ===========================
saveOpts.colorCompression = CompressionQuality.JPEGMAXIMUM;
saveOpts.colorDownsamplingMethod = DownsampleMethod.BICUBICDOWNSAMPLE;
saveOpts.colorDownsampling = 300;
saveOpts.colorDownsamplingImageThreshold = 450;
//-----------------------------------------------------------------------------------------------------------
saveOpts.grayscaleCompression = CompressionQuality.JPEGMAXIMUM;
saveOpts.grayscaleDownsamplingMethod = DownsampleMethod.BICUBICDOWNSAMPLE;
saveOpts.grayscaleDownsampling = 300;
saveOpts.grayscaleDownsamplingImageThreshold = 450;
//-----------------------------------------------------------------------------------------------------------
saveOpts.monochromeCompression = MonochromeCompression.CCIT4;
saveOpts.monochromeDownsamplingMethod = DownsampleMethod.BICUBICDOWNSAMPLE;
saveOpts.monochromeDownsampling = 1200;
saveOpts.monochromeDownsamplingImageThreshold = 1800;
//==================== END OF COMPRESSION =======================
saveOpts.colorConversionID = ColorConversion.COLORCONVERSIONREPURPOSE;
saveOpts.colorDestinationID = ColorDestination.COLORDESTINATIONWORKINGCMYK;
///
saveOpts = new PDFSaveOptions();
saveOpts.requirePermissionPassword = true;
saveOpts.permissionPassword = "pink";
saveOpts.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128LOWRESOLUTION;
doc.saveAs( saveName, saveOpts );
}
}
saveOpts.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128LOWRESOLUTION;
doc.saveAs( saveName, saveOpts );
}
}