Search code examples
photoshop-script

Photoshop scripting mantain same file name and whitespace


I'm trying to add a layer to some images and then save them.

The problem is that many images have whitespace in the name. When I try to save the whitespaces are replaced with "-", but I want mantain the same name.

For example an image called "Google Analytics.png" become "Google-Analytics.png". But I need to preserve the name without that extra "-".

My code:

function SavePNG(saveFile){
    var file = new File(saveFile);

    var pngOpts = new ExportOptionsSaveForWeb; 
    pngOpts.format = SaveDocumentType.PNG
    pngOpts.PNG8 = false; 
    pngOpts.transparency = false; 
    pngOpts.interlaced = false; 
    pngOpts.quality = 100;
    $.writeln(file)
    app.activeDocument.exportDocument(file,ExportType.SAVEFORWEB,pngOpts); 
}

Solution

  • Try using saveAs instead of exportDocument; it'll respect the name of the file you give it. The options are yours to change:

    var myfile = "C:\\temp\\Google Analytics.png";
    
    SavePNG(saveFile)
    
    function savePNG(saveFile)
    {
      // save out the image
      var pngFile = new File(filepath);
      pngSaveOptions = new PNGSaveOptions();
      pngSaveOptions.embedColorProfile = true;
      pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
      pngSaveOptions.matte = MatteType.NONE;
      pngSaveOptions.quality = 1;
    
     activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);
    }