Illustrator batch script only runs once and stops
Hi,
I am trying to get a batch script to process all the AI files in a selected folder but I can only get this script to process the first file in the folder. I'm starting to learn Javascript but up to this point I only have the skills to find existing scripts online and combine them to get them to do what I need (you may recognize parts of the following code as coming from other peoples scripts). Essentially, the following is what I'm trying to do:
- Open the first AI file in a designated folder
- Select the pathItems that have a RGB Green fill color
- Move those pathItems to a folder named "Layer NEW"
- Hide the "Layer New" layer
- Save the AI file
- Close the AI file
- Repeat until all the files in the folder are processed.
Any help would be appreciated.
// It promts the user for the source folder and destination folder
// Opens an AI file, Selects all the RGB Green paths
// Moves the selected paths to the folder named 'Layer NEW'
// Hides 'Layer NEW'
// Saves the AI file and closes the file
// Looks to see if there is another AI file to process
var destFolder, sourceFolder, files, sourceDoc;
// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with the AI files that you want to remove the green on', '~' );
// If a valid folder is selected
if ( sourceFolder != null )
{
files = new Array();
// Get all files matching the pattern
files = sourceFolder.getFiles("*.ai");
if ( files.length > 0 )
{
// Get the destination to save the files
destFolder = Folder.selectDialog( 'Select the folder where you want to save the finished AI files.', '~' );
for ( i = 0; i < files.length; i++ )
{
sourceDoc = app.open(files[i]); // returns the document object
// Select path item based on RGB swatch
// This script does the same operation as "Select Same Fill Color"
// does in Illustrator. Commented code as well for your help. It works
// perfectly in Illustrator CC 2017
var rgbCol = new RGBColor();
rgbCol.red = 0;
rgbCol.green = 255;
rgbCol.blue = 0;
sourceDoc.defaultFillColor = rgbCol;
app.executeMenuCommand("Find Fill Color menu item");
// This portion moves the selected items to a layer called 'Layer NEW'
var layerName = 'Layer NEW';
var _layer = sourceDoc.layers.getByName(layerName);
var _selectedItems = app.selection;
for (var i = _selectedItems.length - 1; i>=0; i--) {
_selectedItems[i].move(_layer, ElementPlacement.PLACEATEND)
;
_selectedItems[i].selected = false;
}
app.redraw();
// LayerHideByName.jsx
var myLayers = sourceDoc.layers;
var HideName = "Layer NEW";
try {
HideLayer = myLayers.getByName (HideName);
HideLayer.visible = false;
redraw();
}
catch (e) {}
// Save the AI file and close
sourceDoc.save();
sourceDoc.close();
}
alert( 'Files are saved as AI in ' + destFolder );
}
else
{
alert( 'No matching files found' );
}
}
