Search code examples
photoshop-script

exportDocument() 'destination folder does not exist' error


I'm trying to make a script in photoshop that will modify some layers and than export them as a PNG image. I've copied the following code from another place:

function SavePNG(saveFile){
    var pngOpts = new ExportOptionsSaveForWeb; 
    pngOpts.format = SaveDocumentType.PNG
    pngOpts.PNG8 = false; 
    pngOpts.transparency = true; 
    pngOpts.interlaced = true; 
    pngOpts.quality = 100;
    activeDocument.exportDocument(saveFile,ExportType.SAVEFORWEB,pngOpts);
}

The function export the active document of photoshop to the file specified by the saveFile parameter.

It's working fine with simple paths like "C:\images\result.png" but when trying with different paths like "~/Desktop/" or paths with some special characters the file isn't exported, and a "destination folder does not exist" error message appears.

Any idea how can I solve it?


Solution

  • Well, I'm not sure why this is occur but you could try the following modification:

    function SavePNG(saveFile){
        var tmpFile = "./tmp.png";
        tmpFile = new File(tmpFile);
        var pngOpts = new ExportOptionsSaveForWeb; 
        pngOpts.format = SaveDocumentType.PNG
        pngOpts.PNG8 = false; 
        pngOpts.transparency = true; 
        pngOpts.interlaced = true; 
        pngOpts.quality = 100;
        activeDocument.exportDocument(tmpFile,ExportType.SAVEFORWEB,pngOpts); 
        tmpFile.rename (saveFile);
        tmpFile.changePath(saveFile);
    }
    

    it'll export the file into a temporary file and then rename & move it to the requested path, should solve the path problem.