• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

JavaScript to prompt user to save as PDF

Engaged ,
Oct 22, 2014 Oct 22, 2014

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

9.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Oct 23, 2014 Oct 23, 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.save

...

Votes

Translate

Translate
Adobe
Guide ,
Oct 22, 2014 Oct 22, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 23, 2014 Oct 23, 2014

Copy link to clipboard

Copied

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)

Capture.JPG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 23, 2014 Oct 23, 2014

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 24, 2014 Oct 24, 2014

Copy link to clipboard

Copied

THANK YOU SO MUCH!!!!

The folder structure does already exist so I modified the code just a bit more to eliminate the prompts.

Final code looks like this....

var doc = app.activeDocument;

var fileName = doc.name;

var jobName = (fileName).substr(0, 11);

aiFile = "D:\\NGS Jobs\\" + jobName + "\\AI\\Schematic.ai"

pdfFile = "D:\\NGS Jobs\\" + jobName + "\\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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 24, 2014 Oct 24, 2014

Copy link to clipboard

Copied

Glad I could help. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 24, 2014 Oct 24, 2014

Copy link to clipboard

Copied

imagecollection wrote:

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.

just for completeness, Javascript can create folders

var path = 'c:/temp/just created';

var folder = Folder(path);

folder.create();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 24, 2014 Oct 24, 2014

Copy link to clipboard

Copied

hey Carlos, always good to have your input.

I had a lot of trouble getting the folder creation to work when I wrote my script.

Or I would not have gone to all the effort of doing the batch file workaround.

Maybe it has something to do with the work server I am saving to.

I will have to have a play when I go back in on Monday.

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 24, 2014 Oct 24, 2014

Copy link to clipboard

Copied

oh ok, I have not tried creating folders on a server, I'll check and post back on Monday as well.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2014 Oct 27, 2014

Copy link to clipboard

Copied

I was able to create folders on a server using the code above without any issues.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 27, 2014 Oct 27, 2014

Copy link to clipboard

Copied

After some fiddling I got it to work.

all the way through my script I have been using the below format:

"\\\\Server\\folder\\file"

escaping every Backslash.

but using forward slash allows it to create a folder:

"//Server/folder/file"

So now I'm confused as to why the below works (providing the folder exists).

yet when creating a folder it fails...

path = "\\\\Server\\folder\\file";

saveName = new File(path);

saveOpts = new PDFSaveOptions();

saveOpts.compatibility = PDFCompatibility.ACROBAT5;

saveOpts.generateThumbnails = true;

saveOpts.preserveEditability = true;

app.activeDocument.saveAs( saveName, saveOpts );

I did write this script for cs4. so that was a while ago.

I have no idea why I did not try this back then.

Or where I got the idea to escape the backslashes instead of using forward slashes.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2014 Oct 27, 2014

Copy link to clipboard

Copied

it works with forward or backward slashes with escape character, from the documentation

Character interpretation in paths

There are some platform differences in how pathnames are interpreted:

   On Windows and Mac OS, path names are not case sensitive. In UNIX, paths are case sensitive.

   On Windows, both the slash ( / ) and the backslash (\) are valid path element separators. Backslash is

the escape character, so you must use a double backslash (\\) to indicate the character.

   On Mac OS, both the slash ( / ) and the colon (:) are valid path element separators.

If a path name starts with two slashes (or backslashes on Windows), the first element refers to a remote

server. For example, //myhost/mydir/myfile refers to the path /mydir/myfile on the server myhost.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 27, 2014 Oct 27, 2014

Copy link to clipboard

Copied

on a totally unrelated note...I have never seen so many green checks on the first page, 10 out of 20...that's encouraging.

greenPosts.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 29, 2014 Oct 29, 2014

Copy link to clipboard

Copied

6 of those are mine. lol

I appreciate this community more than you guys know. When someone more knowledgeable is willing to take the time to help educate others that is awesome!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 29, 2014 Oct 29, 2014

Copy link to clipboard

Copied

...and we appreciate it.

thanks for marking your posts, it makes a big difference when managing and navigating the forums.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 27, 2014 Oct 27, 2014

Copy link to clipboard

Copied

well now i'm even more confused.

I just ran a more basic test and both worked.

As I stated in the post about 'PARM' error, I am going to re write this script.

must be an issue with the path, if I hardcode the whole path in a var it works.

even though the hardcoded and script built paths look identical in an alert.

I refuse to put more thought into this until I have re-written this code!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 27, 2014 Oct 27, 2014

Copy link to clipboard

Copied

Thanks for posting this Carlos! I will definitely keep this in my bag of tricks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2015 Sep 14, 2015

Copy link to clipboard

Copied

****EDIT****

turns out if i hard code the file path everything works.. is there any way to use a relative file path? This script will be used with my whole team, and they'll be running the script right from our network so it doesn't work if i have to hard code the path to each user's desktop folder...

****EDIT****

CarlosCanto‌, could you please help me with using this folder creation method with a conditional??

I'm trying to merge what you've posted here with what Qwertyfly... was doing above. I cannot seem to create the folder if it doesn't exist. Normally I would use a try/catch for something like this.. ie:

try{

    var dest = Folder.desktop.fullName + "Desktop%20Folder";

}

catch(e){

    var path = Folder.desktop.fullName;

    var newFolder = Folder(path + "/NewFolderName/");

    newFolder.create();

    var dest = newFolder;

}

unfortunately this doesn't work because you can set the var dest = a folder that doesn't exist...

here's what I have currently. If the folder exists currently, then everything works fine. If it doesn't exist already, it appends the name of the folder i tried to create to the filename and saves directly to the desktop. What am i doing wrong here?

function saveDoc(){

    var dest = Folder.desktop.fullName + "/Today's%20Temp/";

    if(app.activeDocument.name.substring(0,2) == "Un"){

        var oN = prompt("Enter Order Number", "1234567");

        var fileName = oN + " Master";

    }

    else{

        var fileName = app.activeDocument.name;

    }

    if(dest.exists){

        var thisFile = new File(dest + fileName);

        app.activeDocument.saveAs(thisFile);

    }

    else{

        dest = Folder.desktop.fullName;

        var folder = Folder(dest + "/Todays%20Temp/");

        folder.create();

        dest = folder;

        var thisFile = new File(dest + fileName);

        app.activeDocument.saveAs(thisFile);

    }

  

}

saveDoc();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 14, 2015 Sep 14, 2015

Copy link to clipboard

Copied

var myFolder = Folder("~/Desktop/~~~Crap/tst");

var newFolder = new Folder(myFolder + "/newfold/");

if(!newFolder.exists){

    newFolder.create();

    alert("folder created");

}else{

    alert("folder already exists");

}

app.activeDocument.saveAs(new File(newFolder + "/" + app.activeDocument.name));

this works fine for me

maybe you just need to add a    + "/" +   between folder and file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2015 Sep 15, 2015

Copy link to clipboard

Copied

LATEST

Working beautifully. I knew it was going to be a syntax issue. Thanks again guys.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2015 Sep 14, 2015

Copy link to clipboard

Copied

quertyfly is correct, all you need is the "/"

var thisFile = new File(dest + "/" + fileName);

you don't need the "/" at the end of the folder names

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines