Skip to main content
Known Participant
September 12, 2012
Answered

Exporting to PNG, Spaces becomes Hyphen?

  • September 12, 2012
  • 3 replies
  • 3122 views

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

This topic has been closed for replies.
Correct answer Muppet Mark

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( '-', ' ' ) );

   

    }

   

};

3 replies

scottr12917177
Inspiring
January 27, 2016

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.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.name+'.png'); //stores each PNG full path

    }

};

Silly-V
Legend
January 27, 2016

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

scottr12917177
Inspiring
January 27, 2016

Hi there,

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

Scott

Cleanshooter21
Participating Frequently
September 12, 2012

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 .

Known Participant
September 13, 2012

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

Inspiring
September 12, 2012

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

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