This website contains various projects I have worked on. Currently, this includes JavaScripts for Adobe applications and general produciton tutorials using Adobe Creative Suite applications. All projects can be accessed from the right-hand menu.

image of a notepad
Updating swatches in InDesign CS3

Script Description

I got the idea for this script from David Blatner of InDesignSecrets.com. The function of the script is to replace all swatches with swatches of the same name that have the “copy” suffix. This could be useful in the following instance: import an ASE file with a bunch of named swatches. Then import a different bunch with same names, but different definitions. ID apparently doesn’t allow you to choose which definitions you want, so the new swatches all come in as “copy.” In this case, we want the new definitions... so you’d run the script and it would get rid of the original and use the duplicate instead.

Looking at the Script

The script starts out by counting the number of swatches used in your document. For all the swatches in your document the script takes a look at the last four characters to see if they spell “copy”. If the swatch is a copy swatch, the script will try to replace the original swatch with the copy swatch.

mySwatchErrors is an array that is created to store any errors encountered while trying to switch swatches. The “Black” swatch cannot be deleted so that always results in a error. Also, if there is no original swatch for the copy swatch that will produce an error. At the end of the script, an alert window is shown showing the swatches that produced errors.

//swatch_switch.jsx
/* This InDesign CS3 JavaScript will search through all swatches in a document and replace
	original swatches with the "copy" swatch it finds. A copy swatch is defined as a
	swatch that ends with the suffix "copy".  The script will report any swatches that could not
	be switched. The default "[Black]" swatch cannot be removed/switched so it will always appear
	in the report.
	*/

var myDocument = app.activeDocument;
var myTotalSwatches = myDocument.swatches.count();
var mySwatchErrors = new Array(); //array of swatches that could not be removed/replaced/switched

for (i = 0; i < myTotalSwatches; i++)
{
	
	myTempString = myDocument.swatches.item(i).name;	
	myTempStringSlice = myTempString.slice((myTempString.length-4), myTempString.length);
	
	try // try to replace the old swatch with the new "copy" one
		{
			if (myTempStringSlice == 'copy') // If the swatch is a "copy" swatch
			{
				//Get the names of the old swatch and the one we'll replace it with
				myNewSwatchName = myTempString;
				myOldSwatchName = myTempString.slice(0, myTempString.length-5);			
					
				//Remove the old swatch	
				myOldSwatch = myDocument.swatches.itemByName(myOldSwatchName);	
				myOldSwatch.remove(myNewSwatchName);	
				
				//Rename the new swatch with the name of the old swatch
				myNewSwatch = myDocument.swatches.itemByName(myNewSwatchName);
				myNewSwatch.name = myOldSwatchName;
				
				//Reset the loop if you find a 'copy' swatch and start looking again	
				myTotalSwatches = myDocument.swatches.count();
				i = 0; 		
				}		
			}
		catch(e)
			{
				mySwatchErrors[i] = myOldSwatchName;
				}
	}

//Inform user which swatches could not be switched
var myErrorReport = mySwatchErrors.join(" "); 
alert(myErrorReport, "The following swatches could not be switched");
      

back to top

InDesign scripting image

Table-of-Contents

Downloads


Other Projects:

Adobe JavaScripts ↑↓

Adobe Creative Suite Tutorials ↑↓