Rewriting the Save CMD

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Mcquiff

Rewriting the Save CMD

Post by Mcquiff »

I have been using this script thanks to an early post but it still doesn't perform as I would like it to. The purpose of the script is to always add metadata which it does.
The issue I have is that if I am working on a jpg and then add a layer, then run the script, it adds the meta data but will not save as the image now needs to be saved as a PSD.

The change needs to run through the "save as" rather than as the save function. I'm hoping someone can help me figure this out.

Code: Select allfunction save() {
if(!documents.length) return;
activeDocument.info.captionWriter = "Editted_by_the_photographer";
try{executeAction( charIDToTypeID('save'), undefined, DialogModes.NO );}catch(e){}
};

save();

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Rewriting the Save CMD

Post by Mike Hale »

A quick fix would be to add activeDocument.flatten(); before the save line. That would take care of documents opened from jpeg files that have layers. But that would not cover other problems you might run.

A better fix would be to check that the document has a file associated with it. Create a file object if the document doesn't, then explicitly save as jpeg.
Mcquiff

Rewriting the Save CMD

Post by Mcquiff »

Hi I definitely want to avoid the flattening of the image.

I don't how to do it but suppose if there is more than 1 layer then save as PSD or equal to one then save as current format?

I don't expect to run into other problems with other file types as the only ones I use are PSD or JPG.
Mcquiff

Rewriting the Save CMD

Post by Mcquiff »

Can anyone help me get this to count the number of layers and if more than one then save as PSD or if 1 layer save in original format (normally a jpg). I've no idea on the coding sadly Matt
Mike Hale

Rewriting the Save CMD

Post by Mike Hale »

Something like this. Note it assumes that the document has an associated file and it's either a psd or jpg. It will fail if the document doesn't have a file or it a different format such as png.
Code: Select allfunction saveAs() {
   if(!documents.length) return;
   activeDocument.info.captionWriter = "Editted_by_the_photographer";
   var currentFilePath = activeDocument.fullName;
   if(activeDocument.layers.length != 1){
      var saveFilePath = decodeURI(currentFilePath).replace(/\.jpg$/i,'.psd');
      SaveAsPSD( saveFilePath, true, true );
   }else{
      var saveFilePath = decodeURI(currentFilePath).replace(/\.psd$/i,'.jpg');
      SaveAsJPEG( saveFilePath, 8, true );
   }
};
function SaveAsJPEG( inFileName, inQuality, inEmbedICC ) {
   var jpegOptions = new JPEGSaveOptions();
   jpegOptions.quality = inQuality;
   jpegOptions.embedColorProfile = inEmbedICC;
   app.activeDocument.saveAs( File( inFileName ), jpegOptions );
};
function SaveAsPSD( inFileName, inMaximizeCompatibility, inEmbedICC ) {
   var psdSaveOptions = new PhotoshopSaveOptions();
   psdSaveOptions.embedColorProfile = inEmbedICC;
   psdSaveOptions.maximizeCompatibility = inMaximizeCompatibility;
   app.activeDocument.saveAs( File( inFileName ), psdSaveOptions );
};

saveAs();
Mcquiff

Rewriting the Save CMD

Post by Mcquiff »

excellent that works well for me. What I might do is create a drop folder so that the images get saved there then append the data afterwards. That way it prevents any risks of saving Png files as you mention.

Many Thanks
Mike Hale

Rewriting the Save CMD

Post by Mike Hale »

It wouldn't save a png file. It only saves jpeg and psd file. It could save a file with the wrong extension. And if the document doesn't have an associated file it would throw an error.

This would be more fault tolerant.
Code: Select allfunction saveAs() {
   if(!documents.length) return;
   activeDocument.info.captionWriter = "Editted_by_the_photographer";
   try{
      var currentFilePath = activeDocument.fullName;
   }catch(e){
      alert('Document has not been saved');
      return;
   }
   if(activeDocument.layers.length != 1){
      var saveFilePath = decodeURI(currentFilePath).replace(/\....?.$/i,'.psd');
      SaveAsPSD( saveFilePath, true, true );
   }else{
      var saveFilePath = decodeURI(currentFilePath).replace(/\....?.$/i,'.jpg');
      SaveAsJPEG( saveFilePath, 8, true );
   }
};
function SaveAsJPEG( inFileName, inQuality, inEmbedICC ) {
   var jpegOptions = new JPEGSaveOptions();
   jpegOptions.quality = inQuality;
   jpegOptions.embedColorProfile = inEmbedICC;
   app.activeDocument.saveAs( File( inFileName ), jpegOptions );
};
function SaveAsPSD( inFileName, inMaximizeCompatibility, inEmbedICC ) {
   var psdSaveOptions = new PhotoshopSaveOptions();
   psdSaveOptions.embedColorProfile = inEmbedICC;
   psdSaveOptions.maximizeCompatibility = inMaximizeCompatibility;
   app.activeDocument.saveAs( File( inFileName ), psdSaveOptions );
};

saveAs();