Skip to main content
Participating Frequently
September 26, 2024
Answered

specific colour swatches are grouped in that folder

  • September 26, 2024
  • 1 reply
  • 2528 views

hello,
I'm new to programming and I'm trying to create a .jsx to make an action where a folder is created and some specific colour swatches are grouped in that folder.

So far I have only been able to create the folder, but I don't know how to make the colour swatches to be selected and put inside this folder.

Could someone help me?

 

thanks

This topic has been closed for replies.
Correct answer m1b

yes! I'm attaching it here, (type the .ait, remove .pdf)
Hopefully it will be solved
Thanks


Well the script works perfectly for me. I chose all the "BM" swatches to add to a swatch group:

 

Here is the exact script I used:

 

(function () {

    // get reference to active document
    var doc = app.activeDocument;

    // get reference to each color swatches I want, stored in array
    var mySwatches = [
        doc.swatches.getByName('BM BLACK'),
        doc.swatches.getByName('BM WHITE'),
        doc.swatches.getByName('BM BROWN'),
        doc.swatches.getByName('BM RED'),
        doc.swatches.getByName('BM GOLD'),
        doc.swatches.getByName('BM GOLD132'),
        doc.swatches.getByName('BM SILVER'),
        doc.swatches.getByName('BM BLUE'),
        doc.swatches.getByName('BM GREEN'),
        doc.swatches.getByName('BM YELLOW'),
        doc.swatches.getByName('BM BLUE LA CAIXA'),
    ];

    // create a swatch group
    var mySwatchGroup = doc.swatchGroups.add();
    mySwatchGroup.name = 'Excellent CMYK Colors';

    // add my swatches to the color group
    for (var i = 0; i < mySwatches.length; i++) {
        mySwatchGroup.addSwatch(mySwatches[i]);
    }

})();

 

 

Do you get the same result from this script? Maybe you need to share your exact script so we can test it with your sample document.

- Mark

1 reply

m1b
Community Expert
Community Expert
September 26, 2024

Hi @monica_5152, here is some code to show you the basics. It will work with the demo.pdf file attached.

(function () {

    // get reference to active document
    var doc = app.activeDocument;

    // get reference to each color swatches I want, stored in array
    var mySwatches = [
        doc.swatches.getByName('CMYK Red'),
        doc.swatches.getByName('CMYK Yellow'),
        doc.swatches.getByName('CMYK Green'),
        doc.swatches.getByName('CMYK Cyan'),
        doc.swatches.getByName('CMYK Blue'),
        doc.swatches.getByName('CMYK Magenta'),
    ];

    // create a swatch group
    var mySwatchGroup = doc.swatchGroups.add();
    mySwatchGroup.name = 'Excellent CMYK Colors';

    // add my swatches to the color group
    for (var i = 0; i < mySwatches.length; i++) {
        mySwatchGroup.addSwatch(mySwatches[i]);
    }

})();

 

You will need to look at the scripting API documentation, such as for Swatch, Swatches, SwatchGroup, SwatchGroups.

 

Good luck!

- Mark

Participating Frequently
September 26, 2024

Hii 🙂 , thank you very much for the answer...Although unfortunately it doesn't work for me, I think the problem is that my swatches are not CMYK, they are spot colours (pantones, etc)...Is it possible that this is the error?

Thank you very much

Participating Frequently
September 27, 2024

Oh, so what happens when you run the script? Do you get an error? I just converted some colours in my demo document into spots and also non-globals and the code worked perfectly. If you look at the docs for SwatchGroup, there is an "addSpot" method. We probably don't need it if your colours are already in swatches.

 

Maybe you could post a little demo document with your colours, and include the code you are using? We'll get to the bottom of it!

- Mark


Hi!
Well the other part I have done it by illustrator actions. basically is to delete the samples that are not used. but then I would like that the remaining ones are grouped in a folder, to be able to do it all from an action.

this is the error that comes out when i run the script.

 

I don't know if it is possible to do that.

Anyway, thank you very much for your help. 🙂