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

Using datasets

Community Beginner ,
Apr 29, 2010 Apr 29, 2010

Copy link to clipboard

Copied

Hi,

In a photoshop dataset, I wish to do the following-

1. save as .jpg

2. name the file using a variable used in the document

how can this be done? i am using cs4

TOPICS
Actions and scripting

Views

4.0K
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 3 Correct answers

Guru , Apr 30, 2010 Apr 30, 2010

Here is one way to save the sets as jpgs. It assumes that the activeDocument has datasets defined and uses the first fieldname in the dataset csv as the savename for the jpg.

fileImportDataSets = function(file) { 
    var desc = new ActionDescriptor(); 
        var ref = new ActionReference(); 
        ref.putClass( stringIDToTypeID( "dataSetClass" ) ); 
    desc.putReference( charIDToTypeID( "null" ), ref ); 
    desc.putPath( charIDToTypeID( "Usng" ), new File( file ) ); 
    desc.putEnumerate
...

Votes

Translate
Community Beginner , Jun 24, 2019 Jun 24, 2019

I got it working by checking the listener for the menu item's apply dataset

Copied that into my code and it works fine

However, I have various dataset files in an array and processing it and saving

For some reason its only doing the 1st file and not cycling through the rest of the files
(no errors, but only does the 1st dataset file)

Votes

Translate
LEGEND , Sep 09, 2021 Sep 09, 2021

I corrected the script that now it works.

Votes

Translate
Adobe
Community Beginner ,
Feb 19, 2025 Feb 19, 2025

Copy link to clipboard

Copied

@Stephen Marsh I have created a video that illustrates my point. Please see the attached video. I hope it makes sense now.

 

Here are the two things I wish to achieve:

  1. I do not want to replace text layers, but image layers
  2. Instead of File>Export DataSets as Files, I want to Export the Linked DataSets as JPGs

Votes

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 ,
Feb 19, 2025 Feb 19, 2025

Copy link to clipboard

Copied

quote

Stephen Marsh I have created a video that illustrates my point. Please see the attached video. I hope it makes sense now.

 

Here are the two things I wish to achieve:

  1. I do not want to replace text layers, but image layers
  2. Instead of File>Export DataSets as Files, I want to Export the Linked DataSets as JPGs

By iRushab

 

I'm standing on the shoulders of giants, who are sadly departed.

 

The referenced script is for saving the data sets directly as JPEG images, with the first column in the spreadsheet used for the suffix, which is taken from a text layer.

 

I haven't tested with pixel replacement, but it should be the same.

 

Let me try and come back to you...

Votes

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 ,
Feb 19, 2025 Feb 19, 2025

Copy link to clipboard

Copied

@iRushab 

 

It works as I expect.

 

1) Here is my test spreadsheet, saved as a CSV:

 

2025-02-19_21-41-32.pngexpand image

 

 

2) This is the PSD data set template and layers. The black area on the left-hand side is the placehoder for the pixel replacement. The text layer variable linked to the first column data set is invisible, it's only used for naming the output files:

 

2025-02-19_21-43-32.jpgexpand image

 

3) These are the variables and data sets (animation):

 

2025-02-19_21-46-38.gifexpand image

 

 

4) This is the output of the script:

 

2025-02-19_21-50-17.pngexpand image

 

The text from the first column is simply used to name the variable suffix after the static template filename:

 

pix-replacement_SKY.jpg

pix-replacement_WATER.jpg

 

 

Votes

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 ,
Feb 20, 2025 Feb 20, 2025

Copy link to clipboard

Copied

@iRushab 

 

This updated 1.2 version removes the requirement for having a text layer linked to the first column of the spreadsheet for the variable filename suffix. The script will automatically use the first column for the suffix, simplifying the script:

 

/* 
Photoshop Variables Data Sets to JPEG 2025 Update.jsx
v1.1 - A text layer was required to be mapped to the first column in the CSV file for use as the suffix for the JPEG file names.
v1.2 - The first column in the CSV file is now automatically used as the suffix for the JPEG file names.
Updated and extended by Stephen Marsh, 18th February 2025 - tested with v2019, v2020, v2021, v2024 and v2025 on Mac and Windows.
Original script by Mike Hale, 2010
~ The active document must have Data Sets already defined. 
~ The first column in the CSV will be used as the suffix for the JPEG file names.
https://community.adobe.com/t5/photoshop-ecosystem-discussions/using-datasets/td-p/2665594
https://forums.adobe.com/thread/628182
https://forums.adobe.com/message/2773935#2773935
*/

#target photoshop;

// Adjust the following "user friendly" variables as required. A GUI for these file format options isn't planned!

var separator = '_'; // '_' | ' ' | '-' (underscore, space or hyphen)

var jpegEmbedColorProfile = true; // Boolean: true | false
var jpegFormatOptions = FormatOptions.STANDARDBASELINE; // FormatOptions.STANDARDBASELINE | FormatOptions.OPTIMIZEDBASELINE | FormatOptions.PROGRESSIVE
var jpegMatte = MatteType.NONE; // MatteType.NONE | MatteType.WHITE | MatteType.BLACK
var jpegQuality = 10; // Numeric: 0 - 12 (low quality to highest quality)


///// FUNCTIONS /////

function fileImportDataSets(file) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("dataSetClass"));
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putPath(charIDToTypeID("Usng"), new File(file));
    desc.putEnumerated(charIDToTypeID("Encd"), stringIDToTypeID("dataSetEncoding"), stringIDToTypeID("dataSetEncodingAuto"));
    desc.putBoolean(stringIDToTypeID("eraseAll"), true); // Remove all existing data sets
    desc.putBoolean(stringIDToTypeID("useFirstColumn"), true);
    executeAction(stringIDToTypeID("importDataSets"), desc, DialogModes.NO);
};

function applyDataSet(setName) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putName(s2t("dataSetClass"), setName);
    descriptor.putReference(s2t("null"), reference);
    executeAction(s2t("apply"), descriptor, DialogModes.NO);
};

function getDataSetNames(csvFileRef) {
    var _ftn = function (string) {
        var csvItems = string.split(",");
        //var datasetName = csvItems[0];
        var datasetName = csvItems[0].replace(/(^['"]|['"]$)/g, ''); // Clean both single and double quote-escaped values
        return datasetName;
    };
    csvFileRef.open();
    var datasetArray = [];
    var i = 0;
    var csvString;
    while (!csvFileRef.eof) {
        csvString = csvFileRef.readln();
        // Skip empty lines
        if (csvString.length < 2) continue;
        datasetArray[i] = _ftn(csvString);
        i++;
    }
    csvFileRef.close();
    return datasetArray;
};

function padNumber(number) {
    var str = number.toString();
    while (str.length < 4) {
        str = "0" + str;
    }
    return str;
};

// File format functions
function jpegSaveOptions() {
    var jpgOptions = new JPEGSaveOptions();
    jpgOptions.formatOptions = jpegFormatOptions;
    jpgOptions.embedColorProfile = jpegEmbedColorProfile;
    jpgOptions.matte = jpegMatte;
    jpgOptions.quality = jpegQuality;
    return jpgOptions;
};

///// SCRIPT EXECUTION /////

try {
    if (app.documents.length > 0) {
        (function () {

            // Confirm with the user that the file meets the criteria
            if (!confirm("Data Sets must be created before running this script. Continue?", false)) {
                return;
            }

            // Prompt and import the CSV file into the template
            var csvFileRef = File.openDialog("Please select CSV file:");
            // Test if Cancel button returns null, then do nothing
            if (csvFileRef === null) {
                //app.beep();
                return;
            }
            fileImportDataSets(csvFileRef);

            // Select the save folder
            var saveFolder = Folder.selectDialog("Please select the output folder to save the files to:");
            // Test if Cancel button returns null, then do nothing
            if (saveFolder === null) {
                //app.beep();
                return;
            }

            // Set up the dataset array
            var datasetNames = getDataSetNames(csvFileRef);

            // JPEG Save options  
            var jpgOptions = jpegSaveOptions();

            // Initialize the counter for the saved files
            var fileCounter = 0;

            // Loop over the data sets
            for (i = 1; i < datasetNames.length; i++) {

                applyDataSet(datasetNames[i]);

                // Remove the filename extension
                var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');

                // The first column in the CSV to be used as the filename suffix
                var suffix = getDataSetNames(csvFileRef)[i].replace(/.*\//, '').replace(/\.[^\.]+$/, '');
                // The dataset index is used as the filename suffix
                // var suffix = padNumber(i); // This will create "0001", "0002", "0003", "0010", "0100" etc.

                // Save the file
                var jpgSaveFile = new File(saveFolder + "/" + docName + separator + suffix + ".jpg");
                app.activeDocument.saveAs(jpgSaveFile, jpgOptions, true, Extension.LOWERCASE);

                // Increment the counter
                fileCounter++;
            }

            // End of script notification
            app.beep();
            alert(fileCounter + " JPEG files have been saved to:" + "\n" + saveFolder.fsName);

            // Close the template without saving so as not to affect the original data sets
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

        })();
    } else {
        app.beep();
        alert('A template file must be open before running this script...');
    }
} catch (error) {
    app.beep();
    alert(error + ', Line: ' + error.line);
}

 

 

Votes

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 Beginner ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

@Stephen Marsh Thanks for your effort. This is the error I get.

 

Screenshot 2025-02-21 at 15.10.28.pngexpand image

Votes

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 ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

@iRushab 

 

That error would be on the JPEG save. It works for me in all tested versions of Photoshop.

 

What is the first column in your spreadsheet? Please share the CSV or screenshot the text file or spreadsheet 1st column.

Votes

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 Beginner ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

Here it is. I have one column with the pixel replacement information.

Screenshot 2025-02-21 at 16.54.07.pngexpand image

Votes

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 ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

Ah, I only tested with relative paths:

 

my file.jpg

 

Not with full absolute paths:

 

/some location/some folder/my file.jpg

 

I'm guessing that it's choking on the directory forward-slash separators, which would create an illegal filename.

 

I'll revise the code so that only the content after the last directory separator is used.

 

EDIT: I have updated the previous code, please try again... This is for the Mac, I haven't tested Windows back-slash \ path separators yet!

Votes

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 Beginner ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied

Yay! It worked. Thanks  @Stephen Marsh

 

I assume that in the future, I can have multiple columns in the CSV file and replace both text and images. As long as the datasets are defined, the script should function for both pixel and text replacements.

Votes

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 ,
Feb 21, 2025 Feb 21, 2025

Copy link to clipboard

Copied


iRushab wrote:

Yay! It worked. Thanks  Stephen Marsh

 

Fantastic!

 

Yes, I have tested it for both text and pixel replacement with multiple columns.

 

The prerequisites are:

 

* Variables/Data Sets must be defined, for consistency I advise using the same CSV as used when running the script to initially create the data sets.

 

* The first column in the CSV will be used for the filename suffix, it therefore must have valid, legal characters.

 

There is placeholder code in the script to use the data set index numbers for the suffix rather than the spreadsheet first column, although it's probably easier to just have the first column using sequential numbers.

Votes

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 ,
Mar 04, 2025 Mar 04, 2025

Copy link to clipboard

Copied

LATEST

I have been working on a generic GUI version of the Data Sets to JPEG script, allowing users to select from JPEG, PNG, WEBP, TIFF and PSB output file formats. Although I could have added PSD, this is covered by the native Data Set export functionality.

 

Data Sets to Files v1-0.pngexpand image

 

/*
Photoshop Variables Data Sets to Files GUI v1-0.jsx
v1.0 - 4rd March 2025, Initial Release
Updated and extended by Stephen Marsh, tested with v2019, v2020, v2021, v2024 and v2025 on Mac and Windows.
Original script by Mike Hale, 2010
NOTE: The active document must have Data Sets already defined.
https://community.adobe.com/t5/photoshop-ecosystem-discussions/using-datasets/td-p/2665594
https://forums.adobe.com/thread/628182
https://forums.adobe.com/message/2773935#2773935
*/

#target photoshop

// Adjust the following "user friendly" variables as required. A GUI for these file format options isn't planned!

// Filename suffix separator
var separator = '_'; // '_' | ' ' | '-' (underscore, space or hyphen)

// saveJPEG global variables
var jpegEmbedColorProfile = true; // Boolean: true | false
var jpegFormatOptions = FormatOptions.STANDARDBASELINE; // FormatOptions.STANDARDBASELINE | FormatOptions.OPTIMIZEDBASELINE | FormatOptions.PROGRESSIVE
var jpegMatte = MatteType.NONE; // MatteType.NONE | MatteType.WHITE | MatteType.BLACK
var jpegQuality = 10; // Numeric: 0 - 12 (low quality to highest quality)

// savePNG global variables
var pngCompression = 1; // Numeric: 0 - 9 (low compression to highest)
var pngInterlaced = false; // Boolean: true | false

// saveWebP global variables
var webPCompressionType = "compressionLossy"; // String: "compressionLossless" | "compressionLossy"
var webPCompIsLossless = false; // Boolean: true | false
var webPQuality = 75; // Numeric: 0 - 100 (low quality to highest quality)
var webPIncludeXMPData = true; // Boolean: true | false
var webPIncludeEXIFData = false; // Boolean: true | false
var webPIncludePsExtras = false; // Boolean: true | false
var webPLowerCase = true; // Boolean: true | false
var webPEmbedProfiles = true; // Boolean: true | false

// saveTIFF global variables
var tiffEmbedColorProfile = true; // Boolean: true | false
var tiffByteOrder = ByteOrder.IBM; // ByteOrder.MACOS | ByteOrder.IBM
var tiffTransparency = true; // Boolean: true | false
var tiffLayerCompression = LayerCompression.ZIP; // LayerCompression.RLE | LayerCompression.ZIP
var tiffInterleaveChannels = true; // Boolean: true | false
var tiffAlphaChannels = true; // Boolean: true | false
var tiffAnnotations = true; // Boolean: true | false
var tiffSpotColors = true; // Boolean: true | false
var tiffSaveLayers = true; // Boolean: true | false
var tiffSaveImagePyramid = false; // Boolean: true | false
var tiffImageCompression = TIFFEncoding.TIFFLZW; // TIFFEncoding.NONE | TIFFEncoding.JPEG | TIFFEncoding.TIFFLZW | TIFFEncoding.TIFFZIP


///// GUI FUNCTION /////

try {
    var createDialog = function () {

        // Confirm with the user that the file meets the criteria
        if (!confirm("Data Sets must be created before running this script. Continue?", false)) {
            return;
        }

        // Create the dialog window
        var dlg = new Window('dialog', 'Data Sets to Files (v1.0)');
        dlg.preferredSize = [500, 350];

        // Create main panel to hold input controls
        dlg.mainPanel = dlg.add('panel', undefined, '');
        dlg.mainPanel.alignment = ['fill', 'fill'];
        dlg.mainPanel.margins = [15, 15, 15, 15];
        dlg.mainPanel.spacing = 10;
        dlg.mainPanel.alignChildren = 'left';

        // CSV File button and path
        var csvGroup = dlg.mainPanel.add('group');
        csvGroup.orientation = 'column';
        csvGroup.alignChildren = 'fill';
        csvGroup.alignment = ['fill', 'top'];
        csvGroup.add('statictext', undefined, 'Select the Data Set .csv file to import:');
        var csvBtnGroup = csvGroup.add('group');
        csvBtnGroup.alignment = ['fill', 'top'];
        csvBtnGroup.alignChildren = 'left';
        dlg.mainPanel.csvFileBtn = csvBtnGroup.add('button', undefined, 'Browse...', { name: 'browse' });
        dlg.mainPanel.csvFileBtn.preferredSize.width = 100;

        // Add a panel for the CSV file path
        var csvPathPanel = csvGroup.add('panel');
        csvPathPanel.alignment = ['fill', 'top'];
        csvPathPanel.margins = 10;
        dlg.mainPanel.csvFilePath = csvPathPanel.add('statictext', undefined, 'No file selected', { truncate: "middle" });
        dlg.mainPanel.csvFilePath.alignment = ['fill', 'top'];
        dlg.mainPanel.csvFilePath.minimumSize.width = 350;

        // Output Folder button and path
        var outputGroup = dlg.mainPanel.add('group');
        outputGroup.orientation = 'column';
        outputGroup.alignChildren = 'fill';
        outputGroup.alignment = ['fill', 'top'];
        outputGroup.add('statictext', undefined, 'Select the output folder:');
        var outputBtnGroup = outputGroup.add('group');
        outputBtnGroup.alignment = ['fill', 'top'];
        outputBtnGroup.alignChildren = 'left';
        dlg.mainPanel.outputFolderBtn = outputBtnGroup.add('button', undefined, 'Browse...', { name: 'browse' });
        dlg.mainPanel.outputFolderBtn.preferredSize.width = 100;

        // Add a panel for the output folder path
        var outputPathPanel = outputGroup.add('panel');
        outputPathPanel.alignment = ['fill', 'top'];
        outputPathPanel.margins = 10;
        dlg.mainPanel.outputFolderPath = outputPathPanel.add('statictext', undefined, 'No folder selected', { truncate: "middle" });
        dlg.mainPanel.outputFolderPath.alignment = ['fill', 'top'];
        dlg.mainPanel.outputFolderPath.minimumSize.width = 350;

        // File Format Group
        var formatGroup = dlg.mainPanel.add('group');
        formatGroup.orientation = 'column';
        formatGroup.alignChildren = 'left';
        formatGroup.alignment = ['left', 'top'];

        // Add a static text label
        formatGroup.add('statictext', undefined, 'Select the output file format:');

        // Add a group for the dropdown and description
        var formatDropdownGroup = formatGroup.add('group');
        formatDropdownGroup.orientation = 'row';
        formatDropdownGroup.alignChildren = 'left';

        // Create the conditional dropdown options array based on Photoshop version 2022 check/test
        var formatOptions = ["JPEG", "PNG", "TIFF", "PSB"];
        if (parseFloat(app.version) >= 23) {
            formatOptions.splice(2, 0, "WEBP"); // Insert into the array at index 2 (3rd position)
        }

        // Add a dropdown list for format selection using the conditional format options
        dlg.mainPanel.formatList = formatDropdownGroup.add('dropdownlist', undefined, formatOptions);

        // Default selection to the first entry (JPEG)
        dlg.mainPanel.formatList.selection = 0;

        // Set the preferred width for the dropdown list
        dlg.mainPanel.formatList.preferredSize.width = 70;

        // Add a static text label that updates with format selection
        dlg.mainPanel.formatDescription = formatDropdownGroup.add('statictext', undefined, '(' + jpegFormatOptions.toString().replace(/FormatOptions\./, '') + ' Format Option, Quality ' + jpegQuality + ')');

        // Add a static text label for filename suffix options
        formatGroup.add('statictext', undefined, 'Select the filename suffix:');

        // Add a dropdown list for filename suffix options
        dlg.mainPanel.suffixList = formatGroup.add('dropdownlist', undefined, ['First Column in CSV', 'Dataset Index No.']);

        // Default selection to 'First column in CSV'
        dlg.mainPanel.suffixList.selection = 0;

        // Set the preferred width for the suffix dropdown list
        dlg.mainPanel.suffixList.preferredSize.width = 160;

        // Create button group for Cancel and OK buttons
        var buttonGroup = dlg.add('group');
        buttonGroup.orientation = 'row';
        buttonGroup.alignment = 'right';
        buttonGroup.spacing = 5;
        dlg.cancelBtn = buttonGroup.add('button', undefined, 'Cancel', { name: 'cancel' });
        dlg.okBtn = buttonGroup.add('button', undefined, 'OK', { name: 'ok' });

        // Update the static text when the dropdown selection changes
        dlg.mainPanel.formatList.onChange = function () {
            var selectedFormat = dlg.mainPanel.formatList.selection.text;
            var descriptions = {
                'JPEG': '(' + jpegFormatOptions.toString().replace(/FormatOptions\./, '') + ' Format Option, Quality ' + jpegQuality + ')',
                'PNG': '(Lossless Compression ' + pngCompression + ', Supports Transparency)',
                'WEBP': '(Lossy Compression ' + webPQuality + ', Supports Transparency)',
                'TIFF': '(' + tiffByteOrder.toString().replace(/ByteOrder\./, '') + ' Byte Order, ' + tiffImageCompression.toString().replace(/TIFFEncoding\./, '') + ' Compression)',
                'PSB': '(Large Document Format)'
            };
            dlg.mainPanel.formatDescription.text = descriptions[selectedFormat];
        };

        // Return the dialog
        return dlg;
    };

} catch (error) {
    alert(error + ', Line: ' + error.line);
}


///// HELPER FUNCTIONS /////

function fileImportDataSets(file) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putClass(stringIDToTypeID("dataSetClass"));
    desc.putReference(charIDToTypeID("null"), ref);
    desc.putPath(charIDToTypeID("Usng"), new File(file));
    desc.putEnumerated(charIDToTypeID("Encd"), stringIDToTypeID("dataSetEncoding"), stringIDToTypeID("dataSetEncodingAuto"));
    desc.putBoolean(stringIDToTypeID("eraseAll"), true);
    desc.putBoolean(stringIDToTypeID("useFirstColumn"), true);
    executeAction(stringIDToTypeID("importDataSets"), desc, DialogModes.NO);
};

function applyDataSet(setName) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putName(s2t("dataSetClass"), setName);
    descriptor.putReference(s2t("null"), reference);
    executeAction(s2t("apply"), descriptor, DialogModes.NO);
};

function getDataSetNames(csvFileRef) {
    var _ftn = function (string) {
        var csvItems = string.split(",");
        var datasetName = csvItems[0].replace(/(^['\"]|['\"]$)/g, ''); // Clean both single and double quote-escaped values
        return datasetName;
    };
    csvFileRef.open();
    var datasetArray = [];
    var i = 0;
    var csvString;
    while (!csvFileRef.eof) {
        csvString = csvFileRef.readln();
        // Skip empty lines
        if (csvString.length < 2) continue;
        datasetArray[i] = _ftn(csvString);
        i++;
    }
    csvFileRef.close();
    return datasetArray;
};

function paddedSetNo(number) {
    var str = number.toString();
    // 4-digit padding (e.g., 1 = 0001, 23 = 0023)
    while (str.length < 4) {
        str = "0" + str;
    }
    return str;
}

function jpegSaveOptions() {
    var jpgOptions = new JPEGSaveOptions();
    jpgOptions.formatOptions = jpegFormatOptions;
    jpgOptions.embedColorProfile = jpegEmbedColorProfile;
    jpgOptions.matte = jpegMatte;
    jpgOptions.quality = jpegQuality;
    return jpgOptions;
};

function pngSaveOptions() {
    var pngOptions = new PNGSaveOptions();
    pngOptions.compression = pngCompression;
    pngOptions.interlaced = pngInterlaced;
    return pngOptions;
};

function saveWebP(saveFile) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor2.putEnumerated(s2t("compression"), s2t("WebPCompression"), s2t(webPCompressionType));
    if (webPCompIsLossless == false) {
        descriptor2.putInteger(s2t("quality"), webPQuality);
    }
    // Metadata options
    descriptor2.putBoolean(s2t("includeXMPData"), webPIncludeXMPData);
    descriptor2.putBoolean(s2t("includeEXIFData"), webPIncludeEXIFData);
    descriptor2.putBoolean(s2t("includePsExtras"), webPIncludePsExtras);
    // WebP format and save path
    descriptor.putObject(s2t("as"), s2t("WebPFormat"), descriptor2);
    descriptor.putPath(s2t("in"), saveFile);
    // The extension
    descriptor.putBoolean(s2t("lowerCase"), webPLowerCase);
    // Embed color profile
    descriptor.putBoolean(s2t("embedProfiles"), webPEmbedProfiles);
    // Execute the save
    executeAction(s2t("save"), descriptor, DialogModes.NO);
}

function saveTIFF(saveFile) {
    var tiffSaveOptions = new TiffSaveOptions();
    tiffSaveOptions.embedColorProfile = tiffEmbedColorProfile;
    tiffSaveOptions.byteOrder = tiffByteOrder;
    tiffSaveOptions.transparency = tiffTransparency;
    tiffSaveOptions.layers = tiffSaveLayers;
    tiffSaveOptions.layerCompression = tiffLayerCompression;
    tiffSaveOptions.interleaveChannels = tiffInterleaveChannels;
    tiffSaveOptions.alphaChannels = tiffAlphaChannels;
    tiffSaveOptions.annotations = tiffAnnotations;
    tiffSaveOptions.spotColors = tiffSpotColors;
    tiffSaveOptions.saveImagePyramid = tiffSaveImagePyramid;
    tiffSaveOptions.imageCompression = tiffImageCompression;
    // Execute the save
    app.activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
}

function savePSB(saveFile) {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor2.putBoolean(s2t("maximizeCompatibility"), true);
    descriptor.putObject(s2t("as"), s2t("largeDocumentFormat"), descriptor2);
    descriptor.putPath(s2t("in"), saveFile);
    descriptor.putBoolean(s2t("lowerCase"), true);
    descriptor.putBoolean(s2t("layers"), true);
    // Execute the save
    executeAction(s2t("save"), descriptor, DialogModes.NO);
}


///// MAIN SCRIPT EXECUTION /////

try {
    if (app.documents.length > 0) {

        var dlg = createDialog();

        // CSV selection button
        dlg.mainPanel.csvFileBtn.onClick = function () {
            var csvFileRef = File.openDialog("Select CSV file:");
            if (csvFileRef !== null) {
                dlg.mainPanel.csvFilePath.text = csvFileRef.fsName;
            }
        };

        // Output folder selection button
        dlg.mainPanel.outputFolderBtn.onClick = function () {
            var saveFolder = Folder.selectDialog("Select the output folder to save the files to:");
            if (saveFolder !== null) {
                dlg.mainPanel.outputFolderPath.text = saveFolder.fsName;
            }
        };

        // Generate the dialog
        if (dlg.show() === 1) {
            // File and folder variables
            var csvFileRef = new File(dlg.mainPanel.csvFilePath.text);
            var saveFolder = new Folder(dlg.mainPanel.outputFolderPath.text);
            var selectedFormat = dlg.mainPanel.formatList.selection.text;
            var selectedSuffixOption = dlg.mainPanel.suffixList.selection.index;

            // Import the data sets from the CSV file
            fileImportDataSets(csvFileRef);

            // Variables for the loop
            var datasetNames = getDataSetNames(csvFileRef);
            var fileCounter = 0;
            var originalDocName = app.activeDocument.name.replace(/\.[^\.]+$/, ''); // Capture the base name once

            // Loop through the dataset names and save each file
            for (var i = 1; i < datasetNames.length; i++) {
                applyDataSet(datasetNames[i]);

                // Define suffix based on selection
                var suffix = '';
                // First column in CSV
                if (selectedSuffixOption === 0) {
                    suffix = getDataSetNames(csvFileRef)[i].replace(/.*\//, '').replace(/\.[^\.]+$/, '');
                    // 4-digit padded set number
                } else if (selectedSuffixOption === 1) {
                    suffix = paddedSetNo(i);
                }

                // Construct the filename
                var fileName = originalDocName + separator + suffix;
                var extension = '';
                if (selectedFormat === 'JPEG') {
                    extension = '.jpg';
                } else if (selectedFormat === 'PNG') {
                    extension = '.png';
                } else if (selectedFormat === 'WEBP') {
                    extension = '.webp';
                } else if (selectedFormat === 'TIFF') {
                    extension = '.tif';
                } else if (selectedFormat === 'PSB') {
                    extension = '.psb';
                }

                // Save folder and file name
                var saveFile = new File(saveFolder + "/" + fileName + extension);

                // Save the file with the selected format
                if (selectedFormat === 'JPEG') {
                    app.activeDocument.saveAs(saveFile, jpegSaveOptions(), true, Extension.LOWERCASE);
                } else if (selectedFormat === 'PNG') {
                    app.activeDocument.saveAs(saveFile, pngSaveOptions(), true, Extension.LOWERCASE);
                } else if (selectedFormat === 'WEBP') {
                    saveWebP(saveFile);
                } else if (selectedFormat === 'TIFF') {
                    saveTIFF(saveFile);
                } else if (selectedFormat === 'PSB') {
                    savePSB(saveFile);
                }

                // Increment the file counter
                fileCounter++;
            }

            // End of script
            app.beep();
            alert(fileCounter + " " + selectedFormat + " files have been saved to:" + "\n" + saveFolder.fsName);
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    } else {
        alert('A template file must be open before running this script...');
    }
} catch (error) {
    alert(error + ', Line: ' + error.line);
}

 

  1. Copy the code text to the clipboard
  2. Open a new blank file in a plain-text editor (not in a word processor)
  3. Paste the code in
  4. Save as a plain text format file – .txt
  5. Rename the saved file extension from .txt to .jsx
  6. Install or browse to the .jsx file to run (see below):

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

Votes

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