Skip to main content
June 3, 2013
Answered

[JS][CS5] rounding decimals in swatch values

  • June 3, 2013
  • 2 replies
  • 642 views

Hello again,

I recently made this script to round decimals in the swatch values if there were any. I'm pretty new to JS so this probably isn't the prettiest code so I was wondering if someone had a better way of doing it.  This works, but sometimes it takes a while and I'm pretty sure it is looking at the [paper], [black], and other swatches that can't be changed.  That is why I made it skip the errors during the script...

function round(color) {

    try { color.space = ColorSpace.cmyk;

    var vals = color.colorValue;

    for (var j = vals.length - 1; j >= 0; j--) {

        vals = Math.round(vals);

    }

    color.colorValue = vals;

} catch (err) {}

}

if (app.documents.length > 0) {

        var target = app.documents[0];

    } else {

        var target = app;

    }

   

var myColors = target.colors.everyItem().getElements();

for (var j = myColors.length - 1; j >= 0; j--) {

    round(myColors);

}

alert("Swatches Rounded");

Any help is appreciated.

This topic has been closed for replies.
Correct answer Dirk Becker

Above script by aguisler86 fell victim to bad forum migration - square bracketed expressions were stripped.

Apparently the initial problem about [paper] was never addressed.

 

function round(color) {
    try { color.space = ColorSpace.cmyk;
    var vals = color.colorValue;
    for (var j = vals.length - 1; j >= 0; j--) {
        vals[j] = Math.round(vals[j]);
    } 
    color.colorValue = vals;
} catch (err) {}
}
if (app.documents.length > 0) {
        var target = app.documents[0];
    } else {
        var target = app;
    }

var myColors = target.colors.everyItem().getElements();
for (var j = myColors.length - 1; j >= 0; j--) {
    round(myColors[j]);
}

alert("Swatches Rounded");

 

2 replies

Dirk BeckerCorrect answer
Legend
December 28, 2024

Above script by aguisler86 fell victim to bad forum migration - square bracketed expressions were stripped.

Apparently the initial problem about [paper] was never addressed.

 

function round(color) {
    try { color.space = ColorSpace.cmyk;
    var vals = color.colorValue;
    for (var j = vals.length - 1; j >= 0; j--) {
        vals[j] = Math.round(vals[j]);
    } 
    color.colorValue = vals;
} catch (err) {}
}
if (app.documents.length > 0) {
        var target = app.documents[0];
    } else {
        var target = app;
    }

var myColors = target.colors.everyItem().getElements();
for (var j = myColors.length - 1; j >= 0; j--) {
    round(myColors[j]);
}

alert("Swatches Rounded");

 

Participating Frequently
December 28, 2024

worked great otherwise though! Thank you very much!

 

Participating Frequently
December 28, 2024

this does not work, but i desperately want it to.