10 Replies Latest reply on Jan 27, 2016 9:47 AM by Silly-V

    Exporting to PNG, Spaces becomes Hyphen?

    Mr Clint Eastwood

      Hello!

       

      I have this beautiful function that export layers and stuf to PNG-files. It works great exept one thing. If i spaces in the filename it will convert the spaces to hyphen!

       

      For example. If i am working with C:\users\Clint\Clint Eastwood is so beautiful.ai. The script puts a file named Clint-Eastwood-is-so-beautiful.png in the folder C:\users\Clint.

       

      Why oh why the Hyphens?? I dont want them

       

      Any one have a good idea?

       

      
      fullPath= app.activeDocument.fullName.parent;
      docName = app.activeDocument.name.match(/^.*[^.ai]/i);
        
          exportFileToPNG24(fullPath + "\\" + docName + ".png");
      
      
      function exportFileToPNG24 (dest) {
                if ( app.documents.length > 0 ) { 
                var exportOptions = new ExportOptionsPNG24();
                var type = ExportType.PNG24;
                var fileSpec = new File(dest);
                alert(dest);
                exportOptions.antiAliasing = false;
                exportOptions.transparency = true;
                exportOptions.saveAsHTML = false;
                exportOptions.horizontalScale = 100;
                exportOptions.verticalScale = 100;
                app.activeDocument.exportFile( fileSpec, type, exportOptions );
                }
      }
      
      

       

      // Mr Clint

        • 1. Re: Exporting to PNG, Spaces becomes Hyphen?
          Muppet Mark Level 5

          Clint, you get the file parent then the name then concat the suffix… why not just

           

          File( yourSring.replace( /\.ai$/i, '.png' ) );

          • 2. Re: Exporting to PNG, Spaces becomes Hyphen?
            Cleanshooter21 Level 1

            Just some ideas you could try

             

            Type Cast:

             

            docName = String(docName);
            

             

            or Maybe even try

             

            var docName = String(docName).replace(" ", "%20");
            

             

            Not sure if it'll work or not .

            • 3. Re: Exporting to PNG, Spaces becomes Hyphen?
              Mr Clint Eastwood Level 1

              Thanks for the replies but it does not work. To be clear, its not the variables that mess the name up. It is the ExportAsPNG24 function.

               

              This code:

              exportFileToPNG24 ("C:\user\Clint Eastwood\A file with spaces.png")


              Will output a file named: A-file-with-spaces.png

               

              That is the problem and i cant find any one else with this problem?



              Any more ideas would be great!


              // Clint

              • 4. Re: Exporting to PNG, Spaces becomes Hyphen?
                Muppet Mark Level 5

                Looks like its part of the export… Tried and it does the same here… The only option is to let it do this then rename the file afterwards… Don't know if it's a bug or indended…

                 

                Try this:

                 

                #target illustrator
                
                exportPNG24();
                
                function exportPNG24() {
                
                    if ( app.documents.length == 0 ) { return; }
                
                    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                    
                    var opts = new ExportOptionsPNG24();
                    opts.antiAliasing = false;
                    opts.transparency = true;
                    opts.saveAsHTML = false;
                    opts.horizontalScale = 100;
                    opts.verticalScale = 100;
                
                    var pngFile = File( app.activeDocument.fullName.toString().replace( /\.ai$/i, '.png' ) );
                    
                    app.activeDocument.exportFile( pngFile, ExportType.PNG24 ,opts );
                    
                    var autoFile = File( pngFile.fsName.replace( /\s/g,  '-' ) );
                    
                    if ( autoFile.exists ) {
                        
                        $.writeln( 'yup' );
                    
                        autoFile.rename(  pngFile.name.replace( '-', ' ' ) );
                    
                    }
                    
                };
                
                • 5. Re: Exporting to PNG, Spaces becomes Hyphen?
                  Mr Clint Eastwood Level 1

                  Hey Muppet, do you work with stuf like this or do just think it is so fun to write?

                   

                  Anyway, you are awsome!

                  • 6. Re: Exporting to PNG, Spaces becomes Hyphen?
                    Muppet Mark Level 5

                    Both… My job requires a lot of workflow / automation routines… So I use script with AI, ID, BR & PS what ever I need to get the job done… Product catalouge stuff mostly. I do spend quite a bit of my own time learning this stuff and helping others too, kind of a hobby… Got to do something

                    • 7. Re: Exporting to PNG, Spaces becomes Hyphen?
                      scottRaysNI Level 1

                      Hi there,

                       

                      I am running into the same issue when I run a batch script to export files as PNG's. Unfortunately I need the spaces in the names because the developers have already established files names in their code.

                       

                      Our software has several files with space in their artboard names like the following:

                      Generic Sensor16x16

                      Generic Sensor24x24

                      Generic Sensor40x40

                       

                      I applied your code and I am unable to changes the names? After doing some digging, it appears the name following the URI encoding? Does that mean that the exported PNG names don't support spaces due to this encoding? (i have limited experience is this encoding stuff)

                       

                      Cheers!

                      Scott

                       

                      CODE:

                       

                      function saveFilePNG(document){

                       

                          // creates  exportOption object and sets variables inside object

                          var options = new ExportOptionsPNG24();

                          options.artBoardClipping = true;

                          options.transparency = true;

                        

                          for (var i = 0 ;  i < document.artboards.length; i++){

                                //    sets active artboard to activeArtboardIndex parameter

                                 document.artboards.setActiveArtboardIndex(i);

                        

                                //creates new file object, then assigns path using active artboard name

                                 var pngFile = File (destFolder + '/'+ document.artboards[i].name);

                                

                                 //exports current artboard and layer by layer

                                 document.exportFile(pngFile, ExportType.PNG24, options);

                       

                                 var autoFile = File(pngFile.fsName.replace( /\s/g,  '-' ) );

                                

                                 if ( autoFile.exists ) {                      

                                          $.writeln( 'yup' );                  

                                          autoFile.rename( pngFile.name.replace( '-', '' ) );              

                                  }        

                                  savedPNGFullFilePath.push( destFolder + '/'+ document.artboards[i].name+'.png'); //stores each PNG full path

                          }

                      };

                      • 8. Re: Exporting to PNG, Spaces becomes Hyphen?
                        Silly-V Adobe Community Professional

                        Can you see if it will work when you change autoFile.rename( pngFile.name.replace( '-', '' ) );        to              autoFile.rename( pngFile.name.replace( /-/g, ' ' ) );       ?

                        • 9. Re: Exporting to PNG, Spaces becomes Hyphen?
                          scottRaysNI Level 1

                          Hi there,

                           

                          Thanks for helping out. I pasted the exact code and was unsuccessful. Curious if you were successful running my code?

                           

                          Scott

                          • 10. Re: Exporting to PNG, Spaces becomes Hyphen?
                            Silly-V Adobe Community Professional

                            Yea, on closer inspection, it is apparent that the pngFile would never have the '-' dash to be replaced with a space, since the pngFile is a variable made from the original artboard name.

                            I made the following edits which put all the artboard names into an array and then rename all of the exported files accordingly. Using this, it would be theoretically possible to export files with arbitrary names (as long as they are unique) and rename them with the artboard names which can be totally different.

                            #target illustrator-19
                            function test(){
                                function saveFilePNG(document){
                                    var destFolder = Folder(document.path);
                                    var namesArr = [];
                                    var savedPNGFullFilePath = [];
                                    // creates  exportOption object and sets variables inside object
                                    var options = new ExportOptionsPNG24();
                                    options.artBoardClipping = true;
                                    options.transparency = true;
                                 
                                    for (var i = 0 ;  i < document.artboards.length; i++){
                                        //    sets active artboard to activeArtboardIndex parameter
                                        document.artboards.setActiveArtboardIndex(i);
                            
                                        //creates new file object, then assigns path using active artboard name
                                        var pngFile = File (destFolder + '/'+ document.artboards[i].name);
                                       
                                        //exports current artboard and layer by layer
                                        document.exportFile(pngFile, ExportType.PNG24, options);
                                        var autoName = document.artboards[i].name.replace(/\s/g, "-");
                                       
                                        namesArr.push(document.artboards[i].name);
                                        savedPNGFullFilePath.push(destFolder + '/'+ autoName +'.png'); //stores each PNG full path
                                    }
                               
                                    var existFile;
                                    for (var i = 0 ;  i < savedPNGFullFilePath.length; i++){
                                        existFile = File(savedPNGFullFilePath[i]);
                                        if(existFile.exists){
                                            existFile.rename(namesArr[i] + ".png");
                                        }
                                    }
                                };
                            
                            
                                saveFilePNG(app.activeDocument);
                               
                            }
                            test();