Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
What they have already tried? Where there are problems?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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…
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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…?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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…
Copy link to clipboard
Copied
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;
};
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Oh forgot to ask… What OS are you on… A known issue with Lion that I forgot to include the fix in this…
Copy link to clipboard
Copied
I'm using os 10.8.2
Copy link to clipboard
Copied
Thank you Muppet Mark for all of your work! I really appreciate your willingness to help.
Copy link to clipboard
Copied
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;
};
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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://', '' ) ) ); |
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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…?
Copy link to clipboard
Copied
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;
};
Copy link to clipboard
Copied
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 );
Copy link to clipboard
Copied
yep you are right- luckly I'm working with all raster art. Good news it worked!!!
Thanks again!!
Copy link to clipboard
Copied
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();
};
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now