To be clear, SFW does not change ppi to 72. What it does is strip the ppi value from the file altogether. The 72 number just appears as a default value when the file is reopened into Photoshop.
By @D Fosse
Agreed, in fact, the PNG specification is actually based on PIXELS PER METRE! Photoshop opens and rounds to the nearest PPI value when the units are inches, but the value written is in PPM.
I've added a little bit extra to my script to reopen the SaveForWeb png and then it sets it to 300ppi.
Thanks very much for all your help guys! Much appreciated.
For anyone that wants to reference this in the future. Here is the script. It saves the png in a folder called PNG where the filepath of the original psd file is. It's a little frustrating that the ppi needs to be set to 300 but i'm just working to a spec.
var doc = app.activeDocument;
var doc_path = doc.path;
var fullName = doc.name;
var myFileName = fullName.replace(/.[^.]+$/, '');
var defaultRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var options = new ExportOptionsSaveForWeb();
options.format = SaveDocumentType.PNG;
options.transparency = true;
options.includeProfile = true;
options.PNG8 = false;
options.optimization = false;
options.interlaced = false;
// Check for folder
var pngf = new Folder(doc_path + "/PNG");
if (!pngf.exists) {
pngf.create();
}
var outputFile = new File(doc_path + "/PNG/" + myFileName + ".png");
// Check if a file with the same name already exists and delete it
if (outputFile.exists) {
outputFile.remove();
}
// Export the document using the ExportOptionsSaveForWeb
doc.exportDocument(outputFile, ExportType.SAVEFORWEB, options);
// Close the currently open document
doc.close(SaveOptions.DONOTSAVECHANGES);
// Reopen the exported PNG file
var reopenedDoc = app.open(outputFile);
// Set the DPI to 300 PPI
reopenedDoc.resizeImage(null, null, 300, ResampleMethod.NONE);
// Save and close the document
reopenedDoc.save();
reopenedDoc.close(SaveOptions.SAVECHANGES);