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

Assigning random colors from a defined color palette.

Community Beginner ,
Mar 12, 2013 Mar 12, 2013

I'm using a mac running illustrator cs5 and have about 4 thousand eps files (all vector paths filled with black) that need to be assigned colors randomly from a defined color palette (25 custom swatches). The first 2 swatches should be omitted- they aren't relevant to this task.

The script should open the file select all vector elements and assign a random color form the defined custom swatch palette then save the file and move on to the next.

Any help will be greatly appreciated.

TOPICS
Scripting
4.8K
Translate
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 1 Correct answer

Guru , Mar 12, 2013 Mar 12, 2013

OK… I only made a half dozen file and colors… but it should be close or there abouts if you have done the above…?

#target illustrator

main();

function main() {

          var csv, eps, cols, files;

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          csv = File.openDialog( 'Please choose your CSV text file?' );

          eps = Folder.selectDialog( 'Please choose your folder of EPS files?' );

          if ( csv != null && eps != null ) {

                    cols = readInCS

...
Translate
Adobe
Community Expert ,
Mar 12, 2013 Mar 12, 2013

What they have already tried? Where there are problems?

Translate
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 ,
Mar 12, 2013 Mar 12, 2013

I'm very new to this- I've tried running actions but it's imposible to select a random color- scripting I haven't tried because I don't know exactly where to begin.

Translate
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
Guru ,
Mar 12, 2013 Mar 12, 2013

Yeah a batch script should be able to do this without much trouble… Do all the *.eps files already have the color swatches in them…? This is usually the biggest issue…

Translate
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 ,
Mar 12, 2013 Mar 12, 2013

I really hope that it is possible, unfortunately the eps files don't have the custom palette available, you have to asign it each time a new file is open, which is a problem. I've already added a custom palette option to my presets so I can select it when necessary.

Translate
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
Guru ,
Mar 12, 2013 Mar 12, 2013

Script does NOT have all the same access to elements of the app that the GUI has… If you have saved a swatch library and set to persistent that can't be accessed… It may be the case that the array of colors could be coded into the script or it could read the values in from some external text file… What are you working with RGB, CMYK or custom spots…? Are all the *.eps files in question in the same location…? Do they need overwriting or saving elsewhere…?

Translate
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 ,
Mar 12, 2013 Mar 12, 2013

the eps files are located in the same folder, they would most likely be overwritten. The eps files are in cmyk format and the colors are as well- they all have cmyk values.

Translate
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
Guru ,
Mar 12, 2013 Mar 12, 2013

Put whatever color values you want to use for this in Excel, FMP or as lines of plain text… as CSV… ie:

100, 80, 0, 10

65, 100, 50, 0

and so on…

Should be able to come up with something…

Translate
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
Guru ,
Mar 12, 2013 Mar 12, 2013

OK… I only made a half dozen file and colors… but it should be close or there abouts if you have done the above…?

#target illustrator

main();

function main() {

          var csv, eps, cols, files;

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          csv = File.openDialog( 'Please choose your CSV text file?' );

          eps = Folder.selectDialog( 'Please choose your folder of EPS files?' );

          if ( csv != null && eps != null ) {

                    cols = readInCSV( csv );

                    files = eps.getFiles( /\.eps$/i );

                    proccessDocs( files, cols );

          };

          app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

};

//

function proccessDocs( files, cols ) {

          var i, count, rand;

          count = files.length;

          for ( i = 0; i < count; i++ ) {

                    rand = Math.floor( Math.random() * cols.length ); // Is this right hum now Im not sure…?

                    //$.writeln( rand );

                    colorDoc( files, cols[rand] );

          };

};

//

function colorDoc( file, rand ) {

          var i, doc, count, col;

          doc = app.open( file );

          col = cmykColor( rand[0], rand[1], rand[2], rand[3] );

          count = doc.pathItems.length;

          for ( i = 0; i < count; i++ ) {

                    doc.pathItems.fillColor = col;

          };

          app.redraw();

          doc.close( SaveOptions.SAVECHANGES );

};

//

function cmykColor( c, m, y, k ) {

          var colour = new CMYKColor();

          colour.cyan = c, colour.magenta = m;

          colour.yellow = y, colour.black = k;

          return colour;

};

//

function readInCSV( fileObj ) {

          var line, fileArray, csvArray;

          fileArray = new Array();

          fileObj.open( 'r' );

          while( !fileObj.eof ) {

                    line = fileObj.readln();

                    csvArray = line.split( ',' );

                    fileArray.push( csvArray );

          };

          fileObj.close();

          return fileArray;

};

Translate
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 ,
Mar 12, 2013 Mar 12, 2013

Wow! This is amazing! for some strange reason it states result undefined? it selects the cvs file and eps folder- Not sure what I'm doing wrong. Thank you so much for the quick responce great work man!

Translate
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
Guru ,
Mar 13, 2013 Mar 13, 2013

Oh forgot to ask… What OS are you on… A known issue with Lion that I forgot to include the fix in this…

Translate
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 ,
Mar 13, 2013 Mar 13, 2013

I'm using os 10.8.2

Translate
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 ,
Mar 13, 2013 Mar 13, 2013

Thank you Muppet Mark for all of your work! I really appreciate your willingness to help.

Translate
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
Guru ,
Mar 13, 2013 Mar 13, 2013

I can't remember if it's just the file objects from the folder get files method or all file objects… Give this a test… If it don't work then I may need to fix the file open too…?

#target illustrator

main();

function main() {

          var csv, eps, cols, files;

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          csv = File.openDialog( 'Please choose your CSV text file?' );

          eps = Folder.selectDialog( 'Please choose your folder of EPS files?' );

          if ( csv != null && eps != null ) {

                    cols = readInCSV( csv );

                    files = eps.getFiles( /\.eps$/i );

                    proccessDocs( files, cols );

          };

          app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

};

//

function proccessDocs( files, cols ) {

          var i, count, rand;

          count = files.length;

          for ( i = 0; i < count; i++ ) {

 

                    rand = Math.floor( Math.random() * cols.length ); // Is this right…?

 

                    //$.writeln( rand );

                    colorDoc( files, cols[rand] );

          };

};

//

function colorDoc( file, rand ) {

          var i, doc, count, col;

          //doc = app.open( file ); Pre Lion

 

          doc = app.open( File( file.fsName.replace( 'file://', '' ) ) );

          col = cmykColor( rand[0], rand[1], rand[2], rand[3] );

          count = doc.pathItems.length;

          for ( i = 0; i < count; i++ ) {

                    doc.pathItems.fillColor = col;

          };

          app.redraw();

          doc.close( SaveOptions.SAVECHANGES );

};

//

function cmykColor( c, m, y, k ) {

          var colour = new CMYKColor();

          colour.cyan = c, colour.magenta = m;

          colour.yellow = y, colour.black = k;

          return colour;

};

//

function readInCSV( fileObj ) {

          var line, fileArray, csvArray;

          fileArray = new Array();

          fileObj.open( 'r' );

          while( !fileObj.eof ) {

                    line = fileObj.readln();

                    csvArray = line.split( ',' );

                    fileArray.push( csvArray );

          };

          fileObj.close();

          return fileArray;

};

Translate
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 ,
Mar 13, 2013 Mar 13, 2013

I tried running it but it dosen't return a result in 10.8.2, but I tested the previous script in 10.6.8 and it worked prefectly!! Good job!! I wonder why 10.8.2 is being so difficult.

Translate
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
Guru ,
Mar 13, 2013 Mar 13, 2013

Like I said Im a little behined… may me its all file objestcs in which case change line 18 from …

cols = readInCSV( csv );

to



cols = readInCSV( File( csv.fsName.replace( 'file://', '' ) ) );
Translate
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 ,
Mar 25, 2013 Mar 25, 2013

Hi Muppet Mark

I've tried running the script CS5 OS 10.6.8 but after running it on about 50 files I get the error mesage below, any idea why?

I need to execute this task on roughly 7000 files.

Screen shot 2013-03-25 at 10.20.43 AM.png

Translate
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
Guru ,
Mar 25, 2013 Mar 25, 2013

Like it says it's expecting a number… There is no error trapping in the script… Have you checked the CSV file is all numbers only and no columns are empty…?

Translate
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 ,
Mar 26, 2013 Mar 26, 2013

Hello again Muppet Mark,

First of all I want to thank you for all of your help, you made a huge difference in my workflow, this script works amazingly well!

I have one alteration- what if I wanted the script to select only raster images or placed items- ignoring all paths items. Here is what I came up with based on your script. Actually I changed doc.pathItems to doc.placedItems, but of course having no experience in javascript it fails to work. Can you please tell me what I'm doing wrong?

#target illustrator

main();

function main() {

          var csv, eps, cols, files;

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          csv = File.openDialog( 'Please choose your CSV text file?' );

          eps = Folder.selectDialog( 'Please choose your folder of EPS files?' );

          if ( csv != null && eps != null ) {

                    cols = readInCSV( csv );

                    files = eps.getFiles( /\.eps$/i );

                    proccessDocs( files, cols );

          };

          app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

};

//

function proccessDocs( files, cols ) {

          var i, count, rand;

          count = files.length;

          for ( i = 0; i < count; i++ ) {

                    rand = Math.floor( Math.random() * cols.length ); // Is this right hum now Im not sure…?

                    //$.writeln( rand );

                    colorDoc( files, cols[rand] );

          };

};

//

function colorDoc( file, rand ) {

          var i, doc, count, col;

          doc = app.open( file );

          col = cmykColor( rand[0], rand[1], rand[2], rand[3] );

          count = doc.placedItems.length;

          for ( i = 0; i < count; i++ ) {

                    doc.placedItems.fillColor = col;

          };

          app.redraw();

          doc.close( SaveOptions.SAVECHANGES );

};

//

function cmykColor( c, m, y, k ) {

          var colour = new CMYKColor();

          colour.cyan = c, colour.magenta = m;

          colour.yellow = y, colour.black = k;

          return colour;

};

//

function readInCSV( fileObj ) {

          var line, fileArray, csvArray;

          fileArray = new Array();

          fileObj.open( 'r' );

          while( !fileObj.eof ) {

                    line = fileObj.readln();

                    csvArray = line.split( ',' );

                    fileArray.push( csvArray );

          };

          fileObj.close();

          return fileArray;

};

Translate
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
Guru ,
Mar 26, 2013 Mar 26, 2013

Not all placed items… can be colored… Only placed raster items can be colored ( and I don't think using spots works either )…

You don't set a property to do this but use a method instead…

Try looping the raster items and using…

rasterItems.colorize( col );

Translate
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 ,
Mar 26, 2013 Mar 26, 2013

yep you are right- luckly I'm working with all raster art. Good news it worked!!!

Thanks again!!

Translate
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 ,
Mar 27, 2013 Mar 27, 2013
LATEST

One more alteration- if I wanted to add a pathItem in the form of a rectangle that fits to the perimeter of the artboard as a final step in this script sequence, how would I go about doing so? The artboard pathItem can remain blank. Below is a script that I borrowed from fellow contributor moluapple.

It does exactly what I want, however I have no idea where or how to insert the addition to your existing code. I have been guessing all night and all my attempts have failed. Can you please help…I am forever grateful for your time and generosity, Thank you again.

var docRef = app.activeDocument;

var artboardRef = docRef.artboards;

for(i=0;i<artboardRef.length;i++){

     var top=artboardRef.artboardRect[1] ;

     var left=artboardRef.artboardRect[0];

     var width=artboardRef.artboardRect[2]-artboardRef.artboardRect[0];

     var height=artboardRef.artboardRect[1]-artboardRef.artboardRect[3];

     var rect = docRef.pathItems.rectangle (top, left, width, height);

     rect.fillColor = rect.strokeColor = new NoColor();

     };

Translate
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 ,
Mar 25, 2013 Mar 25, 2013

I checked my CSV file it, I copied and pasted it in a new document and it appears to be working! Without interruption. Sorry for the confusion thanks again for the help.

Translate
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