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

Change Swatch Names Script

Explorer ,
Jul 06, 2020 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.

TOPICS
Scripting

Views

386

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

correct answers 1 Correct answer

Community Expert , Jul 06, 2020 Jul 06, 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 undefin

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jul 06, 2020 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

Votes

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
community guidelines
Explorer ,
Jul 07, 2020 Jul 07, 2020

Copy link to clipboard

Copied

LATEST

It was useful to fix my script.

Thanks!

Votes

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