Skip to main content
subieguy2
Inspiring
October 22, 2014
Answered

JavaScript to prompt user to save as PDF

  • October 22, 2014
  • 1 reply
  • 10729 views

I am making changes with code to the file. I want the user to be prompted to save as a .ai file first. I have that down. Now I am wanting to have the user immediately be prompted to save as a pdf with only the optimize for fast web view option selected. I believe it is an adobe preset of Smallest File Size.

The reason for wanting the prompt is because I want to save the files in 2 different locations.

Here is my code so far.....

var doc = app.activeDocument;

// Save as .ai file 

var fileName = doc.fullName; 

var thisFile = new File(fileName);

var saveFile = thisFile.saveDlg();

doc.saveAs (saveFile);

// Save as .pdf file

var pdfSaveOptions = new PDFSaveOptions(); 

pdfSaveOptions.pDFXStandard=PDFXStandard.PDFXNONE; 

pdfSaveOptions.compatibility = PDFCompatibility.ACROBAT5; 

pdfSaveOptions.preserveEditability = false; 

 

var pdfFile = new File(fileName); 

doc.saveAs(pdfFile, pdfSaveOptions);

AI CS4 Windows 7 64bit

This topic has been closed for replies.
Correct answer Qwertyfly___

is the folder structure existing already?

if so, things are simple.

if the job number and name are in the file the you could pull that from the doc rather then manual entry at the prompts.

(please note this has no testing to check folders exist etc...)

var job = prompt("Enter Job Name");

var num = prompt("Enter Job Number");

aiFile = "D:\\"+job+"\\"+num+"\\AI\\Schematic.ai"

pdfFile = "D:\\"+job+"\\"+num+"\\PDF\\Schematic.pdf"

var newaiFile = new File(aiFile);

var doc = app.activeDocument;

doc.saveAs (newaiFile);

var pdfOpts = new PDFSaveOptions();   

pdfOpts.pDFXStandard=PDFXStandard.PDFXNONE;

pdfOpts.compatibility = PDFCompatibility.ACROBAT5;   

pdfOpts.preserveEditability = false;

var newpdfFile = new File(pdfFile);   

doc.saveAs(newpdfFile, pdfOpts);

if you want the script to create the folders then its a little harder.

Javascript will not do this and you need some other work around.

something like this...

if (Folder(qfolder).exists){

app.activeDocument.saveAs( saveName, saveOpts );

}else{

           //alert("about to try bat");

           batpath= 'call "C:\\Adobe Scripts\\MakeDirectory.bat"';

            battemp = new File("C:\\Adobe Scripts\\tempBAT.bat");

            battemp.open("w");

            battemp.writeln(batpath + " " + qfolder);

            battemp.close();

            battemp.execute();

         //alert("Had to Create Folder, Please press OK to continue...");

            $.setTimeout = function(func, time) {

                    $.sleep(time);

                    func();

            };

         $.setTimeout(function(){ app.activeDocument.saveAs( saveName, saveOpts )},200);

         }

}

the MakeDirectory.bat is just:

ECHO OFF

CLS

IF %1=="" GOTO BLANK

SET savepath=%*

MKDIR %savepath%

GOTO FINISH

:Blank

ECHO No Folder provided

GOTO FINISH

:FINISH

the tempBat.bat is used to call the MakeDirectory.bat with the folder name as an argument.

1 reply

Qwertyfly___
Legend
October 22, 2014

Do you know where the save location will be?

I have a save script I use 50 times a day that pulls file name from doc,

then from that saves the file in art folder under A-Z folder listing and creates another  subfolder in there of file name if does not exist already.

then you dont need the dialogue and user input.

subieguy2
subieguy2Author
Inspiring
October 23, 2014

The folder location changes based on the job number. For instance:

Job KENR5632-01 is saved on my D drive in a folder NGS Jobs.

Inside that folder there are 2 folders AI and PDF

When I do the job for KENR5632-01 I save a file called Schematic.ai in the AI folder and Schematic.pdf in the PDF folder.

The format is always the same the only thing that changes is the job number (KENR5632-01)

Qwertyfly___
Qwertyfly___Correct answer
Legend
October 24, 2014

is the folder structure existing already?

if so, things are simple.

if the job number and name are in the file the you could pull that from the doc rather then manual entry at the prompts.

(please note this has no testing to check folders exist etc...)

var job = prompt("Enter Job Name");

var num = prompt("Enter Job Number");

aiFile = "D:\\"+job+"\\"+num+"\\AI\\Schematic.ai"

pdfFile = "D:\\"+job+"\\"+num+"\\PDF\\Schematic.pdf"

var newaiFile = new File(aiFile);

var doc = app.activeDocument;

doc.saveAs (newaiFile);

var pdfOpts = new PDFSaveOptions();   

pdfOpts.pDFXStandard=PDFXStandard.PDFXNONE;

pdfOpts.compatibility = PDFCompatibility.ACROBAT5;   

pdfOpts.preserveEditability = false;

var newpdfFile = new File(pdfFile);   

doc.saveAs(newpdfFile, pdfOpts);

if you want the script to create the folders then its a little harder.

Javascript will not do this and you need some other work around.

something like this...

if (Folder(qfolder).exists){

app.activeDocument.saveAs( saveName, saveOpts );

}else{

           //alert("about to try bat");

           batpath= 'call "C:\\Adobe Scripts\\MakeDirectory.bat"';

            battemp = new File("C:\\Adobe Scripts\\tempBAT.bat");

            battemp.open("w");

            battemp.writeln(batpath + " " + qfolder);

            battemp.close();

            battemp.execute();

         //alert("Had to Create Folder, Please press OK to continue...");

            $.setTimeout = function(func, time) {

                    $.sleep(time);

                    func();

            };

         $.setTimeout(function(){ app.activeDocument.saveAs( saveName, saveOpts )},200);

         }

}

the MakeDirectory.bat is just:

ECHO OFF

CLS

IF %1=="" GOTO BLANK

SET savepath=%*

MKDIR %savepath%

GOTO FINISH

:Blank

ECHO No Folder provided

GOTO FINISH

:FINISH

the tempBat.bat is used to call the MakeDirectory.bat with the folder name as an argument.