Copy link to clipboard
Copied
Every day i have to do this steps many times:
1. Window -> Swatches -> Other Libraries -> "Select MySwatches.ai from Computer".
2. Then i click on swatches to apply them into swatch list
Can u tell me please, how to apply swatches into document via script, if it possible?
Copy link to clipboard
Copied
Yea it's possible, a script can open your swatch file and copy all the swatches over. However, if your issue is putting them into a new document, you could alter one of the new document profiles to include the swatches so that your new default documents will already have them.
Copy link to clipboard
Copied
Hello, dear Silly-V!
Unfortunately, all the documents I work with are not new. Please, tell me how to do this for already created documents.
Copy link to clipboard
Copied
I found how to open swatches, but can't find how to copy them. Please take a look.
#target Illustrator
// insert your path here, or a file picker prompt
var theFile = new File('path/to/file/file_name.ai');
var openOpt = new OpenOptions();
// get swatches
openOpt.openAs=LibraryType.SWATCHES;
open(theFile,null,openOpt);
Copy link to clipboard
Copied
They will not do what you want when you're opening as library. What you want to do is open the document and duplicate the swatches from one document to another.
Copy link to clipboard
Copied
Here is an example of basic process swatches, assuming you have two documents open, both having the same color space.
#target illustrator
function test(){
var doc = app.activeDocument, doc2 = app.documents[1];
alert(doc.name + " , " + doc2.name);
var thisSw, newSw;
for(var i=0; i<doc2.swatches.length; i++){
thisSw = doc2.swatches;
if(thisSw.name == "[Registration]" || thisSw.name == "[None]"){
continue;
}
newSw = doc.swatches.add();
newSw.name = thisSw.name;
newSw.color = thisSw.color;
}
};
test();
Copy link to clipboard
Copied
Thanks, but it's actually not that i want to.
I need copy swatches not from another document, i need to copy them from library into document swatches.
Copy link to clipboard
Copied
Your library is just another .ai document, so are all the other libraries such as graphic styles or symbols. You'd have to open such a file with your script and copy the items over.
Copy link to clipboard
Copied
Silly-V, can u please correct code to open, work with and close document with swatches file by name, not by documents index?
Copy link to clipboard
Copied
Now i understand, thanks a lot Silly-V!
I am not strong in coding, by this one is works right.
#target Illustrator
// insert your path here, or a file picker prompt
var theFile = new File('C:/mySwatch.ai');
// var openOpt = new OpenOptions();
// get swatches
// openOpt.openAs=LibraryType.SWATCHES;
open(theFile,null/*,*openOpt*/);
var doc2 = app.activeDocument, doc = app.documents[1];
var thisSw, newSw;
for(var i=0; i<doc2.swatches.length; i++){
thisSw = doc2.swatches;
if(thisSw.name == "[Registration]" || thisSw.name == "[None]"){
continue;
}
newSw = doc.swatches.add();
newSw.name = thisSw.name;
newSw.color = thisSw.color;
};
doc2.close(SaveOptions.DONOTSAVECHANGES);