Skip to main content
Participating Frequently
Jan 10, 2025 9:30 PM UTC
Question

How to script if / then

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.

 

 

 

 

3 replies

CarlosCanto
Community Expert
Community Expert
Jan 10, 2025 9:52 PM UTC

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")
}
Translate
Participating Frequently
Jan 11, 2025 4:47 PM UTC

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

 

Translate
CarlosCanto
Community Expert
Community Expert
Jan 11, 2025 9:10 PM UTC

your for loop is missing a closing bracket

Translate