Edit script to include file name in saved png
I use this script a lot which works perfectly, thanks to an amazing person who helped me on here
I know there are name matches and such in scripts but I can't figure out how exactly to include it.
I open an existing png file in Photoshop and use an action which decreases the image size to 80%, then saves it as a .png file with the measurements included in the file name. I was wondering if it's possible to take the original file name and include that in the new .png file name, whilst also slicing 12 characters off the end (which are usually 123x321-100%)
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 + "- 80%.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); }
}
