Skip to main content
maria0101
Known Participant
November 9, 2018
Answered

Edit script to save psd instead of png

  • November 9, 2018
  • 1 reply
  • 4220 views

I use this script quite often. It was enhanced yesterday by awesome people on here. I was wondering if it's possible, instead of it saving as a png to save as a psd file, but with the image size measurements still included? Many, many thanks in advance

var pth = "~/Desktop"; // path to save the file, the default save to the desktop, change as needed 

 

var tmp = app.preferences.rulerUnits; 

 

app.preferences.rulerUnits = Units.MM; 

 

var w = Math.round(activeDocument.width.value); 

var h = Math.round(activeDocument.height.value); 

 

var file = new File(pth+ "/" + w+"x"+h + ".png"); 

 

save_as_png(file); 

 

app.preferences.rulerUnits - tmp; 

 

 

 

function save_as_png(file, filter, none, comp) 

    { 

    try { 

        if (filter == undefined) filter = "PNGFilterAdaptive"; 

        if (none == undefined) none = true; 

        if (comp == undefined) comp = 9; 

 

        var d = new ActionDescriptor(); 

        var d1 = new ActionDescriptor(); 

        if (none) 

            d1.putEnumerated(stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceNone")); 

        else 

            d1.putEnumerated(stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceType"), stringIDToTypeID("PNGInterlaceAdam7")); 

 

        d1.putEnumerated(stringIDToTypeID("PNGFilter"), stringIDToTypeID("PNGFilter"), stringIDToTypeID(filter)); 

        d1.putInteger(stringIDToTypeID("compression"), comp); 

        d.putObject(stringIDToTypeID("as"), stringIDToTypeID("PNGFormat"), d1); 

        d.putPath(stringIDToTypeID("in"), file); 

        d.putBoolean(stringIDToTypeID("copy"), true); 

        executeAction(stringIDToTypeID("save"), d, DialogModes.NO); 

        } 

    catch (e) { alert(e); throw(e); } 

    } 

This topic has been closed for replies.
Correct answer Stephen Marsh

This is what I came up with:

//https://forums.adobe.com/thread/2558835

var pth = "~/Desktop"; // path to save the file, the default save to the desktop, change as needed

var tmp = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.MM;

var w = Math.round(activeDocument.width.value);

var h = Math.round(activeDocument.height.value);

var saveFile = new File(pth + "/" + w + "x" + h + ".psd");

SavePSD(saveFile);

app.preferences.rulerUnits = tmp;

function SavePSD(saveFile){  

psdSaveOptions = new PhotoshopSaveOptions();  

psdSaveOptions.embedColorProfile = true;

psdSaveOptions.alphaChannels = true;  

psdSaveOptions.layers = true;

psdSaveOptions.annotations = true;

psdSaveOptions.spotColors = true;

activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);  

}

P.S. I believe that the NaN result (not a number) in the JavaScript Console of ESTK was due to the erroneous - hyphen rather than equal = sign. Once I fixed that the original ruler units were returned (line 09).

1 reply

Legend
November 9, 2018

Please, finally read the documentation.

maria0101
maria0101Author
Known Participant
November 9, 2018

Thanks so much for your time, I know you've been so patient and helpful to me. I really appreciate it.

I have tried changing the png to psd myself but I get a NaN result.

var pth = "~/Desktop";

var tmp = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.MM;

var w = Math.round(activeDocument.width.value);

var h = Math.round(activeDocument.height.value);

var file = new File(pth+ "/" + w+"x"+h + ".psd");

app.preferences.rulerUnits - tmp;

function SavePSD(saveFile){  

psdSaveOptions = new PhotoshopSaveOptions();  

psdSaveOptions.embedColorProfile = true;  

psdSaveOptions.alphaChannels = true;

psdSaveOptions.layers = true;   

activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);  

}; 

I can't figure out what I've done wrong :/

Legend
November 9, 2018

What does NaN mean? )

You forgot to insert a line to call a function.

SavePSD(file)

And it works.

And there is a typo (it looks like mine)

app.preferences.rulerUnits - tmp;

must be

app.preferences.rulerUnits = tmp;