• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Illustrator batch script only runs once and stops

New Here ,
Mar 05, 2022 Mar 05, 2022

Copy link to clipboard

Copied

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' );
	}
}

 

TOPICS
Scripting

Views

183

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guide ,
Mar 06, 2022 Mar 06, 2022

Copy link to clipboard

Copied

You have two "for" loops, the second nested inside the first, both using the same counter (i).  Change one of the counters, e.g. to "j", in both the loop expressions and the body of the loop.  Then take it from there. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2022 Mar 06, 2022

Copy link to clipboard

Copied

LATEST

Awesome! Works great now.  Thanks for taking time to help. Here's the working script if anybody is needing to do something similar.

// 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 j = _selectedItems.length - 1; j>=0; j--) {
                _selectedItems[j].move(_layer, ElementPlacement.PLACEATEND)
             ;
                _selectedItems[j].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' );
	}
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines