Skip to main content
Participant
July 19, 2007
Question

Tweaking the SaveDocsAsPDF script

  • July 19, 2007
  • 35 replies
  • 14666 views
Hello. Absolute scripting newbie here; will someone take pity...?

I am looking at the SaveDocsAsPDF script. It works for our purposes except I would like it to save PDFs using a Preset we created called "SGS PDFs. (We have trouble with people in our office forgetting to choose this setting, so I thought a script might be the answer.)

I found a posting on this forum where someone wanted to save files as small as possible, and got some clues from that. So I left everything as is in the original script, except I added this first line in the function:
{var NamePreset = '[SGS PDFs]';

Then after the line:
var options = new PDFSaveOptions();

I added:
options.PDFPreset=NamePreset;

I thought this would result in a script that does the same thing I now do "manually". However, if I save my document "manually" using File/Save As/ Adobe PDF, and choosing the SGS PDFs preset (and changing nothing else), I get a 716 KB file. But if I use my script, I get a 4.9 MB file. So it appears that what I added is NOT making the script use my preset settings.

So my question is: Is there something REALLY basic missing from my script, or... In order to get a script that accomplishes everything already determined in the Preset, do I manually have to list all those settings as options?

I think I can do the latter if I need to if I just study some of the other scripts... unless there's a shortcut!
This topic has been closed for replies.

35 replies

Larry G. Schneider
Community Expert
Community Expert
November 1, 2007
OK, in message 9 of this thread is a version of the script which will process a folder of PDFs. Look for the line which has var NamePreset = 'pdf map'; and change it to var NamePreset = '[Smallest File Size]';

See if that works better.
Participant
November 1, 2007
I did just that and Illustrator tells me:

Error 5: Unterminated comment.
Line: 192
-> }

Am I missing something? I've never messed with scripting of any kind before.
Larry G. Schneider
Community Expert
Community Expert
November 1, 2007
Use the above but change the line

var NamePreset = 'SGS PDFs';

to read

var NamePreset = '[Smallest File Size]';
Participant
November 1, 2007
Can someone point me in the direction of the Smallest File Size PDF script?
Participant
September 18, 2007
For all who have been waiting, this script APPEARS to work. (I don't guarantee anything!)

The two lines I added are...

var NamePreset = 'SGS PDFs';

options.pDFPreset=NamePreset;

... assuming that the user has a preset called "SGS PDFs".

Hope this helps. If it doesn't work, I am ...quite useless as an answer person...

-----------------------------

/** Saves every document open in Illustrator

as a PDF file in a user specified folder, using

a preset called SGS PDFs

*/

// Main Code [Execution of script begins here]

try {

// uncomment to suppress Illustrator warning dialogs

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

if (app.documents.length > 0 ) {

// Get the folder to save the files into

var destFolder = null;

destFolder = Folder.selectDialog( 'Select folder for PDF files.', '~' );

if (destFolder != null) {

var options, i, sourceDoc, targetFile;



// Get the PDF options to be used

options = this.getOptions();

// You can tune these by changing the code in the getOptions() function.



for ( i = 0; i < app.documents.length; i++ ) {

sourceDoc = app.documents; // returns the document object



// Get the file to save the document as pdf into

targetFile = this.getTargetFile(sourceDoc.name, '.pdf', destFolder);



// Save as pdf

sourceDoc.saveAs( targetFile, options );

}

alert( 'Documents saved as PDF' );

}

}

else{

throw new Error('There are no document open!');

}

}

catch(e) {

alert( e.message, "Script Alert", true);

}

/** Returns the options to be used for the generated files.

@return PDFSaveOptions object

*/

function getOptions()

{ var NamePreset = 'SGS PDFs';

// Create the required options object

var options = new PDFSaveOptions();

options.pDFPreset=NamePreset;

// See PDFSaveOptions in the JavaScript Reference for available options



// Set the options you want below:

// For example, uncomment to set the compatibility of the generated pdf to Acrobat 7 (PDF 1.6)

// options.compatibility = PDFCompatibility.ACROBAT7;



// For example, uncomment to view the pdfs in Acrobat after conversion

// options.viewAfterSaving = true;



return options;

}

/** Returns the file to save or export the document into.

@param docName the name of the document

@param ext the extension the file extension to be applied

@param destFolder the output folder

@return File object

*/

function getTargetFile(docName, ext, destFolder) {

var newName = "";

// if name has no dot (and hence no extension),

// just append the extension

if (docName.indexOf('.') < 0) {

newName = docName + ext;

} else {

var dot = docName.lastIndexOf('.');

newName += docName.substring(0, dot);

newName += ext;

}



// Create the file object to save to

var myFile = new File( destFolder + '/' + newName );



// Preflight access rights

if (myFile.open("w")) {

myFile.close();

}

else {

throw new Error('Access is denied');

}

return myFile;

}
Participant
September 14, 2007
I'm sorry I didn't get back to you folks! Right after my last posting, my hard drive crashed--and of course I hadn't backed up the script yet and hadn't yet distributed it to anyone else, so it was gone...(I had my Illustrator preferences and keyboard shortcuts backed up, but forgot the scripting folder)... I haven't had a chance to redo the script. I hope Larry's posting has helped you; I'll set a reminder to recreate that script in the next week or so (and just you watch, it probably won't work for me this time)!
Larry G. Schneider
Community Expert
Community Expert
September 13, 2007
/**********************************************************

ADOBE SYSTEMS INCORPORATED
Copyright 2005 Adobe Systems Incorporated
All Rights Reserved

NOTICE: Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it.
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.

*********************************************************/

/**********************************************************

Save as PDFs.js

DESCRIPTION

This sample gets files specified by the user from the
selected folder and batch processes them and saves them
as PDFs in the user desired destination with the same
file name.

**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PDF', '~' );

// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
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( 'Files are saved as PDF in ' + destFolder );
}
else
{
alert( 'No matching files found' );
}
}

/*********************************************************

getNewName: Function to get the new file name. The primary
name is the same as the source file.

**********************************************************/

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;
}

/*********************************************************

getPDFOptions: Function to set the PDF saving options of the
files using the PDFSaveOptions object.

**********************************************************/

function getPDFOptions()
{ var NamePreset = 'pdf map';

// Create the PDFSaveOptions object to set the PDF options
var pdfSaveOpts = new PDFSaveOptions();

// Setting PDFSaveOptions properties. Please see the JavaScript Reference
// for a description of these properties.
// Add more properties here if you like
pdfSaveOpts.acrobatLayers = true;
pdfSaveOpts.colorBars = false;
pdfSaveOpts.colorCompression = CompressionQuality.AUTOMATICJPEGHIGH;
pdfSaveOpts.compressArt = true; //default
pdfSaveOpts.embedICCProfile = false;
pdfSaveOpts.enablePlainText = true;
pdfSaveOpts.generateThumbnails = true; // default
pdfSaveOpts.optimization = true;
pdfSaveOpts.pageInformation = true;
pdfSaveOpts.pDFPreset = NamePreset;

// uncomment to view the pdfs after conversion.
// pdfSaveOpts.viewAfterSaving = true;


return pdfSaveOpts;
}
Participant
September 13, 2007
Yes please, don't leave us to find the pieces of the jigsaw.

May we have the entire working script please?

I've searched my HardDrive and I can't find where the scripts are held, where are they stored?
August 30, 2007
Wendy
If youre a newbie than I must be a real dummy. I tried replacing those and the script is not working now...can you show the whole script? Can this be used for AiCS3 as well?
thx
paulw
Participant
August 14, 2007
Oops, I'm sorry. I guess I thought the fix was self-explanitory. (Sorry for the delay in response; I was on vacation). I implemented the corrections offered by Ang$t and Larry Schneider. For the two lines I had added, which were:

{var NamePreset = '[SGS PDFs]';
options.PDFPreset=NamePreset;

... I dropped the square brackets and made the "p" lower case:
{var NamePreset = 'SGS PDFs';
(options.pDFPreset)

That's all I did. I made no other changes to the original supplied script. I can't help with Alisdair Troup's error, can someone else?