20 Replies Latest reply on Sep 15, 2015 6:39 AM by williamadowling

    JavaScript to prompt user to save as PDF

    subieguy2 Level 1

      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

        • 1. Re: JavaScript to prompt user to save as PDF
          Qwertyfly... Level 4

          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.

          • 2. Re: JavaScript to prompt user to save as PDF
            subieguy2 Level 1

            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

            • 3. Re: Re: JavaScript to prompt user to save as PDF
              Qwertyfly... Level 4

              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.

              • 4. Re: Re: Re: JavaScript to prompt user to save as PDF
                subieguy2 Level 1

                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);
                
                • 6. Re: Re: Re: JavaScript to prompt user to save as PDF
                  CarlosCanto Adobe Community Professional & MVP

                  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();
                  
                  • 7. Re: Re: Re: JavaScript to prompt user to save as PDF
                    Qwertyfly... Level 4

                    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.

                    • 8. Re: JavaScript to prompt user to save as PDF
                      CarlosCanto Adobe Community Professional & MVP

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

                      • 9. Re: Re: Re: JavaScript to prompt user to save as PDF
                        subieguy2 Level 1

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

                        • 10. Re: JavaScript to prompt user to save as PDF
                          CarlosCanto Adobe Community Professional & MVP

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

                          • 11. Re: Re: JavaScript to prompt user to save as PDF
                            Qwertyfly... Level 4

                            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.

                            • 12. Re: JavaScript to prompt user to save as PDF
                              CarlosCanto Adobe Community Professional & MVP

                              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.

                              • 13. Re: JavaScript to prompt user to save as PDF
                                CarlosCanto Adobe Community Professional & MVP

                                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

                                • 14. Re: JavaScript to prompt user to save as PDF
                                  Qwertyfly... Level 4

                                  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!

                                  • 15. Re: JavaScript to prompt user to save as PDF
                                    subieguy2 Level 1

                                    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!

                                    • 16. Re: JavaScript to prompt user to save as PDF
                                      CarlosCanto Adobe Community Professional & MVP

                                      ...and we appreciate it.

                                       

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

                                      • 17. Re: Re: Re: JavaScript to prompt user to save as PDF
                                        williamadowling Level 4

                                        ****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();

                                        • 18. Re: Re: Re: JavaScript to prompt user to save as PDF
                                          Qwertyfly... Level 4
                                          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.

                                          • 19. Re: Re: Re: JavaScript to prompt user to save as PDF
                                            CarlosCanto Adobe Community Professional & MVP

                                            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

                                            • 20. Re: Re: Re: JavaScript to prompt user to save as PDF
                                              williamadowling Level 4

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