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

Edit script to include file name in saved png

  • November 7, 2018
  • 1 reply
  • 1557 views

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); } 

    } 

This topic has been closed for replies.
Correct answer r-bin

Try this (if I understand you correctly)

var pcn = 70; // what is it??

var pth = "~/Desktop";

var nm = activeDocument.name;

// remove file extention

var n = nm.lastIndexOf(".")

if (n > 0) nm = nm.substr(0, n);

// remove WWWxHHH - PP% at the end if it is present

nm = nm.replace(/\s*\d+x\d+\s*-\s*\d+%$/g, "");

var tmp = app.preferences.rulerUnits; 

app.preferences.rulerUnits = Units.MM; 

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

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

app.preferences.rulerUnits = tmp; 

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

save_as_png(file); 

1 reply

Stephen Marsh
Community Expert
November 7, 2018

Same code as the OP, however I believe that it is presented a little better for the eyes…

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);

    }

}

I’m only a newb at scripting, however I believe that line 6 is the one that requires changing, possibly another line or two of code may be required… Can you please provide two different before and after filename examples so that the forum can be sure of what you are after?

ORIGINAL: myreallyfantasticfilename.psd

RESULT: myreallyfanta.png

ORIGINAL: 12345678901234567890123456789.tif

RESULT: 123456789012345678.tif

maria0101
maria0101Author
Known Participant
November 7, 2018

Gah!! Sorry, I had that grid when I was typing it out. I was hoping the bad formatting on this page would just be my rubbish computer. Sorry!

When it's saved, it looks like 123x321- 80%

I have to add AB12345 TITLE NAME before it so it looks like AB12345 TITLE NAME 23x321- 80%

Then I'll open that file in Photoshop and change the image size again and it'll save it to 111x222- 70%, but I want it to include the AB12345 TITLE NAME of that original file open in Photoshop

Many thanks again

r-binCorrect answer
Brainiac
November 7, 2018

Try this (if I understand you correctly)

var pcn = 70; // what is it??

var pth = "~/Desktop";

var nm = activeDocument.name;

// remove file extention

var n = nm.lastIndexOf(".")

if (n > 0) nm = nm.substr(0, n);

// remove WWWxHHH - PP% at the end if it is present

nm = nm.replace(/\s*\d+x\d+\s*-\s*\d+%$/g, "");

var tmp = app.preferences.rulerUnits; 

app.preferences.rulerUnits = Units.MM; 

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

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

app.preferences.rulerUnits = tmp; 

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

save_as_png(file);