Highlighted

Change Swatch Names Script

Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

3E23CE1E-3DE8-4DE3-950F-FF2DBC10FC65.jpeg

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 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

TOPICS
How to, Scripting

Views

85

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Change Swatch Names Script

Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

3E23CE1E-3DE8-4DE3-950F-FF2DBC10FC65.jpeg

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 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

TOPICS
How to, Scripting

Views

86

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 06, 2020 0
Contributor ,
Jul 06, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 06, 2020 3
Community Beginner ,
Jul 07, 2020

Copy link to clipboard

Copied

It was useful to fix my script.

Thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 07, 2020 1
Resources