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

Delete unused swatches not working [JS]

Community Beginner ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

var myDocument = app.activeDocument;
function trashUnusedSwatch(){

while (myDocument.unusedSwatches[0].name != "") {

id = myDocument.unusedSwatches[0].id;

sw = myDocument.swatches.itemByID(id);

sw.remove();

}
}

 

What's wrong here?

 

TOPICS
How to , Scripting , SDK

Views

266

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 , Feb 24, 2021 Feb 24, 2021

Again, be clear about what's not working. Are you losing some swatches, none? Is there an error? I assume you're calling the trashUnusedSwatch() later on in your script?

 

I wouldn't rely on the unnamed swatches not being randomly at the 0 position, so this alternative may be better:

 

var mydoc = app.documents[0];
var mydeleteswatches = mydoc.unusedSwatches;
for (i = mydeleteswatches.length - 1;i >=0; i--) {
    var mydeleteswatch = mydeleteswatches[i];
    if (mydeleteswatch.name != "") {
    
...

Votes

Translate

Translate
People's Champ ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

You may want to try

 

var myDocument = app.activeDocument;

var unused = myDocument.unusedSwatches;

var n = unused.length;

while (n--) unused[n].remove();

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
Community Expert ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

Again, be clear about what's not working. Are you losing some swatches, none? Is there an error? I assume you're calling the trashUnusedSwatch() later on in your script?

 

I wouldn't rely on the unnamed swatches not being randomly at the 0 position, so this alternative may be better:

 

var mydoc = app.documents[0];
var mydeleteswatches = mydoc.unusedSwatches;
for (i = mydeleteswatches.length - 1;i >=0; i--) {
    var mydeleteswatch = mydeleteswatches[i];
    if (mydeleteswatch.name != "") {
        mydeleteswatch.remove();
    }
}

 

 

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
Community Beginner ,
Feb 25, 2021 Feb 25, 2021

Copy link to clipboard

Copied

LATEST

Sorry about that. I'm piecing together a file cleaning script to keep my files organized for print. Thanks so much for your help.

 

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