Quantcast
Channel: Adobe Community: Message List - InDesign Scripting
Viewing all articles
Browse latest Browse all 37788

Re: [JS] CS3 and later. Sort the Swatches alphabetically

$
0
0

Ok. Updated version posted:

http://ajarproductions.com/blog/2013/12/13/sort-swatches-in-adobe-inde sign/

 

It should now:

  • Respect (and ignore) the default swatches, despite their current location in the list
  • Include numeric sorting (when the swatch name begins with a number)
  • Undo in a single step

 

I also moved everything into a self-executing function to keep the global space clean:

 

#target indesign

/*
 * Sort Swatches
 * Created by Ajar Productions
 * http://ajarproductions.com
 * version 1.1.0
 */

(function() {
app.doScript(sortSwatches, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT); //can undo entire script

function sortSwatches(){
    var docOpen = (app.documents.length > 0);    var doc = (docOpen) ? app.activeDocument : null;    var swatches = (docOpen) ? doc.swatches : app.swatches;    var colors = (docOpen) ? doc.colors : app.colors;    var tints = (docOpen) ? doc.tints : app.tints;    var inks = (docOpen) ? doc.mixedInks : app.mixedInks;    var gradients = (docOpen) ? doc.gradients : app.gradients;    var swatchNames = swatches.everyItem().name;    swatchNames = swatchNames.sort().sort(sortNum);    var i = 0, len = swatchNames.length, tempSwatch, iSwatch, props, iType;    for(i;i<len;i++){        try{            iSwatch = swatches.item(swatchNames[i]).getElements()[0];            iType = iSwatch.constructor.name;            props = iSwatch.properties;            collection = colors;            switch(iType){                case 'Tint':                    iSwatch.tintValue += .01; //avoid duplicate                    tempSwatch = tints.add(props.baseColor, props);                    iSwatch.remove(tempSwatch);                    break;                case 'MixedInkGroup':                case 'MixedInk':                    break; //creates error -- skip for now                    iSwatch.name = new Date().getTime() + ''; //produce unique value                    tempSwatch = inks.add(props.inkList, props.inkPercentages, props);                    iSwatch.remove(tempSwatch);                    break;                case 'Swatch':                    break; //ignore [None]                case 'Gradient':                case 'Color':                    iSwatch.name = new Date().getTime() + ''; //produce unique value                    tempSwatch = (iType == 'Gradient') ? gradients.add(props) : colors.add(props);                    iSwatch.remove(tempSwatch);                    break;            }        } catch(e) {}    }    function sortNum(a,b){        var aN = parseInt(a), bN = parseInt(b);        if(isNaN(aN)){            if(isNaN(bN)) return 0;            return 1;        } else if(isNaN(bN)){            return - 1;        } else return aN - bN;    }
}

})();

Viewing all articles
Browse latest Browse all 37788

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>