Skip to main content
dereke12252909
Participating Frequently
October 17, 2019
Question

saving multiple images and changing filename with script/action

  • October 17, 2019
  • 7 replies
  • 4177 views

Hello everyone!

I'm looking to create a script/action that will help automate my workflow.

I have to save convert images from a psd (rgb_filename.psd) into 
rgb_filename.jpg

rgb_filename.png
rgb_filename.tif
then flatten all layers and convert image to CMYK(created an action for this)

then save to:

cmyk_filename.tif

cmyk_filename.jpg

 

So i guess what i'm trying to ask is how can i create an action that will change or add CMYK from rgb?

Ty!!!

This topic has been closed for replies.

7 replies

Stephen Marsh
Community Expert
Community Expert
October 24, 2019

OK, the updated code is below, it will save out 3 RGB and 2 CMYK versions of the previously saved and open master PSD image.

 

I am not 100% happy with the code, however, it is fully functional.

 

 

#target photoshop

/* 
USE AT YOUR OWN RISK
Input files are expected to be open, saved and have a prefix of: "rgb_" 
*/

/* Start Open Document Check - Part A: Try */
activeDoc();
var originalDoc

function activeDoc() {
    try {
        originalDoc = app.activeDocument
        /* Finish Open Document Check - Part A: Try */

        /* Main Code Start */

        var doc = app.activeDocument;
        var docName = doc.name.replace(/\.[^\.]+$/, '');
        var cmykDocName = doc.name.replace(/^rgb_/, 'cmyk_').replace(/\.[^\.]+$/, '');

        // Save RGB as Original ICC TIFF
        var saveFileTIFF = new File(doc.path + '/' + docName + '.tif');
        SaveTIFF(saveFileTIFF);

        // Convert to U.S. Web Coated (SWOP) v2, Merged, Relative Colorimetric Intent with BPC on
        convertToProfile("U.S. Web Coated (SWOP) v2", true, true, -1);

        function convertToProfile(to, mapBlack, flatten, shadowMode) {
            var c2t = function (s) {
                return app.charIDToTypeID(s);
            };

            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };

            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();

            reference.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(c2t("null"), reference);
            descriptor.putString(s2t("to"), to);
            descriptor.putEnumerated(s2t("intent"), s2t("intent"), s2t("colorimetric"));
            descriptor.putBoolean(s2t("mapBlack"), mapBlack);
            descriptor.putBoolean(s2t("flatten"), flatten);
            descriptor.putInteger(s2t("shadowMode"), shadowMode);
            executeAction(s2t("convertToProfile"), descriptor, DialogModes.NO);
        }

        // Save PSD to U.S. Web Coated (SWOP) v2 CMYK TIFF, No Compression, IBM, ZIP layer compression, with Transparency
        var saveFileCMYKTIFF = new File(doc.path + '/' + cmykDocName + '.tif');
        SaveCMYKTIFF(saveFileCMYKTIFF);

        // Save PSD to U.S. Web Coated (SWOP) v2 CMYK as JPEG, Quality 12, Baseline, No Matte
        var saveFileCMYKJPEG = new File(doc.path + '/' + cmykDocName + '.jpg');
        SaveCMYKJPEG(saveFileCMYKJPEG);

        // History Step Back – Undo the U.S. Web Coated (SWOP) v2 CMYK Conversion
        select();

        function select() {
            var c2t = function (s) {
                return app.charIDToTypeID(s);
            };

            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };

            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();

            reference.putEnumerated(c2t("HstS"), s2t("ordinal"), s2t("previous"));
            descriptor.putReference(c2t("null"), reference);
            executeAction(s2t("select"), descriptor, DialogModes.NO);
        }

        // Convert to sRGB IEC61966-2.1, Merged, Relative Colorimetric Intent with BPC on
        convertToProfile("sRGB IEC61966-2.1", true, true, -1);

        function convertToProfile(to, mapBlack, flatten, shadowMode) {
            var c2t = function (s) {
                return app.charIDToTypeID(s);
            };

            var s2t = function (s) {
                return app.stringIDToTypeID(s);
            };

            var descriptor = new ActionDescriptor();
            var reference = new ActionReference();

            reference.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
            descriptor.putReference(c2t("null"), reference);
            descriptor.putString(s2t("to"), to);
            descriptor.putEnumerated(s2t("intent"), s2t("intent"), s2t("colorimetric"));
            descriptor.putBoolean(s2t("mapBlack"), mapBlack);
            descriptor.putBoolean(s2t("flatten"), flatten);
            descriptor.putInteger(s2t("shadowMode"), shadowMode);
            executeAction(s2t("convertToProfile"), descriptor, DialogModes.NO);
        }

        // Save RGB as JPEG
        var saveFileJPEG = new File(doc.path + '/' + docName + '.jpg');
        SaveJPEG(saveFileJPEG);

        // Save RGB as PNG
        var saveFilePNG = new File(doc.path + '/' + docName + '.png');
        SavePNG(saveFilePNG);

        doc.close(SaveOptions.DONOTSAVECHANGES);

        alert('3 RGB and 2 CMYK files were saved!');

        /* Save Options Start */

        // JPEG Options (CMYK)
        function SaveCMYKJPEG(saveFileCMYKJPEG) {

            jpgSaveOptions = new JPEGSaveOptions();
            jpgSaveOptions.embedColorProfile = true; // or false
            jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; // or OPTIMIZEDBASELINE or PROGRESSIVE
            jpgSaveOptions.quality = 12; // range 0 - 12
            jpgSaveOptions.MatteType = MatteType.NONE;
            activeDocument.saveAs(saveFileCMYKJPEG, jpgSaveOptions, true, Extension.LOWERCASE);
        };

        // TIFF Options (CMYK)
        function SaveCMYKTIFF(saveFileCMYKTIFF) {
            tiffSaveOptions = new TiffSaveOptions();
            tiffSaveOptions.embedColorProfile = true; // or false
            tiffSaveOptions.byteOrder = ByteOrder.IBM; // or MACOS
            tiffSaveOptions.transparency = true; // or false
            tiffSaveOptions.layers = true; // or false
            tiffSaveOptions.layerCompression = LayerCompression.ZIP; // or RLE
            tiffSaveOptions.interleaveChannels = true; // or false
            tiffSaveOptions.alphaChannels = true; // or false
            tiffSaveOptions.spotColors = true; // or false
            tiffSaveOptions.imageCompression = TIFFEncoding.NONE; // or TIFFZIP or JPEG or TIFFLZW 
            activeDocument.saveAs(saveFileCMYKTIFF, tiffSaveOptions, true, Extension.LOWERCASE);
        };

        // PNG Options (RGB)
        function SavePNG(saveFilePNG) {

            // Convert to 8 bpc - Otherwise the PNG would be 16 bpc!?
            doc.bitsPerChannel = BitsPerChannelType.EIGHT;

            pngSaveOptions = new PNGSaveOptions();
            pngSaveOptions.compression = 1; // 0-9
            pngSaveOptions.interlaced = false;
            activeDocument.saveAs(saveFilePNG, pngSaveOptions, true, Extension.LOWERCASE);
        };

        // JPEG Options (RGB)
        function SaveJPEG(saveFileJPEG) {
            jpgSaveOptions = new JPEGSaveOptions();
            jpgSaveOptions.embedColorProfile = true; // or false
            jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; // or OPTIMIZEDBASELINE or PROGRESSIVE
            jpgSaveOptions.quality = 12; // range 0 - 12
            jpgSaveOptions.MatteType = MatteType.NONE;
            activeDocument.saveAs(saveFileJPEG, jpgSaveOptions, true, Extension.LOWERCASE);
        };

        // TIFF Options (RGB)
        function SaveTIFF(saveFileTIFF) {
            tiffSaveOptions = new TiffSaveOptions();
            tiffSaveOptions.embedColorProfile = true; // or false
            tiffSaveOptions.byteOrder = ByteOrder.IBM; // or MACOS
            tiffSaveOptions.transparency = true; // or false
            tiffSaveOptions.layers = true; // or false
            tiffSaveOptions.layerCompression = LayerCompression.ZIP; // or RLE
            tiffSaveOptions.interleaveChannels = true; // or false
            tiffSaveOptions.alphaChannels = true; // or false
            tiffSaveOptions.spotColors = true; // or false
            tiffSaveOptions.imageCompression = TIFFEncoding.NONE; // or TIFFZIP or JPEG or TIFFLZW 
            activeDocument.saveAs(saveFileTIFF, tiffSaveOptions, true, Extension.LOWERCASE);
        };

        /* Save Options Finish */

        /* Main Code Finish */

        /* Start Open Document Check - Part B: Catch */
    } catch (err) {
        alert('An image must be open and saved with a filename prefix of "rgb_" before running this script!')
    }
}
/* Finish Open Document Check - Part B: Catch */

 

 

 

 

 

dereke12252909
Participating Frequently
November 5, 2019

While driving to work this morning, I came to the realization that I never replied back to you!

Dude this is amazing. I really appreciate it and it's working flawlessly!!!

Stephen Marsh
Community Expert
Community Expert
October 23, 2019

Just to confirm, you originally mentioned a flattening step performed by the action, however, in both the RGB and CMYK workflow screenshots, you are saving a transparent/layered file.

 

Is that correct, don't flatten in the script... Just accept the natural flattening for formats like JPG and transparency for PNG or TIFF (whether RGB or CMYK)?

dereke12252909
Participating Frequently
October 23, 2019

oh right, i would always just accept the default/natural flattening for formats like JPG and transparency for PNG/tiff. i always had to merge all the visible layers prior to CMYK conversion because for some reason all the color adjustments would look very off.

 

i could always just create a layer on the top using apply image to bypass the merge layers if its not possible during CMYK conversion.

like so

all files besides the jpgs should be transparent 

Stephen Marsh
Community Expert
Community Expert
October 23, 2019

The colour appearance will be incorrect if I don't merge before or during the RGB or CMYK conversion... All good, I'll update the code and keep testing!

Stephen Marsh
Community Expert
Community Expert
October 22, 2019

 

 

/* 
This is amazing!!! This will help me so much!

Is there anyway to automate the rgb file before the cmyk conversion?
rgb_jpg
rgb_png
rgb_tif(optional)

if not it's no worries at all. this itself helps me so much. thank you so much for your effort
*/

 

 

 

Yes, the script can be modified to do everything in your stated workflow. However, before undertaking this I would need explicit instructions...

 

RGB Workflow:

  1. Is there a colour conversion from one RGB ICC profile to another? Example – your main project RGB is ProPhoto RGB, which is fine for the TIFF but for the PNG and JPG you require a conversion to the sRGB ICC profile.
  2. TIFF settings, can you show a screenshot from Save as? What exactly do you mean by "optional"...
  3. What exact PNG settings, can you show a screenshot from Save as or export/save for web?
  4. What exact JPEG settings, can you show a screenshot from Save as or export/save for web?

 

CMYK Workflow:

  1. What CMYK ICC profile and conversion options, can you take a screenshot?
  2. TIFF settings, can you show a screenshot from Save as?
  3. What exact JPEG settings, can you show a screenshot from Save as?

 

dereke12252909
Participating Frequently
October 22, 2019

main project is an Prophoto RGB. 

 

Here's my RGB saving process 

the tif is optional because i wasn't sure if it was too much work for you 😮

usually the JPG and PNG(w/ transparency) is just converted to sRGB during export.

  

really again thank you so much.

Stephen Marsh
Community Expert
Community Expert
October 19, 2019

The previous script saved out the files with the rgb_ prefix changed to cmyk_ 

 

However, an alternative approach is to simply duplicate the source file with the desired cmyk_ prefix + variable file name and then use an action for everything else. You would need to record the execution of this script into your action.

 

 

 

#target photoshop
var origDoc = app.activeDocument;
var newDocName = origDoc.name.replace(/^rgb_/, 'cmyk_').replace(/\.[^\.]+$/, '');
origDoc.duplicate((newDocName), false); // true to flatten
origDoc.close(SaveOptions.DONOTSAVECHANGES); // close the original doc without saving

 

 

 

Stephen Marsh
Community Expert
Community Expert
October 19, 2019

OK, here you go, give this a try on copies just in case something goes wrong.

 

You will have to change the action set name and action name on line 15 to the appropriate flatten and CMYK conversion action.

 

You can record the running of this script into an action, then you should be able to play a single action to create all of the required files from the original master file.

 

Please let me know if anything needs adjusting, however, I believe that this is what you are looking for.

 

EDIT: I have just re-posted the original script as the CMYK TIFF was converting to 8 bpc when it should have been 16 bpc.

 

 

 

 

#target photoshop

/* 
INFO:
Input files are expected to have a prefix of: rgb_ 
Edit the action set and action name on line 13 to match the action in use on your system
*/ 

var doc = app.activeDocument;
var docName = doc.name.replace(/^rgb_/, 'cmyk_').replace(/\.[^\.]+$/, '');

// Run an action that flattens and converts to CMYK
app.doAction('Flatten & Convert to CMYK', 'Flatten & Convert to CMYK Set.atn'); // Change the action & action set name as required

/*
// Flatten
doc.flatten();
*/

/*
// Convert to Fogra 39 CMYK, Relative Colorimetric Intent with BPC on
convertToProfile("Coated FOGRA39 (ISO 12647-2:2004)", true, false, -1);
function convertToProfile(to, mapBlack, dither, shadowMode) {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};

	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};

	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();

	reference.putEnumerated( s2t( "document" ), s2t( "ordinal" ), s2t( "targetEnum" ));
	descriptor.putReference( c2t( "null" ), reference );
	descriptor.putString( s2t( "to" ), to );
	descriptor.putEnumerated( s2t( "intent" ), s2t( "intent" ), s2t( "colorimetric" ));
	descriptor.putBoolean( s2t( "mapBlack" ), mapBlack );
	descriptor.putBoolean( s2t( "dither" ), dither );
	descriptor.putInteger( s2t( "shadowMode" ), shadowMode );
	executeAction( s2t( "convertToProfile" ), descriptor, DialogModes.NO );
}
*/ 

var saveFileTIFF = new File(doc.path + '/' + docName + '.tif');
SaveTIFF(saveFileTIFF);

var saveFileJPEG = new File(doc.path + '/' + docName + '.jpg');
SaveJPEG(saveFileJPEG);

// Close without saving changes to the previously saved file
doc.close(SaveOptions.DONOTSAVECHANGES);

// TIFF Options
function SaveTIFF(saveFileTIFF) {
    tiffSaveOptions = new TiffSaveOptions();
    tiffSaveOptions.embedColorProfile = true; // or false
    tiffSaveOptions.byteOrder = ByteOrder.IBM; // or MACOS
    tiffSaveOptions.transparency = true; // or false
    tiffSaveOptions.layers = true; // or false
    tiffSaveOptions.layerCompression = LayerCompression.ZIP; // or RLE
    tiffSaveOptions.interleaveChannels = true; // or false
    tiffSaveOptions.alphaChannels = true; // or false
    tiffSaveOptions.spotColors = true; // or false
    tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW; // or NONE or JPEG or TIFFZIP  
    activeDocument.saveAs(saveFileTIFF, tiffSaveOptions, true, Extension.LOWERCASE);
};

// JPEG Options
function SaveJPEG(saveFileJPEG) {
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.embedColorProfile = true; // or false
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; // or OPTIMIZEDBASELINE or PROGRESSIVE
    jpgSaveOptions.quality = 10; // range 0 - 12
    activeDocument.saveAs(saveFileJPEG, jpgSaveOptions, true, Extension.LOWERCASE);
};

 

 

 

 

 

dereke12252909
Participating Frequently
October 21, 2019

This is amazing!!! This will help me so much!

 

Is there anyway to automate the rgb file before the cmyk conversion?

rgb_jpg

rgb_png

rgb_tif(optional)

 

if not it's no worries at all. this itself helps me so much. thank you so much for your effort

Stephen Marsh
Community Expert
Community Expert
October 17, 2019

I just read your post again, it looks like a custom script is required if you need to change the filename prefix from _rgb to _cmyk – however one could still use Image Processor Pro and just have a dupe/rename script and still leverage IPP for the saving (which can be recorded into an action etc).

 

Are you all good with the RGB files and it is only the CMYK that need work?

 

I have mostly completed a custom script, however it is not clear to me on the following in red:

 

rgb_filename.jpg

rgb_filename.png
rgb_filename.tif
then flatten all layers and convert image to CMYK(created an action for this)

then save to:

cmyk_filename.tif

cmyk_filename.jpg

 

So the rgb side is all good, nothing needed in the script, is that right? Or do you also need the script to save these formats as well?

 

You made an action for the flatten and convert to CMYK... What about 16 bpc to 8 bpc, easy enough to add an action step...

 

At the minimum, the script will rename the prefix of rgb_ to cmyk_ and save the TIFF and JPEG versions... But what file format options (LZW, ZIP etc)? Where will the files be saved (same folder as original file, different location, to a sub-folder etc)?

 

Finally, the original layered RGB files are being flattened and converted to CMYK, do you just wish to close down the original files without saving over the top of them (as you would no longer have layers or RGB).

dereke12252909
Participating Frequently
October 18, 2019

correct! the RGB side is automated with the actions. my main issue was after converting them to CMYK was renaming them from RGB_x to CMKY_x. my current process right now looks like

 

after this process i have to save the CMYK_x.tif & CMYK_x.jpg individually because i lack the ability to automate a rename.

Stephen Marsh
Community Expert
Community Expert
October 18, 2019

So I’ll just assume that the file is closed without saving after the CMYK saves?

 

Or save the RGB file before creating the CMYK versions, then close?

 

Or save the RGB file, create the CMYK versions and revert to the last save?

 

Or something else?

 

 I am obviously concerned about the state of the file after the flatten and CMYK steps...

 

 

Stephen Marsh
Community Expert
Community Expert
October 17, 2019

A custom script can be created, as an action can't change the filename. More info would be required about save options/methods and output locations... however, there is another option! Using the Image Processor Pro script, you can save multiple images in different formats with different names and actions etc. The great thing is that as this is an automation script, it can all be recorded into an action (the step will record blank but all settings will be included). Then the action can be run from a keyboard shortcut etc.

 

https://sourceforge.net/projects/ps-scripts/files/Image%20Processor%20Pro/v3_2%20betas/