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

How to script if / then

Community Beginner ,
Jan 10, 2025 Jan 10, 2025

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.

 

 

 

 

TOPICS
Scripting
152
Translate
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
Community Expert ,
Jan 10, 2025 Jan 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")
}
Translate
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
Community Beginner ,
Jan 11, 2025 Jan 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")
}

 

Translate
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
Community Expert ,
Jan 11, 2025 Jan 11, 2025
LATEST

your for loop is missing a closing bracket

Translate
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