Using datasets

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

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.putEnumerated( charIDToTypeID( "Encd" ), stringIDToTypeID( "dataSetEncoding" ), stringIDToTypeID( "dataSetEncodingAuto" ) );
    desc.putBoolean( stringIDToTypeID( "eraseAll" ), true );
    desc.putBoolean( stringIDToTypeID( "useFirstColumn" ), true );
executeAction( stringIDToTypeID( "importDataSets" ), desc, DialogModes.NO );
}
applyDataSet = function(setName) {
    var desc = new ActionDescriptor();
        var setRef = new ActionReference();
        setRef.putName( stringIDToTypeID( "dataSetClass" ), setName );
    desc.putReference( charIDToTypeID( "null" ), setRef );
executeAction( charIDToTypeID( "Aply" ), desc, DialogModes.NO );
}
getDataSetNames = function(csvFileRef) {
     _ftn = function(string){
          var csvItems = string.split(",");
          datasetName = csvItems[0]          
          return datasetName;
     }
     csvFileRef.open();
     var datasetArray = new Array();
     var i = 0;
     while (csvString = csvFileRef.readln()) {
          if (csvString.length < 2) continue; // Skip empty lines
       datasetArray = _ftn(csvString);
       i++;
     }
     csvFileRef.close();
     return datasetArray;
}
// jpeg options used for all the saves
var jpgOptns = new JPEGSaveOptions();
     jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
     jpgOptns.embedColorProfile = true;
     jpgOptns.matte = MatteType.NONE;
     jpgOptns.quality = 8;
//prompt for file
var csvFileRef = File.openDialog("Please select CSV file");
fileImportDataSets(csvFileRef); //inport the CSV file into the template
var datasetNames = getDataSetNames(csvFileRef);// set up the dataset array
//setup a loop for your data set names
for (i=1; i < datasetNames.length; i++) {
     applyDataSet(datasetNames);
     //edit the file if needed
     //save the file. Here I use testItem.contents of layer 'name' as the filename
     app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("name");
     jpgSaveFile = new File(csvFileRef.path + "/" + app.activeDocument.activeLayer.textItem.contents + ".jpg");
     app.activeDocument.saveAs (jpgSaveFile ,jpgOptns , true, Extension.LOWERCASE);
}//loop to next data set

TOPICS
Actions and scripting

Views

1.3K

Likes

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

Using datasets

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

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.putEnumerated( charIDToTypeID( "Encd" ), stringIDToTypeID( "dataSetEncoding" ), stringIDToTypeID( "dataSetEncodingAuto" ) );
    desc.putBoolean( stringIDToTypeID( "eraseAll" ), true );
    desc.putBoolean( stringIDToTypeID( "useFirstColumn" ), true );
executeAction( stringIDToTypeID( "importDataSets" ), desc, DialogModes.NO );
}
applyDataSet = function(setName) {
    var desc = new ActionDescriptor();
        var setRef = new ActionReference();
        setRef.putName( stringIDToTypeID( "dataSetClass" ), setName );
    desc.putReference( charIDToTypeID( "null" ), setRef );
executeAction( charIDToTypeID( "Aply" ), desc, DialogModes.NO );
}
getDataSetNames = function(csvFileRef) {
     _ftn = function(string){
          var csvItems = string.split(",");
          datasetName = csvItems[0]          
          return datasetName;
     }
     csvFileRef.open();
     var datasetArray = new Array();
     var i = 0;
     while (csvString = csvFileRef.readln()) {
          if (csvString.length < 2) continue; // Skip empty lines
       datasetArray = _ftn(csvString);
       i++;
     }
     csvFileRef.close();
     return datasetArray;
}
// jpeg options used for all the saves
var jpgOptns = new JPEGSaveOptions();
     jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
     jpgOptns.embedColorProfile = true;
     jpgOptns.matte = MatteType.NONE;
     jpgOptns.quality = 8;
//prompt for file
var csvFileRef = File.openDialog("Please select CSV file");
fileImportDataSets(csvFileRef); //inport the CSV file into the template
var datasetNames = getDataSetNames(csvFileRef);// set up the dataset array
//setup a loop for your data set names
for (i=1; i < datasetNames.length; i++) {
     applyDataSet(datasetNames);
     //edit the file if needed
     //save the file. Here I use testItem.contents of layer 'name' as the filename
     app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("name");
     jpgSaveFile = new File(csvFileRef.path + "/" + app.activeDocument.activeLayer.textItem.contents + ".jpg");
     app.activeDocument.saveAs (jpgSaveFile ,jpgOptns , true, Extension.LOWERCASE);
}//loop to next data set

TOPICS
Actions and scripting

Views

1.4K

Likes

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
Apr 29, 2010 0
Apr 30, 2010

Copy link to clipboard

Copied

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.putEnumerated( charIDToTypeID( "Encd" ), stringIDToTypeID( "dataSetEncoding" ), stringIDToTypeID( "dataSetEncodingAuto" ) );
    desc.putBoolean( stringIDToTypeID( "eraseAll" ), true );
    desc.putBoolean( stringIDToTypeID( "useFirstColumn" ), true );
executeAction( stringIDToTypeID( "importDataSets" ), desc, DialogModes.NO );
}
applyDataSet = function(setName) {
    var desc = new ActionDescriptor();
        var setRef = new ActionReference();
        setRef.putName( stringIDToTypeID( "dataSetClass" ), setName );
    desc.putReference( charIDToTypeID( "null" ), setRef );
executeAction( charIDToTypeID( "Aply" ), desc, DialogModes.NO );
}
getDataSetNames = function(csvFileRef) {
     _ftn = function(string){
          var csvItems = string.split(",");
          datasetName = csvItems[0]          
          return datasetName;
     }
     csvFileRef.open();
     var datasetArray = new Array();
     var i = 0;
     while (csvString = csvFileRef.readln()) {
          if (csvString.length < 2) continue; // Skip empty lines
       datasetArray = _ftn(csvString);
       i++;
     }
     csvFileRef.close();
     return datasetArray;
}
// jpeg options used for all the saves
var jpgOptns = new JPEGSaveOptions();
     jpgOptns.formatOptions = FormatOptions.STANDARDBASELINE;
     jpgOptns.embedColorProfile = true;
     jpgOptns.matte = MatteType.NONE;
     jpgOptns.quality = 8;
//prompt for file
var csvFileRef = File.openDialog("Please select CSV file");
fileImportDataSets(csvFileRef); //inport the CSV file into the template
var datasetNames = getDataSetNames(csvFileRef);// set up the dataset array
//setup a loop for your data set names
for (i=1; i < datasetNames.length; i++) {
     applyDataSet(datasetNames);
     //edit the file if needed
     //save the file. Here I use testItem.contents of layer 'name' as the filename
     app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("name");
     jpgSaveFile = new File(csvFileRef.path + "/" + app.activeDocument.activeLayer.textItem.contents + ".jpg");
     app.activeDocument.saveAs (jpgSaveFile ,jpgOptns , true, Extension.LOWERCASE);
}//loop to next data set

Likes

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
Reply
Loading...
Apr 30, 2010 3
Dec 21, 2020

Copy link to clipboard

Copied

Found this linked from another thread and I'm a bit confused by its use. Is it supposed to loop automatically and save multiple JPG's one after the other with the data set data and filename applied?  I tried it but all it does is save the last data set with name after churning a few seconds. I have about 80+ data sets in a CSV on the first column.

Likes

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
Reply
Loading...
Dec 21, 2020 0
Apr 30, 2019

Copy link to clipboard

Copied

I'm getting an error when I run it on line 52,  "Error 1302, No such element. "... app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("name");

Any ideas?

Likes

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
Reply
Loading...
Apr 30, 2019 0
Apr 30, 2019

Copy link to clipboard

Copied

Nevermind! I just renamed my variable from 'filename' to what you had as 'name' and it worked great. This 10 year old java script is a life saver.

Likes

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
Reply
Loading...
Apr 30, 2019 0
May 01, 2019

Copy link to clipboard

Copied

In the ten that yeares Mike Hale has passed but he is still helping us here Rest In Peace Mike thanks....

JJMack

Likes

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
Reply
Loading...
May 01, 2019 2
Jun 21, 2019

Copy link to clipboard

Copied

Hmmm the dataset is not getting applied automatically for me on my PSD template

The window comes up to select the csv, I select it and it saves the jpg. However it saves the jpg with my template as is, without the data applied

I'm having to manually go into the menu under:
Image > Variables > Datasets
then since the dataset is already loaded, I need to click "Apply"
only then it applies the set

Any thoughts why its not happening automatic?

This is with PS 2019 and WIndows 10

Likes

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
Reply
Loading...
Jun 21, 2019 0
Jun 21, 2019

Copy link to clipboard

Copied

The script is best used for "behind the scenes" saving. If you do really want it to update your open PSD, you'll have to continue using the method you mentioned above.

Likes

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
Reply
Loading...
Jun 21, 2019 0
Jun 24, 2019

Copy link to clipboard

Copied

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)

Likes

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
Reply
Loading...
Jun 24, 2019 0
Jun 24, 2019

Copy link to clipboard

Copied

Nevermind, all good and working now

I had 2 nested for loops, each using the same i to increment, duh!

Likes

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
Reply
Loading...
Jun 24, 2019 0