1 Reply Latest reply on Apr 8, 2018 1:13 AM by SuperMerlin

    Help .... newb question. CSV file and Hex fill script

    jaymzveehee

      Hey guys,

       

      i have a daunting task infront front of me and I am thinking a smarter way would be a script. Problem I don’t code.

       

      I need to create a whack of hex swatch png files (2000px by 2000px - 72dpi) for a project. I have a bunch of upc codes, and their hex values separated in a csv table and I need to use the hex value to fill the square and save the file using both fields as part of the filename.

       

      so so I have a the 12 digit upc say 057896543001 and the hex value for that of say 325647 ...... I would need a coloured square png with the fill color of 325647 and a file name of 057896543001_325647.png

       

      manually it is taking a minute or so for each file ..... any direction or help would be greatly appreciated In building a script to automate this process.

       

      cheers,

       

      james

        • 1. Re: Help .... newb question. CSV file and Hex fill script
          SuperMerlin Level 5

          You could try this, it needs a csv file in the format:-

          upc,hex
          057896543001,325647
           057856543001,ff0000 
          957896543001,ffff00
          
          

           

          #target photoshop;
          main();
          function main(){
          var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;"); 
          if(csvFile == null) return;
          var outputFolder = Folder.selectDialog( "Please select output folder");
          if(outputFolder == null ) return;
          csvFile.open("r");
          var data = csvFile.read().split("\n");
          csvFile.close();
          app.preferences.rulerUnits = Units.PIXELS;  
          app.preferences.typeUnits = TypeUnits.PIXELS;  
          var doc = app.documents.add(2000, 2000, 72, null, NewDocumentMode.RGB, DocumentFill.WHITE);
          for(var a in data){
          if(data[a].length < 12) continue; 
          var line = data[a].split(",");
          if(line[0].replace(/^\s+|\s+$/g,'').match(/^[a-zA-Z]/)) continue;
          var upc = line[0].replace(/^\s+|\s+$/g,'');
          var hex = line[1].replace(/^\s+|\s+$/g,'');
          var saveFile = File(outputFolder + "/" + upc + "_" + hex + ".png");
          var FillColour = new SolidColor;
          FillColour.rgb.hexValue = hex; 
          activeDocument.selection.selectAll();
          activeDocument.selection.fill(FillColour);
          activeDocument.selection.deselect();
          SavePNG(saveFile);
              }
          app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
          };
          function SavePNG(saveFile){
              pngSaveOptions = new PNGSaveOptions(); 
          activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE); 
          }