Copy link to clipboard
Copied
I need a Script to change swatch names without Error Message when it doesn't find a swatch.
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 undefin
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
It was useful to fix my script.
Thanks!