2

I'm very new to illustrator and I have not done scripting before, I have like a hundred illustrator files that I need to change the background color lets say background color is layer 5 for every file, can scripting make it like repeat changing the background color for all my files, i would really appreciate some help with how to start writing a script.

Example below:

script below:

    var doc = app.activeDocument;
    var layers = doc.layers;
    var colortest = new RGBColor();
    colortest.red = 251;
    colortest.green = 237;
    colortest.blue = 31;
    layers[4].color = colortest;

i havent done any scripting before, and ive just been researching for a while and its really confusing me. i tried the script above based on my researches but its wrong.instead of changing the blue color of the layer it changes that yellow color thing

Script result:

1
  • Have you tried scripting before? Please make an edit with some code.
    – user9447
    Dec 19, 2016 at 14:28

2 Answers 2

2

The issue is that you're targeting the wrong scripting object to command. The layer.color is simply the layer's color-marker inside the layers panel. What you want to do is actually change the artwork inside the actual layer. Check out the edited script here- it uses the collection of pathItems inside a layer to target the first path. This will only work if your blue shape is a rectangle inside the layer - and is not part of any group but is a top-level piece. (Open up your individual layers inside the layers panel to observe the artwork grouping structure) It will also not work if your blue shape is a raster image, etc.

#target illustrator function test(){ var doc = app.activeDocument; var layers = doc.layers; var colortest = new RGBColor(); colortest.red = 251; colortest.green = 237; colortest.blue = 31; layers[4].pathItems[0].fillColor = colortest; }; test();

1
  • Thank you for your time explaining this stuff, i really appreciate it because i dont really know those terms Dec 21, 2016 at 7:30
0

Actually guys ive got an answer from another forum and it works great

var main = function() {  
var doc,   
fo = Folder.selectDialog("Please select some folder with ai files"),  
aiFilter  = function(f){return /\.ai$/i.test(decodeURI(f.name))},  
aiFiles, n, aiFile;  

if ( !fo ) return;  

aiFiles = fo.getFiles ( aiFilter );  
if ( !aiFiles.length ) return;  
while ( aiFile = aiFiles.pop() ){  
processAiFile ( aiFile );  
}  


}  


function processAiFile ( aiFile ) {  
var doc = app.open ( aiFile ),  
aiLayer = getLayerByName (doc, "Layer 5"),  
background;  

if ( !aiLayer ) {  
log ( "Layer \"Layer 5\" couldn't be found in  "+doc.name );  
doc.close( SaveOptions.DONOTSAVECHANGES );  
return;  
}  

if ( !aiLayer.pathItems.length ) {  
log ( "Layer \"Layer 5\" doesn't contain any path item" );  
doc.close( SaveOptions.DONOTSAVECHANGES );  
return;  
}  


background = aiLayer.pathItems[0];  

 background.filled = true;  
background.fillColor = getColor({cyan:0, magenta:100, yellow:100, black:0} );  

doc.close ( SaveOptions.SAVECHANGES );  
log ( aiFile.fsName+ " processed successfully" );  
}  


 function getColor(obj) {  
  newCMYKColor = new CMYKColor();  
  newCMYKColor.black = obj.black;  
  newCMYKColor.cyan = obj.cyan;  
  newCMYKColor.magenta = obj.magenta;  
  newCMYKColor.yellow = obj.yellow;  
  return newCMYKColor;  
 }  


function getLayerByName ( doc, layerName) {  
var found = false,  
layers = doc.layers,  
n = layers.length;  

while ( n-- ) {  
if ( layers[n].name == layerName ) {  
return layers[n];  
  }  
 }  


return null;  
}  


var log = function(msg) {  
var r = new File ( Folder.desktop+"/log.txt" );  
r.open('a');  
r.writeln ( msg );  
r.close();  
}  
main();  

credits to Loic

http://www.ozalto.com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.