• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Save increments and save embedded colour profile (JPG and PNG)

Participant ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

I have two scripts I have been using for a while... one saves a incremenal jpg and the other saves a incrmental PNG... both "should" also retain the current pixle dimentison and DPI. (My old png script used to lower the dpi for some reason)

 

PNG Script

var saveFolder = new Folder( "D:\\Projects\\_IncSave" );
var saveExt = 'png';  
var saveSufixStart = '_';  
var saveSufixLength = 3;  
// End of user options   
  
var docName = decodeURI ( activeDocument.name );  
docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ];  
var saveName = docName[ 1 ]; // activeDocument name with out ext  
var files = saveFolder.getFiles( saveName + '*.' + saveExt );// get an array of files matching doc name prefix  
  
var saveNumber = files.length + 1;  
//alert("New file number: " + zeroPad( saveNumber, saveSufixLength ));  
var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );  
saveAsPNG(saveFile, 9);  
  
function zeroPad ( num, digit ) {   
   var tmp = num.toString();   
   while (tmp.length < digit) { tmp = "0" + tmp;}   
   return tmp;  
}

function saveAsPNG(saveFile, quality) { 
    pngOpts = new PNGSaveOptions();
    pngOpts.compression = quality; //0-9
    pngOpts.interlaced = false;
    activeDocument.saveAs(File(saveFile), pngOpts, true); 
}

 

JPG Script

saveIncrementally();  
  
function saveIncrementally() {  
     try {  
          var saveFolder = "D:\\Projects\\_IncSave";  
          if (!Folder(saveFolder).exists) {  
               saveFolder = Folder.selectDialog("Select output folder");  
          if (!saveFolder)  
               return;  
     }  
       
     var saveExt = 'jpg';  
     var saveSufixStart = '_';  
     var saveSufixLength = 3;  
  
  
     // End of user options  
     //==========================================
  
  
     var docName = decodeURI ( activeDocument.name );  
     var dot = docName.lastIndexOf(".");  
     var saveName = docName.slice(0, dot);  
  
  
     var suffix = zeroPad(0, saveSufixLength);  
     var myFile = saveFolder + "/" + saveName + "_" + suffix + ".jpg";  
  
  
     while (File(myFile).exists) {  
          var suffixInteger = parseInt(suffix, 10);  
               suffixInteger += 1;  
          suffix = zeroPad(suffixInteger, 3);  
          myFile = saveFolder + "/" + saveName + "_" + suffix + ".jpg";  
     }  
  
  
     saveJPEG(myFile, 10)  
  
  
     function saveJPEG(saveFile, qty ) {   
          var saveOptions = new JPEGSaveOptions();   
          saveOptions.embedColorProfile = true;        
          saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;   
          saveOptions.matte = MatteType.NONE;   
          saveOptions.quality = qty;   
          app.activeDocument.saveAs( File(saveFile), saveOptions, true );   
     }  
  
  
     function zeroPad ( num, digit ){  
          var tmp = num.toString();  
          while (tmp.length < digit) { tmp = "0" + tmp;}  
          return tmp;  
     }  
  
  
     } catch(e) {  
          alert(e.toString() + "\nLine: " + e.line.toString())  
     }  
}  

 

 

Can somone that knows how these things work (I copied these form a reddit post that is now locked) and make sure these sctips also save the current colour profile into the image, as if the tick box in the save dialouge is ticked?

TOPICS
Actions and scripting

Views

359

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Aug 01, 2024 Aug 01, 2024

@abUSER23 

 

The JPEG script works as intended in v2021 and v2024, including both the resolution PPI and ICC profile.

 

The PNG in v2021 and v2024 includes resolution, however, Document Object Model based scripting PNGSaveOptions has never supported ICC profile inclusion. For that, you need to swap it out for Action Manager code.

 

You can replace the original DOM function at the end of the PNG script with the following AM function:

 

function saveAsPNG(saveFile) { 
    var s2t = function (s) {
...

Votes

Translate

Translate
Community Expert , Aug 02, 2024 Aug 02, 2024

I'll post the code.

 

@abUSER23 – try this:

 

var saveFolder = new Folder("D:\\Projects\\_IncSave");
var saveExt = 'tif';
var saveSufixStart = '_';
var saveSufixLength = 3;
// End of user options   

var docName = decodeURI(activeDocument.name);
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/) : docName = [docName, docName, undefined];
var saveName = docName[1]; // activeDocument name with out ext  
var files = saveFolder.getFiles(saveName + '*.' + saveExt);/
...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 01, 2024 Aug 01, 2024

Copy link to clipboard

Copied

@abUSER23 

 

The JPEG script works as intended in v2021 and v2024, including both the resolution PPI and ICC profile.

 

The PNG in v2021 and v2024 includes resolution, however, Document Object Model based scripting PNGSaveOptions has never supported ICC profile inclusion. For that, you need to swap it out for Action Manager code.

 

You can replace the original DOM function at the end of the PNG script with the following AM function:

 

function saveAsPNG(saveFile) { 
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor2.putEnumerated(s2t("method"), s2t("PNGMethod"), s2t("quick")); // "quick" || "moderate" || "thorough"
    descriptor2.putEnumerated(s2t("PNGInterlaceType"), s2t("PNGInterlaceType"), s2t("PNGInterlaceNone")); // "PNGInterlaceNone" || "PNGInterlaceAdam7"
    descriptor2.putEnumerated(s2t("PNGFilter"), s2t("PNGFilter"), s2t("PNGFilterAdaptive"));
    descriptor2.putInteger(s2t("compression"), 6); // 0-9 - Only for "quick", otherwise comment out
    descriptor2.putEnumerated(s2t("embedIccProfileLastState"), s2t("embedOff"), s2t("embedOn"));
    descriptor.putObject(s2t("as"), s2t("PNGFormat"), descriptor2);
    descriptor.putPath(s2t("in"), new File(saveFile));
    descriptor.putBoolean(s2t("copy"), true);
    descriptor.putBoolean(s2t("lowerCase"), true);
    descriptor.putBoolean(s2t("embedProfiles"), true);
    executeAction(s2t("save"), descriptor, DialogModes.NO);
}

 

Hope this helps!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 02, 2024 Aug 02, 2024

Copy link to clipboard

Copied

Thanks!

 

Can you check this code... I got it from a chat AI..... it is saving a TIFF as a single layer and saving the profile...

 

#target photoshop

// Main function
function saveTIFWithIncrementalNumber() {
    // Specify the directory to save the TIF files
    var saveDir = "D:\\Projects\\_IncSave\\"; // Edit this path

    // Ensure the directory exists
    var saveFolder = new Folder(saveDir);
    if (!saveFolder.exists) {
        alert("The specified directory does not exist: " + saveDir);
        return;
    }

    // Get the active document
    var doc = app.activeDocument;

    // Check if the document is untitled
    var docName = doc.name;
    if (docName.match(/^Untitled-/i)) {
        docName = "untitled";
    } else {
        docName = doc.name.replace(/\.[^\.]+$/, ''); // Remove file extension
    }

    // Get the file number increment
    var fileNumber = getNextIncrementalNumber(saveDir, docName);

    // Create the file name with incremental number
    var tifFileName = docName + "." + zeroPad(fileNumber, 4) + ".tif";

    // Create the file object
    var tifFile = new File(saveDir + tifFileName);

    // Duplicate the document to preserve the original
    var dupDoc = doc.duplicate();

    // Flatten the duplicated document
    dupDoc.flatten();

    // Save options for TIF file
    var tiffSaveOptions = new TiffSaveOptions();
    tiffSaveOptions.imageCompression = TIFFEncoding.NONE;
    tiffSaveOptions.embedColorProfile = true;

    // Save the TIF file
    dupDoc.saveAs(tifFile, tiffSaveOptions, true, Extension.LOWERCASE);

    // Close the duplicated document without saving
    dupDoc.close(SaveOptions.DONOTSAVECHANGES);
}

// Function to get the next incremental number for the file
function getNextIncrementalNumber(dir, baseName) {
    var files = Folder(dir).getFiles(baseName + ".*.tif");
    var maxNumber = 0;

    for (var i = 0; i < files.length; i++) {
        var fileName = files[i].name;
        var match = fileName.match(/(\d+)\.tif$/);
        if (match) {
            var num = parseInt(match[1], 10);
            if (num > maxNumber) {
                maxNumber = num;
            }
        }
    }

    return maxNumber + 1;
}

// Function to pad the number with zeros
function zeroPad(num, size) {
    var s = "000000000" + num;
    return s.substr(s.length - size);
}

// Run the main function
saveTIFWithIncrementalNumber();

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2024 Aug 02, 2024

Copy link to clipboard

Copied

Seems a little defective. It would be easier to just swap out the PNG or JPEG save function for TIFF.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 02, 2024 Aug 02, 2024

Copy link to clipboard

Copied

how would I do that? I am just copy and paste.. I have no idea what any of the text means.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2024 Aug 02, 2024

Copy link to clipboard

Copied

I'll post the code.

 

@abUSER23 – try this:

 

var saveFolder = new Folder("D:\\Projects\\_IncSave");
var saveExt = 'tif';
var saveSufixStart = '_';
var saveSufixLength = 3;
// End of user options   

var docName = decodeURI(activeDocument.name);
docName = docName.match(/(.*)(\.[^\.]+)/) ? docName = docName.match(/(.*)(\.[^\.]+)/) : docName = [docName, docName, undefined];
var saveName = docName[1]; // activeDocument name with out ext  
var files = saveFolder.getFiles(saveName + '*.' + saveExt);// get an array of files matching doc name prefix  

var saveNumber = files.length + 1;
//alert("New file number: " + zeroPad( saveNumber, saveSufixLength ));  
var saveFile = new File(saveFolder + '/' + saveName + '_' + zeroPad(saveNumber, saveSufixLength) + '.' + saveExt);
saveAsTIFF();

function zeroPad(num, digit) {
    var tmp = num.toString();
    while (tmp.length < digit) { tmp = "0" + tmp; }
    return tmp;
}

function saveAsTIFF() {
    tiffSaveOptions = new TiffSaveOptions();
    tiffSaveOptions.embedColorProfile = true;
    // ByteOrder.MACOS or ByteOrder.IBM
    tiffSaveOptions.byteOrder = ByteOrder.IBM;
    tiffSaveOptions.transparency = false;
    tiffSaveOptions.layers = false;
    tiffSaveOptions.layerCompression = LayerCompression.ZIP;
    tiffSaveOptions.interleaveChannels = true;
    tiffSaveOptions.alphaChannels = true;
    tiffSaveOptions.annotations = true;
    tiffSaveOptions.spotColors = true;
    tiffSaveOptions.saveImagePyramid = false;
    // NONE | JPEG | TIFFLZW | TIFFZIP
    tiffSaveOptions.imageCompression = TIFFEncoding.NONE;
    activeDocument.saveAs(File(saveFile), tiffSaveOptions, true, Extension.LOWERCASE);
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 06, 2024 Aug 06, 2024

Copy link to clipboard

Copied

thanks again... this seems to work very well... cheers!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 07, 2024 Aug 07, 2024

Copy link to clipboard

Copied

LATEST

@abUSER23 – You’re welcome!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2024 Aug 02, 2024

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines