Skip to main content
September 30, 2010
Question

[JS] CS3 and later. Sort the Swatches alphabetically

  • September 30, 2010
  • 4 replies
  • 7162 views

Is there a way to sort the swatches, like I did for the paragraphStyles, characterStyles, and so on...?

There is no swatches.move(Location_option) in CS3 nor CS4 like paragraphStyles have.

Would CS5 have it?

Thanks, Alex.

This topic has been closed for replies.

4 replies

Justin Putney
Known Participant
December 13, 2013

This is an old thread, but I found while looking for a solution to this issue. I wrote a script that will sort Swatches, Tints, and Gradients.

Easy installers are available here:
http://ajarproductions.com/blog/2013/12/13/sort-swatches-in-adobe-indesign/

For those interested in the code, I used add and remove to re-order the items:

#target indesign

/*

* Created by Ajar Productions

* http://ajarproductions.com

*/

var docOpen = (app.documents.length > 0);

var swatches = (docOpen) ? app.activeDocument.swatches : app.swatches;

var colors = (docOpen) ? app.activeDocument.colors : app.colors;

var tints = (docOpen) ? app.activeDocument.tints : app.tints;

var gradients = (docOpen) ? app.activeDocument.gradients : app.gradients;

var swatchNames = swatches.everyItem().name;

swatchNames.splice(0,4);

swatchNames = swatchNames.sort();

var i = 0, len = swatchNames.length, tempSwatch, iSwatch, props;

for(i;i<len;i++){

    try{

        iSwatch = swatches.item(swatchNames).getElements()[0];

        props = iSwatch.properties;

        if(!iSwatch.hasOwnProperty('tintValue')) {

            iSwatch.name = new Date().getTime() + ''; //produce unique value

            tempSwatch = (iSwatch.hasOwnProperty('gradientStops')) ? gradients.add(props) : colors.add(props);

            iSwatch.remove(tempSwatch);

        } else {

            iSwatch.tintValue += .01; //avoid duplicate

            tempSwatch = tints.add(props.baseColor, props);

            iSwatch.remove(tempSwatch);

        }

    } catch(e) {}

}

Community Expert
December 13, 2013

@Justin – well, you may or you may not followed that lengthy discussion on the Swiss/German forum Peter Kahrel pointed to…

There in answer #3 I remarked, that one can well move around all the four standard swatches like [Paper], [Black]… They are not necessarily at the beginning of the Swatches collection.

http://www.hilfdirselbst.ch/foren/Farbfelder_aufsteigend_sortieren_von_A-Z_P413954.html#413954

I did a little experiment with your script:

Screenshot 1:

(Before sort)

Screenshot 2:

(After sort)

And, as you can see: the sort() should be a more elaborate function to do its job right…

(Same problems we ran into some years ago at hilfdirselbst.ch (InDesign Scripting subforum)

Uwe

Community Expert
December 13, 2013

Just a hint, how we could eliminate the problem with the standard swatches (not tested):

look at the ID numbers of the swatches.

No need to sort them with a sort(). The first 2-digit-ones (according to my example file of InDesign CS5.5) are the basic swatches right after the document came into existance. Of course there could be more than 10, if the user added swatches before opening any document. And the position of the four basic ones you cannot remove could be arbitrary as well (you can move them around with no document open).

var allSwatches = app.documents[0].swatches.everyItem();

$.writeln(allSwatches.id.join("\r") +"\r"+allSwatches.name.join("\r"));

//Result:

11

18

20

16

19

22

23

21

14

15

213

214

215

216

217

Black

C=0 M=0 Y=100 K=0

C=100 M=0 Y=0 K=0

Registration

C=0 M=100 Y=0 K=0

C=15 M=100 Y=100 K=0

C=75 M=5 Y=100 K=0

C=100 M=90 Y=10 K=0

None

Paper

1

10

100

2

3

Uwe

Community Expert
November 24, 2011

There's a longish thread in the HDS forum (http://www.hilfdirselbst.ch/foren/Farbfelder_aufsteigend_sortieren_von_A-Z_P484808.html) that discusses sorting swatches.

John Hawkinson
Inspiring
November 24, 2011

It's certainly fun to read this stuff in translated German. Especially amusing that Google Translate converts the comments in the script...

Anyhow, has anyone tried exporting to IDML and sorting the swatches there and then reopening?

I guess it depends on how badly this feature is desired.

Participant
November 15, 2011

Has anyone figured this out yet? Just bumping this.

November 24, 2011

Not is CS3. CS5 Have a sort javascript command

Jongware
Community Expert
Community Expert
November 24, 2011

So? It was there in CS3 as well. It can sort any Javascript object tou like but the swatches in INDesign are no ordinary Javascript objects.

Sure you can sort the swatches but it doesn't do any good as you cannot put them back into the panel in re-sorted order.

Jongware
Community Expert
Community Expert
September 30, 2010
Would CS5 have it?

Not that I know. It's not in the DOM Help, which isn't really promising.

I tried this, by way of Fun Experiment of the Day:

1. Get swatch names

2. Sort

3. Duplicate in sorted order

4. Remove originals, replacing with duplicates

5. Rename duplicates to original names.

Seems to work for "regular" swatches, but tints, for one, cause an error. Perhaps somebody else know how to circumvent this. And I wonder what will happen with "imported" swatches -- those cannot be deleted, right?

Please, only try this on a copy of your Really Important Document!

//DESCRIPTION:Amazing Swatch Sorter

// (And You Thought It Could Not Be Done)

// A Jongware Script, 30-Sep-2010

allSwatchNames = app.activeDocument.swatches.everyItem().name;

allSwatchNames.splice (0,4);

for (order=0; order<allSwatchNames.length; order++)

     allSwatchNames[order] = [ allSwatchNames[order], app.activeDocument.swatches[order+4], 0 ];

allSwatchNames.sort();

for (order=0; order<allSwatchNames.length; order++)

     allSwatchNames[order][2] = allSwatchNames[order][1].duplicate();

for (order=0; order<allSwatchNames.length; order++)

     allSwatchNames[order][1].remove (allSwatchNames[order][2]);

for (order=0; order<allSwatchNames.length; order++)

     allSwatchNames[order][2].name = allSwatchNames[order][0];

Harbs.
Legend
September 30, 2010

I could swear that Dave wrote a script that does this. I can't seem to find it now...

Harbs