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

91

Likes

translate

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

Adobe Community Professional , 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 != "") { ...

Likes

translate

Translate

Translate
Valorous Hero ,
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();

Likes

translate

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
Adobe Community Professional ,
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();
    }
}

 

 

Likes

translate

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

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

 

Likes

translate

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