Copy link to clipboard
Copied
Hi,
I'm very new to JavaScript and I've combinded a few codes I've found on here to build a swatch color replacement tool. It works great! The only issue I'm having is that it only runs on one file in the selected folder. I would like it to run on all of the files in the folder. Am I missing a loop function? Any help appreciated very much. Thanks.
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;
// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator');
// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );
// Get all files matching the pattern
files = sourceFolder.getFiles( fileType );
if ( files.length > 0 )
{
// Get the destination to save the files
//destFolder = Folder.selectDialog( 'Select the folder where you want to save files.', '~' );
destFolder = sourceFolder;
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files); // returns the document object
app.paste()
app.cut()
var docRef = sourceDoc;
with (docRef) {
var replaceColor1= swatches.getByName('YELLOW Final').color;
for (var i = 0; i < pathItems.length; i++) {
with (pathItems) {
if (filled == true && fillColor instanceof SpotColor) {
if (fillColor.spot.name == 'Yellow TEMP') fillColor = replaceColor1;
}
if (stroked == true && strokeColor instanceof SpotColor) {
if (strokeColor.spot.name == 'Yellow TEMP') strokeColor = replaceColor1;
}
}
}
for (var j = 0; j < stories.length; j++) {
with (stories
for (var k = 0; k < characters.length; k++) {
with (characters
if (fillColor instanceof SpotColor) {
if (fillColor.spot.name == 'Yellow TEMP') fillColor = replaceColor1;
}
if (strokeColor instanceof SpotColor) {
if (strokeColor.spot.name == 'Yellow TEMP') strokeColor = replaceColor1;
}
}
}
}
}
}
sourceDoc.save();
sourceDoc.close();
}
}
}
else
{
alert( 'No matching files found' );
}
1 Correct answer
ZINGERERs wrote:
- I've combinded a few codes I've found on here
You should always reference such threads/posts/links, so we can see the original working code prior to cobbling it up with changes. I believe its from the following thread:
Muppet Mark Code: http://forums.adobe.com/message/2570586
ZINGERERs wrote:
The only issue I'm having is that it only runs on one file in the selected folder. I would like it to run on all of the files in the folder.
This works in my testing (I made some test files) an
...Explore related tutorials & articles
Copy link to clipboard
Copied
ZINGERERs wrote:
- I've combinded a few codes I've found on here
You should always reference such threads/posts/links, so we can see the original working code prior to cobbling it up with changes. I believe its from the following thread:
Muppet Mark Code: http://forums.adobe.com/message/2570586
ZINGERERs wrote:
The only issue I'm having is that it only runs on one file in the selected folder. I would like it to run on all of the files in the folder.
This works in my testing (I made some test files) and used Marks code in some batch code I use. Give it a try, works fine in my tests.
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var filesFolder = Folder.selectDialog('Select the folder where the (.ai) files are located.', '~');
if (filesFolder != null) {
var filesArray = new Array();
filesArray = filesFolder.getFiles("*.ai");
if (filesArray.length > 0) {
var saveToFolder = filesFolder;
var curDoc;
for (var f = 0, l = filesArray.length; f < l; f++) {
curDoc = app.open(filesArray
); // ------------------------------
// Muppet Mark Code:
// Thread: http://forums.adobe.com/message/2570586
with(curDoc) {
var replaceColor = swatches.getByName('YELLOW Final').color;
for (var i = 0; i < pathItems.length; i++) {
with(pathItems) {
if (filled == true && fillColor instanceof SpotColor) {
if (fillColor.spot.name == 'Yellow TEMP') fillColor = replaceColor;
}
if (stroked == true && strokeColor instanceof SpotColor) {
if (strokeColor.spot.name == 'Yellow TEMP') strokeColor = replaceColor;
}
}
}
for (var j = 0; j < stories.length; j++) {
with(stories
) { for (var k = 0; k < characters.length; k++) {
with(characters
.characterAttributes) { if (fillColor instanceof SpotColor) {
if (fillColor.spot.name == 'Yellow TEMP') fillColor = replaceColor;
}
if (strokeColor instanceof SpotColor) {
if (strokeColor.spot.name == 'Yellow TEMP') strokeColor = replaceColor;
}
}
}
}
}
}
// ------------------------------
curDoc.save();
curDoc.close();
curDoc = null;
}
} else {
alert('No Illustrator (.ai) files found');
}
}
Let us know if it works for you and if it was helpful.
Copy link to clipboard
Copied
Yes! That's the code I pulled from, noted about the reference. Thanks for linking and thanks for your help W_J_T! Much cleaner, and it works.
One of the hangups I'm having with this code is that the "Final" Colors have to live in the document for this script to work. It would be awesome if it could reference a swatch library for the final colors. Any ideas? I am in the process of adding a paste and cut to this to import the final colors from the clipboard as a workaround.
Copy link to clipboard
Copied
You're welcome, glad it worked for you.
ZINGERERs wrote:
One of the hangups I'm having with this code is that the "Final" Colors have to live in the document for this script to work. It would be awesome if it could reference a swatch library for the final colors. Any ideas? I am in the process of adding a paste and cut to this to import the final colors from the clipboard as a workaround.
Oh, I wondered why you had the app.paste() & app.cut() in the code you posted I found it strange otherwise.
You can create and add colors via scripting if that would help you? Here is an example and where it would need added into the code from above:
// for (var f = 0, l = filesArray.length; f < l; f++) {
// curDoc = app.open(filesArray
); // ------------------------------
// Add new spot color in each file
// (change CMYK values as desired)
var cmykColor = new CMYKColor();
cmykColor.cyan = 0;
cmykColor.magenta = 100;
cmykColor.yellow = 0;
cmykColor.black = 0;
var spotSwatch = curDoc.spots.add();
spotSwatch.color = cmykColor;
spotSwatch.colorType = ColorModel.SPOT;
spotSwatch.name = "YELLOW Final";
// ------------------------------
// Muppet Mark Code:
Does that help doing it that way?
Copy link to clipboard
Copied
Good to know, thanks! For this project, entering those values would actually create more work for me. I'm working with hundreds of colors so accessing a library would be golden.
Copy link to clipboard
Copied
Well, if the app.paste() & app.cut() works for you then run with it.
As far as accessing swatch libraries I would like to know about that as well. I tried some things but can't seem to get it to work, I'm not even sure its possible from what I have read and seen.
ZINGERERs wrote:
... entering those values would actually create more work for me. I'm working with hundreds of colors ...
Just curious however, if you have hundreds of colors to change between files, how is it you're going about that to minimize your effort? Currently the script is only set to change between 2 colors, how are you addressing hundreds?

