Does anyone have a script that utilizes a CSV to create text layers?

New Here ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

I work in Post-Production and am trying to streamline my legal line creation process using a CSV.  I have very limited Javascript experience and am hopeful that someone has a method to auto create text layers using a CSV OR could help point me in the right direction.

 

Thank you

TOPICS
Actions and scripting , Cross-app workflows , macOS

Views

67

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
community guidelines
Adobe Community Professional ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

Photoshop has a built in Variable feature to populate existing text placeholder layers with content from a comma or tab delimited text file.

 

Otherwise, yes, a script can read from the .csv and create a text layer and content.

 

It would be helpful if you could provide sample files or before and after screenshots to illustrate, as there are more questions than answers at the moment.

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
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

I know how to use a CSV to replace exisiting variables.  What I am hoping to accomplish is have a blank canvas with no layers, have photoshop read a CSV and create multiple text layers for me to export.  Rather than me creating a new layer and then copy/pasting each new legal line from a word document. 

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
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Attached images

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
community guidelines
Engaged ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

I have a number of scripts that do things with CSV data, for Photoshop and other apps, but not any published yet that do precisely what you describe. It is certainly possible. The one Photoshop script of mine that is close is described in this video: https://youtu.be/zsaVUHEi6X0

I have two other scripts in progress at this time for some clients that work on multiple text layers from multiple CSV columns. Nothing that creates the layer, but that's not a big deal. The scripts I've made the clients had the layer already because they wanted to style it with effects. That's a good reason to have a layer exist already. I do custom scripts for hire to exact client needs. If interested message me through my website http://www.marspremedia.com/contact/

 

William Campbell

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
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Wow, this is really cool for me, I have been watching your tutorials all week.  Thank you for your response!  I'll keep trying with your tutorials for now. 

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
community guidelines
Engaged ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

That's great! Thanks for watching. Is this a script you want to write yourself? And just need some help? I don't usually post entire scripts because some of mine are thousands of lines, but I'll be happy to post code for a function that parses CSV if you want. Or other specifics tasks. Making a layer isn't too difficult. I think my script tutorial #6 video has some about that. But if not enough, we're always ready to answer questions.

 

William Campbell

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
community guidelines
New Here ,
Aug 06, 2022 Aug 06, 2022

Copy link to clipboard

Copied

Yeah, I am looking to write it myself! Mostly because I want to be able to
automate more tasks that come up in the future. But I need a script
because recording actions wouldn't really be scalable for the company I am
working for. That would be very helpful! I am using scripting listener as
recommended by your videos. But anything would be helpful!

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
community guidelines
Engaged ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

LATEST

Writing your own code is a great way to go. I'll be glad to help however possible. To start, here is my function for parsing CSV:

function parseCsv(data, delimiter) {
    // data: String = contents of a CSV file
    // delimiter: character that separates columns
    //            undefined defaults to comma
    // Returns: Array [[String row, String column]]
    var c = ""; // Character at index.
    var d = delimiter || ","; // Default to comma.
    var endIndex = data.length;
    var index = 0;
    var maxIndex = endIndex - 1;
    var q = false; // "Are we in quotes?"
    var result = []; // Array of rows (array of column arrays).
    var row = []; // Array of columns.
    var v = ""; // Column value.
    while (index < endIndex) {
        c = data[index];
        if (q) { // In quotes.
            if (c == "\"") {
                // Found quote; look ahead for another.
                if (index < maxIndex && data[index + 1] == "\"") {
                    // Found another quote means escaped.
                    // Increment and add to column value.
                    index++;
                    v += c;
                } else {
                    // Next character not a quote; last quote not escaped.
                    q = !q; // Toggle "Are we in quotes?"
                }
            } else {
                // Add character to column value.
                v += c;
            }
        } else { // Not in quotes.
            if (c == "\"") {
                // Found quote.
                q = !q; // Toggle "Are we in quotes?"
            } else if (c == "\n" || c == "\r") {
                // Reached end of line.
                // Test for CRLF.
                if (c == "\r" && index < maxIndex) {
                    if (data[index + 1] == "\n") {
                        // Skip trailing newline.
                        index++;
                    }
                }
                // Column and row complete.
                row.push(v);
                v = "";
                // Add row to result if first row or length matches first row.
                if (result.length == 0 || row.length == result[0].length) {
                    result.push(row);
                }
                row = [];
            } else if (c == d) {
                // Found comma; column complete.
                row.push(v);
                v = "";
            } else {
                // Add character to column value.
                v += c;
            }
        }
        if (index == maxIndex) {
            // Reached end of data; flush.
            if (v.length || c == d) {
                row.push(v);
            }
            // Add row to result if length matches first row.
            if (row.length == result[0].length) {
                result.push(row);
            }
            break;
        }
        index++;
    }
    return result;
}

To use, open the CSV file and read it into a string. Then pass the string to the function. The delimiter is optional. If null it defaults to comma (some European countries use semicolon instead).

var file = new File("path/to/yourfile.csv");
file.open("r");
var data = file.read();
file.close();
var dataArray = parseCsv(data);
// Extract header row.
var header = dataArray.shift();

The result is a two-dimensional array [[row, column]]. Most likely the file has a header row describing what the columns are. So I usually extract that first, and either use the values if they might vary, but likely you know what the column heads are so just dump it. Now row zero of the data array is the first row of actual data to process. Now to do something with it. Let's say the two columns are 'file name' and 'text'.

var fileName;
var text;
for (var i = 0; i < dataArray.length; i++) {
    fileName = dataArray[i][0];
    text = dataArray[i][1];

    // Do some process here.
    // For example, open 'fileName'
    // Or dupe a master doc to save as 'fileName'
    // Create a text layer and set the contents to 'text' for example.
    
}

My tutorial #6 shows a working example of adding a text layer to a folder of images, but not with CSV. It helps illustrate the idea though. Have a look at that.

https://youtu.be/GIZmCp4SuKM

William Campbell

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
community guidelines