Skip to main content
Participating Frequently
September 8, 2023
Answered

Saving in 300dpi Script

  • September 8, 2023
  • 3 replies
  • 922 views

Hi everyone. I'am using a script but its saving images in 96dpi. Is tehere any way to save them in 300dpi ?

Thanks.

 

This is the script :

 

#target photoshop
if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;
    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12;   
    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT") {
        alert("selected layer is not a smart object")
    } else {
        // Select Files;
        if ($.os.search(/windows/i) != -1) {
            var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)
        } else {
            var theFiles = File.openDialog("please select files", getFiles, true)
        };
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
                // Save JPG. If you have need of saving different artboards into separate files,
                // see https://gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c#gistcomment-3996733
// Export each top layer group (artboards) as a JPG
var destStatics = new Folder(activeDocument.path + '/../assets/static-banners/')
var doc = activeDocument;
if (!destStatics.exists) {
  destStatics.create();
}

(function getLayers(el) {
// find layer groups
  var today = new Date();
  var time = today.getHours() + "-" + today.getMinutes() + "-" + today.getSeconds();
  var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
  for (var a = 0; a < el.layerSets.length; a++) {
    var lname = el.layerSets[a].name;
    var exportName = '';
    if (time + lname != 'Null' && lname != 'NULL' && time + lname != 'null') {
      var ext = time + lname.substr(-4);
      if (ext === '.jpg') {
var exportName = time + lname;
      } else if (ext === '.jpg') {
var exportName = time + name.replace('.png', '.jpg')
      } else {
var exportName = lname + theNewName + '.jpg'
      }
      saveLayer(el.layers.getByName(lname), exportName, destStatics, true);
    }
  }
})(doc)

function saveLayer(layer, lname, path, shouldMerge) {
  activeDocument.activeLayer = layer;
  dupLayers();
  if (shouldMerge === undefined || shouldMerge === true) {
    activeDocument.mergeVisibleLayers();
  }
  activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);
  var saveFile = File(path + "/" + lname);
  SaveJPG(saveFile);
  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

function dupLayers() {
  var desc143 = new ActionDescriptor();
  var ref73 = new ActionReference();
  ref73.putClass( charIDToTypeID('Dcmn') );
  desc143.putReference( charIDToTypeID('null'), ref73 );
  desc143.putString( charIDToTypeID('Nm  '), activeDocument.activeLayer.name );
  var ref74 = new ActionReference();
  ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
  desc143.putReference( charIDToTypeID('Usng'), ref74 );
  executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO );
};

function SaveJPG(saveFile){
  var sfwOptions = new ExportOptionsSaveForWeb();   
  sfwOptions.format = SaveDocumentType.JPEG;   
  sfwOptions.includeProfile = false;   
  sfwOptions.interlaced = false;   
  sfwOptions.optimized = true;   
  sfwOptions.quality = 100;
  activeDocument.exportDocument( saveFile, ExportType.SAVEFORWEB, sfwOptions );
}            }
        }
    }
};
// Get PSDs, TIFs and JPGs from files
function getFiles(theFile) {
    if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
        return true
    };
};
// Replace SmartObject Contents
function replaceContents(newFile, theSO) {
    app.activeDocument.activeLayer = theSO;
    // =======================================================
    var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
    var desc3 = new ActionDescriptor();
    var idnull = charIDToTypeID("null");
    desc3.putPath(idnull, new File(newFile));
    var idPgNm = charIDToTypeID("PgNm");
    desc3.putInteger(idPgNm, 1);
    executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
    return app.activeDocument.activeLayer
};

 

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

Any idea sir ? 


@Lukadris – Can you attach the PSD and smart object replacement file? You can reduce their pixel dimensions.

 

Perhaps add a step to flatten before saving (and or removing alphas or ensuring 8 bpc mode)?

 

Or try changing from:

 

activeDocument.saveAs(saveFile, jpgSaveOptions, false);

 

To:

 

activeDocument.saveAs(saveFile, jpgSaveOptions, true);

 

3 replies

Stephen Marsh
Community Expert
Community Expert
September 8, 2023

@Lukadris 

 

The script has code for Save As JPEG, but it isn't actually referenced to save, it's just sitting there doing nothing, perhaps as a placeholder, but there aren't any comments explaining why it is there.

 

    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12; 

 

Then as @D Fosse notes, there is Save for Web code in a function that is actually referenced and used to save:

 

function SaveJPG(saveFile){
  var sfwOptions = new ExportOptionsSaveForWeb();   
  sfwOptions.format = SaveDocumentType.JPEG;   
  sfwOptions.includeProfile = false;   
  sfwOptions.interlaced = false;   
  sfwOptions.optimized = true;   
  sfwOptions.quality = 100;
  activeDocument.exportDocument( saveFile, ExportType.SAVEFORWEB, sfwOptions );
} 

 

So it should just be a case of swapping out the guts of the function, replacing the Save for Web code with the Save As code, which will retain the original resolution info (whether it is 300ppi or not).

Stephen Marsh
Community Expert
Community Expert
September 8, 2023

Curiously, in my tests of the original code, the document resolution was retained using the Save for Web code.

 

Anyway, here is the function with the Save As code instead of Save for Web:

 

function SaveJPG(saveFile) {
  jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.embedColorProfile = true;
  // STANDARDBASELINE | OPTIMIZEDBASELINE | PROGRESSIVE
  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  // Quality  0 low quality - 12 high quality
  jpgSaveOptions.quality = 12;
  // Only valid for Progressive: 3 - 5
  if (jpgSaveOptions.formatOptions == FormatOptions.PROGRESSIVE) {
    jpgSaveOptions.scans = 3;
  }
  // Save As JPEG
  activeDocument.saveAs(saveFile, jpgSaveOptions, false);
}

 

LukadrisAuthor
Participating Frequently
September 9, 2023

Thank you very much to both of you. I will try the code and share the result with you immediately.

D Fosse
Community Expert
Community Expert
September 8, 2023

I don't do scripts - but I noticed the output is Save For Web at the end.

 

Save For Web strips the ppi number altogether. It's not really 96 ppi; that's just a default value assigned when the file is reopened into a Microsoft application. If you reopened into Photoshop you would get 72.

 

Both Save For Web and Export are intended for screen/mobile devices, where ppi is irrelevant and doesn't apply. So it's removed.

 

To retain the ppi number, use Save As or Save A Copy.

 

Save A Copy is for when the file has properties that are not supported in the target save format. The jpeg file format specification doesn't allow 16 bit depth, layers of any kind, transparency or alpha channels. If the file has any of these, it has to be a copy with those properties removed/flattened. So you use Save A Copy.

 

 

LukadrisAuthor
Participating Frequently
September 8, 2023

If it is not possible, please help me on how to save it as PSD with this script. Because then I can get 300dpi images from psd files with image processor.