Skip to main content
July 7, 2020
Answered

Change Swatch Names Script

  • July 7, 2020
  • 1 reply
  • 626 views

I need a Script to change swatch names without Error Message when it doesn't find a swatch.

This topic has been closed for replies.
Correct answer m1b

I think "try" and "catch" might be what you need. Here's an example, not changing names, but you get the idea.

 

 

 

var swatch;
try {
    swatch = app.activeDocument.swatches.getByName("mySwatch");
} catch (error) {
    // do something? or keep quiet?
}
if ( swatch != undefined) {
    // do somethng with the swatch!
}

 

 

 

Edit: I seem to recall there might be times where Try/Catch doesn't stop Illustrator complaining. In those cases, take the time to check variables before using, eg. are they undefined, or null, to avoid the error altogether. This is probably a good practice anyway. So if you are changing names, check to see if the name is unique (or whatever your error is) before doing it.

 

 Regards,

Mark

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 7, 2020

I think "try" and "catch" might be what you need. Here's an example, not changing names, but you get the idea.

 

 

 

var swatch;
try {
    swatch = app.activeDocument.swatches.getByName("mySwatch");
} catch (error) {
    // do something? or keep quiet?
}
if ( swatch != undefined) {
    // do somethng with the swatch!
}

 

 

 

Edit: I seem to recall there might be times where Try/Catch doesn't stop Illustrator complaining. In those cases, take the time to check variables before using, eg. are they undefined, or null, to avoid the error altogether. This is probably a good practice anyway. So if you are changing names, check to see if the name is unique (or whatever your error is) before doing it.

 

 Regards,

Mark

July 8, 2020

It was useful to fix my script.

Thanks!