Skip to main content
Participating Frequently
January 10, 2025
Question

How to script if / then

  • January 10, 2025
  • 1 reply
  • 317 views

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.

 

 

 

 

1 reply

CarlosCanto
Community Expert
Community Expert
January 10, 2025

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")
}
Participating Frequently
January 11, 2025

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")
}

 

CarlosCanto
Community Expert
Community Expert
January 11, 2025

your for loop is missing a closing bracket