12 Replies Latest reply on Aug 23, 2012 9:28 AM by JamesRLamar

    Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically

    JamesRLamar Level 1

      I am trying to do as the title suggests, but I can't find a way to specify the folder I want the script to look at and I can't get it to open/save/close the files without user interaction. Another way to say it is that I need this to be itterative as the PC I'm using cannot handle too many open files at once.

       

      On a side note, I can't even find the documentation. Example, what arguments does Folder.selectDialog() accept? Where is the documentation for that?

       

      Thank peeps!

       

      Using CS6 trying to convert EPS to SVG

        • 1. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
          Larry G. Schneider Adobe Community Professional & MVP

          Here's a copy of a SaveAs which you should be able to modify

          /**********************************************************
          
          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. 
          
          *********************************************************/
          
          /**********************************************************
          
          SaveAsAI12.jsx
          
          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, illustratorSaveOpts;
          
          // Select the source folder.
          sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to AI', '~' );
          
          // 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 AI files.', '~' );
                  for ( i = 0; i < files.length; i++ )
                  {
                      var optRef = new OpenOptions();
                      optRef.updateLegacyText = true;
          
          
                      sourceDoc = app.open(files[i], DocumentColorSpace.CMYK, optRef); // returns the document object
                                              
                      // Call function getNewName to get the name and file to save the ai
                      targetFile = getNewName();
                      
                      // Call function getIllustratorOptions get the illustratorSaveOptions for the files
                      illustratorSaveOpts = getIllustratorOptions( );
                      
                      // Save as AI
                      sourceDoc.saveAs( targetFile, illustratorSaveOpts );
                      
                      sourceDoc.close();
                  }
                  alert( 'Files are saved as AI 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 = '.ai'; // new extension for AI file
              newName = "";
                  
              for ( var i = 0 ; docName[i] != "." ; i++ )
              {
                  newName += docName[i];
              }
              newName += ext; // full AI name of the file
              
              // Create a file object to save the ai file
              saveInFile = new File( destFolder + '/' + newName );
              
          
              return saveInFile;
          }
          
          
          
          
          /*********************************************************
          
          getIllustratorOptions: Function to set the AI saving options of the 
          files using the illustratorSaveOptions object.
          
          **********************************************************/
          
          function getIllustratorOptions()
          {
              
              // Create the PDFSaveOptions object to set the AI options
              var illustratorSaveOpts = new IllustratorSaveOptions();
              
              // Setting IllustratorSaveOptions properties. 
              illustratorSaveOpts.embedLinkedFiles = true;
              illustratorSaveOpts.fontSubsetThreshold = 0.0
              illustratorSaveOpts.pdfCompatible = true
              illustratorSaveOpts.compatibility = Compatibility.ILLUSTRATOR14
              illustratorSaveOpts.embedICCProfile =  false
          
              return illustratorSaveOpts;
          }
          

          It will take a little to replace some of the lines but should get you started. If you uncomment the line to limit user interaction, then you will probably need to restore it at the end of the script.

          • 2. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
            Muppet Mark Level 5

            Where are you looking for… Folder.selectDialog( ' Some String' );

             

            Folder selectDialog (prompt: string)

            Opens the built-in platform-specific file-browsing dialog, and creates a new File or Folder object for the selected file or folder.

            Differs from the object method selectDlg() in that it does not preselect a folder.

            • If the user clicks OK, returns a File or Folder object for the selected file or folder.

            • If the user cancels, returns null.

            1 person found this helpful
            • 3. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
              JamesRLamar Level 1

              That's good info @Muppet, but "where" do I find that kind of info?

               

              selectDlg() sounds like it's one step closer to being able to select a default folder, but want to eliminate all user interaction.

               

              I tried specifying the destFolder with an absolute path, but the getTargetFile throws an error if I do so. My thoughts are that I might not be using the correct syntax to specify the path, but I can't find any documentation that defines methods like that.

              • 4. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                JamesRLamar Level 1

                I'll see what I can do with this and report back. Thanks

                • 5. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                  Muppet Mark Level 5

                  Well each app has it's own DOM and scripting guide depending on the language you choose… In Extension to that if using Adobe's ExtendScript there is also the OMV ( object model viewer ) in the toolkit/debugger or a PDF if you prefer installed in the toolkit folder JavaScript Tools Guide CS5.pdf hope that helps you out… Also see Jong's helpers very fast and friendly too…

                  • 6. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                    Muppet Mark Level 5

                    If you don't want to UI select anything you could just…

                     

                     

                    var inFolder = Folder( Folder.desktop + '/AI JUNK' )
                    
                    var files = inFolder.getFiles( /\.ai$/i );
                    
                    alert( files.join( '\r' ) );
                    
                    1 person found this helpful
                    • 7. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                      JamesRLamar Level 1

                      DIdn't realize that was the documentation, but that's the stuff I was looking for. Much appreciated.

                      • 8. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                        JamesRLamar Level 1

                        Here is what I have so far. I think I'm close I just need to figure out how to open the documents in the app and close them after converting them so only one document is open at a time. My goal is to run this program over night since there are thousands of files to convert. Thank you all for your help!

                         

                        app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

                         

                         

                        try {

                                  if (app.documents.length > 0 ) {

                         

                         

                                            // Get the folder to save the files into

                                var inFolder = Folder( Folder.desktop + '/temp' )

                         

                                var files = inFolder.getFiles( /\.eps$/i );

                         

                                            var destFolder = null;

                                            destFolder = '~/Desktop/temp';

                         

                         

                                            if (destFolder != null) {

                                                      var options, i, sourceDoc, targetFile;

                         

                                                // Get the SVG options to be used.

                                                      options = this.getOptions();

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

                         

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

                         

                                          //THIS IS WHAT I NEED HELP WITH

                                          var docRef = open(files[i]);

                         

                                          sourceDoc = app.documents[i]; // returns the document object

                         

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

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

                         

                                                                // Save as SVG

                                                                sourceDoc.exportFile(targetFile, ExportType.SVG, options);

                                                                // Note: the doc.exportFile function for SVG is actually a Save As

                                                                // operation rather than an Export, that is, the document's name

                                                                // in Illustrator will change to the result of this call.

                                                      }

                                                      alert( 'Documents saved as SVG' );

                                            }

                                  }

                                  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 ExportOptionsSVG object

                        */

                        function getOptions()

                        {

                                  // Create the required options object

                                  var options = new ExportOptionsSVG();

                                  // See ExportOptionsSVG in the JavaScript Reference for available options

                         

                                  // Set the options you want below:

                         

                                  // For example, uncomment to set the compatibility of the generated svg to SVG Tiny 1.1

                                  // options.DTD = SVGDTDVersion.SVGTINY1_1;

                         

                                  // For example, uncomment to embed raster images

                                  // options.embedRasterImages = 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;

                        }

                        • 9. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                          Larry G. Schneider Adobe Community Professional & MVP

                          try changing this

                           

                          var docRef = open(files[i]);

                           

                          sourceDoc = app.documents[i];

                           

                          to

                           

                          sourceDoc = app.open(files[i]);

                          1 person found this helpful
                          • 10. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                            JamesRLamar Level 1

                            That worked in opening and converting the image!

                             

                            Next I tried app.close(files[i]); at the end of the loop , but I got an error that said it was not a function. I tried finding documentation for a list of allow methods on the app object, but couldn't find any. I must really be looking in the wrong place for information.

                             

                            Any ideas?

                             

                            Thanks

                            • 11. Re: Trying to modify SaveDocsAsSVG to open and save all files within a directory automatically
                              Larry G. Schneider Adobe Community Professional & MVP

                              Try using sourceDoc.close(SaveOptions.DONOTSAVECHANGES); after your sourceDoc.exportFile line.

                              1 person found this helpful