How to script if / then
Copy link to clipboard
Copied
I'm struggling to come up with the correct syntax for doing something fairly simple. Basically I want to find a named spot color swatch. If it's found I have other programming I'd like to run (I have working code) If it does not find the swatch, then just carry on.
if swatch "Blue" exists (a spot color)
do this
else
do nothing and carry on with rest of script
endif
Thanks for any help.
Explore related tutorials & articles
Copy link to clipboard
Copied
if then would not work here because if the swatch does not exist, it will raise an error. So we need to use try-catch to direct the script to do something else when the swatch is not found
var idoc = app.activeDocument;
var swatches = idoc.swatches;
try {
var mySwatch = swatches.getByName("CMYK Yellow");
alert ("swatch found");
}
catch (e) {
alert ("swatch not found")
}
Copy link to clipboard
Copied
Thank you very much. That work as is. However when I add other code to the try section it give me "Error 16: catch/finally without try". Basically looking to see if there is a Dimension swatch. If so select everything with that stroke color and move it to the Dimensions layer.
var idoc = app.activeDocument;
var swatches = idoc.swatches;
try {
var mySwatch = swatches.getByName("Dimension");
// alert ("swatch found");
var mydoc = app.activeDocument;
mydoc.selection = null;
mydoc.defaultStrokeColor = mydoc.swatches["Dimension"].color;
app.executeMenuCommand("Find Stroke Color menu item");
var doc = app.activeDocument;
var layerName = 'Dimensions';
var _layer = doc.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();
}
catch (e) {
alert ("swatch not found")
}
Copy link to clipboard
Copied
your for loop is missing a closing bracket

