0

Can you give me a script which will save a PSD automatically when I save a PNG? I found a script that saves a PNG when saving a PSD, I tried to reverse it but it didn't work :(

main(); 
function main(){ 
    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
    var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,''); 
    if(Ext.toLowerCase() != 'psd') return; 
    var Path = app.activeDocument.path; 
    var saveFile = File(Path + "/" + Name +".png"); 
    if(saveFile.exists) saveFile.remove(); 
    SavePNG(saveFile); 
} 

function SavePNG(saveFile){ 
    pngSaveOptions = new PNGSaveOptions(); 
    activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 
} 
1

The code above doesn't have a function to save out a PSD. Your code assumes that the psd already exists.

Try this

main(); 

function main()
{ 

    var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
    var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,''); 
    if(Ext.toLowerCase() != 'psd') return;

    // assumes the file has already been saved
    var Path = app.activeDocument.path; 

    // Save as PSD
    var SaveFile = File(Path + "/" + Name +".psd"); 
    if(SaveFile.exists) SaveFile.remove(); 
    SavePSD(SaveFile);

    // Save as PNG
    SaveFile = File(Path + "/" + Name +".png"); 
    if(SaveFile.exists) SaveFile.remove(); 
    SavePNG(SaveFile);

    alert("File saved as .png and .psd");
} 

function SavePNG(saveFile)
{ 
  var pngSaveOptions = new PNGSaveOptions(); 
  activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 
}

function SavePSD(saveFile)
{
  var psdFile = new File(saveFile);
  psdSaveOptions = new PhotoshopSaveOptions();
  psdSaveOptions.embedColorProfile = true;
  psdSaveOptions.alphaChannels = true;  
  activeDocument.saveAs(psdFile, psdSaveOptions, false, Extension.LOWERCASE);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.