Lookup Value from CSV to become variable in script

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Limey

Lookup Value from CSV to become variable in script

Post by Limey »

Hi Folks,
I currently have a script that looks to the last 3 characters of a string for a Print Size. Based on what it finds, it will run 1 of 3 actions. Right now our production staff must look up what Print Size needs to be entered into the string based upon the style of shirt and it's size. They then have to manually drop each file on 1 of 3 droplets to size the image for printing.

What our web programmers currently deliver to us is a string as follows: 3079904-QT0009-4M124-lrg that we use to rename an image with a GUID.
PO#: 3079904-
Pattern ID: QT0009-
SKU# 4M124-
Shirt Size: lrg <-----NOT Print Size
Each combination of style + size + color has a unique sku. We can therefore use the sku to determine the associated Print Size.

I have actions that use the Pattern ID to select a path from an archive to clip out the pattern. The thing I do not have is the Print Size included in the String from the programmers. This would need a separate database to be made for Skus/Print Size and a way to add and update the data. This is not something that the programmers are able to work on right now so I am hoping that Photoshop will be able to handle it.

My idea is to create a csv file to act as my "database" with the following columns.

A: Style
B: Size
C: Color
D: Sku
E: Print Size

We don't need to be concerned about Columns A, B and C in the Photoshop Script but we would want the script to look at column D and then pass the contents of E to a variable. That variable would then control which action to run using a simple "case" statement. We would need to use the SKU in the file name to look up the Print size in the csv file. Essentially it's like a VLOOKUP in Excel.

The Variable currently would be var printSize. Here is the code that I am using now to pull the size from the file name (string):
Code: Select allvar doc = app.activeDocument.name;
//alert(doc);
var part = doc.slice(0,-4);
var printSize = part.split('-')

switch(printSize[3]){
   //case 1 :  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); break;
   case "lrg":  doAction("Qtee-Print lrg","Qtee print actions"); break;
   case "med": doAction("Qtee-Print med","Qtee print actions"); break;
   case "sml": doAction("Qtee-Print sml","Qtee print actions"); break;
   default : break;
}

If possible, I would like a set name and path to a csv file on our network that can be updated by the buyers and designers when new products are brought online. (e.g. /Volumes/Production/Digital Imaging/Public/database.csv). If the server is an issue, we can place the file on our desktop.

Basic Tee XS H1250 MED
Basic Tee, S H1251 LRG
Basic Tee M H1252 LRG
Basic Tee L H1253 LRG
Hi-Lo LS Banded Tee XS H1254 LRG
Hi-Lo LS Banded Tee S H1255 LRG
Hi-Lo LS Banded Tee M H1256 LRG
Hi-Lo LS Banded Tee L H1257 LRG


Any help would be most gratefully accepted,

Thanks,
Paul (Limey)
Limey

Lookup Value from CSV to become variable in script

Post by Limey »

OK, so I take it that was way too much info so I'll try to break it down to the core of the question...

Can someone read a variable in a script, e.g. var sku = V1234 and then search for it in a column of a CSV file. If it finds a match, report back to the script the value in the column beside it in the CSV file. The result would become a value of a variable in the script e.g. var printsize = lrg


I hope this helps to (dare I say it...) simplify my question.

Gratefully,

Paul (Limey)
xbytor

Lookup Value from CSV to become variable in script

Post by xbytor »

Something like this should work.

Code: Select all//@includepath "/c/Program Files/Adobe/xtools;/Developer/xtools"
//@include "xlib/stdlib.js"

var sku = "V1234";
var printSize = undefined;

var skuIdx = 3;
var sizeIdx = 4;

var lines = Stdlib.readCSVFile("/Volumes/Production/Digital Imaging/Public/database.csv");

for (var i = 0; i < lines.length; i++) {
    var line = lines;
    if (line[skuIdx] == sku) {
       printSize = line[sizeIdx];
       break;
    }
}

$.writeln(printSize);
Limey

Lookup Value from CSV to become variable in script

Post by Limey »

Thanks Xbytor, I'm going to try to check it out before the alarm goes off here at work... If I don't get it done I will let you know tomorrow how it goes.
A huge thanks for coming through as always! I owe you guys on PS-Scripts so much...

Paul
csuebele

Lookup Value from CSV to become variable in script

Post by csuebele »

You could also use an xml file to store and retrieve the information. I've been using them a lot more in my scripts for storing and retrieving variables and creating presets for my scripts.
Limey

Lookup Value from CSV to become variable in script

Post by Limey »

Hi csuebele,
The reason I want to try to use a csv file is because it will need to be updated periodically by non techy people (designers or sales staff) and they can simply open the cdv file in Excel, edit and re-save it. I have to keep the process as simple as possible.

Thanks for the feedback and I will look into the XML more.

Paul
Limey

Lookup Value from CSV to become variable in script

Post by Limey »

Thanks so much Xbytor! By putting your script together with another script I was helped with in PS-Scripts, I was able to read my file name, extract a part or it (the SKU#),locate and search the CSV file for the SKU#, read from the array (CSV columns) the desired data and control which actions run based on the info.

Wow! It's going to help like you can't believe... On my way to full automation

Thank you for the latest version of XTOOLS http://sourceforge.net/projects/ps-scri ... ools/v2.1/. Once I installed the newest version of XTOOLS, the script worked great.

Paul (Limey)


Here is the final code:
Code: Select all//@includepath "/Applications/Adobe Photoshop CC 2014/Presets/Scripts/xtools"
//@include "xlib/stdlib.js"

var doc = app.activeDocument.name;
//alert (doc);
var fileName = doc.slice(0,-4);
var printSize = fileName.split('-');
var part = printSize [2];
    var sku = part;
   // var printSize = part;
    //alert(printSize);
    var skuIdx = 3;
    var sizeIdx = 4;

    var lines = Stdlib.readCSVFile("/Volumes/Production/Digital Imaging/Public/database.csv");


for (var i = 0; i < lines.length; i++) {
        var line = lines;
        if (line[skuIdx] == part) {
           printSize = line[sizeIdx];
           
           break;
        }
    }

    $.writeln(printSize);

switch(printSize){
   //case 1 :  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); break;
   case "LRG":  doAction("Converce-lrg","QTEE-Shoes"); break;
   case "SML": doAction("Converce-sml","QTEE-Shoes"); break;

   default : break;
}