Copy link to clipboard
Copied
Hello!
I know how to get object fillcolor.
myCurrentObject.fillColor
But how can i get custom name of that color, if it exist in swatches? If someone worked with this, please give a hand!
myCurrentObject.fillColor.name // Not works
You need all the objects with color - that exists in the swatch, to bring in the individual layers and layers to assign names - names of swatches?
If I understand correctly, then:
...function comparingArrays ( a, b ) {
var c = 0;
if ( a.length !== b.length ) return false;
for ( var i = 0; i < b.length; i++ ) if ( a === b ) c++;
if ( c === b.length ) return true; else return false;
}
function parseValArr (a) {
var b = []; for ( var i = 0; i < a.length; i++ ) { b.push( Math.round(a) ); }
Copy link to clipboard
Copied
Select the object only and the swatch will also be selected in the Swatches panel, so:
alert(activeDocument.swatches.getSelected()[0].name);
Copy link to clipboard
Copied
Thanks moluapple, but it works only local, then i am clicking myself, i need it to work into cycle like:
for ( i=0; i < Counter; i++ )
Copy link to clipboard
Copied
It's the same, select one object a time in the cycle.
for (var i=0; i < Counter; i++ ) {
// select one object only
app.selection = [obj];
alert(activeDocument.swatches.getSelected()[0].name);
}
Copy link to clipboard
Copied
Thank u, moluapple, i try, but can't get nothing cous my low skill.
I try to insert in in this script, wich we used on insert numbers script., but there is all in functions wich work local.
Please, look on line 156, i need to get name of color in swatch, but not name of RGB values. How can i put script above in it?
/*
* Description: An Adobe Illustrator script that moves objects to separate layers based on the fill color.
* Tested on Illustrator CS5 Windows.
* License: GNU General Public License Version 3. (http://www.gnu.org/licenses/gpl-3.0-standalone.html)
* Copyright (c) 2011, Michel Simons
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// to do:
//
// choose between fill or stroke color
// process locked objects (will be processed by default) and skip objects on locked layers (as they can not be processed);
//
// ********************************************************************************************************
// ** **
// ** initialize variables and start dialog **
// ** **
// ********************************************************************************************************
#target illustrator
if (app.documents.length > 0)
{
if (app.activeDocument.pageItems.length > 0)
{
var release_to_layers_window; // make window global
var doc = app.activeDocument;
var doc_artboard = doc.artboards[0].artboardRect;
var unlocked_layers_count = 0; // number of unlocked layers in layer palette for progress bar
var msis_message_1 = "\nThis script will place each object within this current document into a new layer based on it's fill color. "+
"As the script currently does not detect locked layers and locked objects you need to unlock your layers and objects before you can run this script. " +
"Note that running this script will modify your document. You might want to save your document first.";
var doc = app.activeDocument;
var Counter = app.activeDocument.pageItems.length;
var LayerName = "";
var ReleaseMethod = 1;
// start dialog window
release_to_layers_dialog();
}
// what if no document of no objects?
if (app.documents.length < 1)
{alert("No open documents found. Please open a document before you run this script");}
if (app.documents.length > 0 && app.activeDocument.pathItems.length < 1)
{alert("No objects found in this documents. Please add some objects before you run this script");}
}
// ********************************************************************************************************
// ** **
// ** find the most common value in an array **
// ** **
// ** http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_20871554.html **
// ** var myArray = ["apple","banana","orange","banana","tomato","grape","apple"]; **
// ** alert( GetMostCommonColor(myArray) ); **
// ** **
// ********************************************************************************************************
function GetMostCommonColor(theArray)
{
var tempArray = new Array();
tempArray.length = 0;
tempArray = theArray.slice(0,theArray.length);
tempArray.sort();
tempArray[tempArray.length] = "";
mVal = aVal = "";
mCnt = aCnt = 0;
for(r=0;r<tempArray.length;r++)
{
if(aVal!=tempArray
) {
if(aCnt>mCnt)
{
mCnt = aCnt;
mVal = aVal
}
aVal = tempArray
; aCnt = 1;
} else
{
aCnt++;
}
}
// return [mVal, mCnt]; // returns value and count
return mVal; // only returns value
}
// ********************************************************************************************************
// ** **
// ** create a layer name for the current object **
// ** **
// ********************************************************************************************************
function ConstructLayerNameBasedOnFillColor(myCurrentObject)
{
var myLayerName = "Unknown";
if (myCurrentObject.typename == "TextFrame") { myLayerName = "Text Objects"; }
if (myCurrentObject.typename == "SymbolItem") { myLayerName = "Symbols"; }
if (myCurrentObject.typename == "RasterItem") { myLayerName = "Raster Objects"; }
if (myCurrentObject.typename == "PathItem")
{
if ( myCurrentObject.filled == true )
{
myLayerName = 'Object with unknown Fill Color'; // default name for if no color can be detected
if (myCurrentObject.fillColor == "[CMYKColor]") // CMYK color Item
{
myLayerName = "CMYK: " + Math.round(myCurrentObject.fillColor.cyan) + "," + Math.round(myCurrentObject.fillColor.magenta) + "," + Math.round(myCurrentObject.fillColor.yellow) + "," + Math.round(myCurrentObject.fillColor.black);
}
if (myCurrentObject.fillColor == "[RGBColor]") // RGB color Item
{
myLayerName = "RGB: " + Math.round(myCurrentObject.fillColor.red) + "," + Math.round(myCurrentObject.fillColor.green) + "," + Math.round(myCurrentObject.fillColor.blue);
}
if (myCurrentObject.fillColor == "[GrayColor]") // Gray color Item
{
myLayerName = "Gray: " + Math.round(myCurrentObject.fillColor.gray);
}
if (myCurrentObject.fillColor == "[LabColor]") // LabColor color Item
{
myLayerName = "Lab: " + Math.round(myCurrentObject.fillColor.a) + "," + Math.round(myCurrentObject.fillColor.b) + "," + Math.round(myCurrentObject.fillColor.i);
}
if (myCurrentObject.fillColor == "[GradientColor]") // Gradient color Item
{
myLayerName = "Gradient Fill";
}
if (myCurrentObject.fillColor == "[NoColor]") // No Color Item
{
myLayerName = "No Color";
}
if (myCurrentObject.fillColor == "[PatternColor]") // Pattern Color Item
{
myLayerName = "Pattern Fill";
}
if (myCurrentObject.fillColor == "[SpotColor]") // Spot Color Item
{
// cmyk spot
if (myCurrentObject.fillColor.spot.spotKind == SpotColorKind.SPOTCMYK) // CMYK color Item
{
myLayerName = "Spot CMYK: " + Math.round(myCurrentObject.fillColor.spot.color.cyan) + "," + Math.round(myCurrentObject.fillColor.spot.color.magenta) + "," + Math.round(myCurrentObject.fillColor.spot.color.yellow) + "," + Math.round(myCurrentObject.fillColor.spot.color.black);
}
// rgb spot
if (myCurrentObject.fillColor.spot.spotKind == SpotColorKind.SPOTRGB) // RGB color Item
{
myLayerName = "Spot RGB: " + Math.round(myCurrentObject.fillColor.spot.color.red) + "," + Math.round(myCurrentObject.fillColor.spot.color.green) + "," + Math.round(myCurrentObject.fillColor.spot.color.blue);
}
// lab spot
if (myCurrentObject.fillColor.spot.spotKind == SpotColorKind.SPOTLAB) // RGB color Item
{
myLayerName = "Spot Lab: " + Math.round(myCurrentObject.fillColor.spot.color.a) + "," + Math.round(myCurrentObject.fillColor.spot.color.b) + "," + Math.round(myCurrentObject.fillColor.spot.color.i);
}
}
}
else
{
myLayerName = "Transparant Fill";
}
}
if (myLayerName == undefined) {myLayerName = "Temporary Layer";}
if (myLayerName == "") {myLayerName = "Temporary Layer";}
if (myLayerName.length==0) {myLayerName = "Temporary Layer";}
try
{
doc.layers.getByName(myLayerName);
}
catch (e)
{
doc.layers.add().name = myLayerName;
}
return myLayerName;
}
// ********************************************************************************************************
// ** **
// ** ungroup objects on current layer **
// ** **
// ********************************************************************************************************
function Ungroup(WhereToUngroup, aGroup) // where to ungroup? layer, document, other group?
{
try
{
for (s=aGroup.pageItems.length-1; s>=0; s--)
{
WhereToUngroup = CreateLayerNameFill(myCurrentObject); // ????????? should be something like WhereToUngroup = CreateLayerNameFill(=aGroup.pageItems); // ???????????????????????????
aGroup.pageItems
.move(WhereToUngroup, ElementPlacement.PLACEATBEGINNING);}
}
catch(err)
{
}
}
// ********************************************************************************************************
// ** **
// ** remove layerswith no objects **
// ** **
// ********************************************************************************************************
function RemoveEmptyLayers()
{
var ThisDoc = app.activeDocument;
var ActiveLayer = '';
for (t = ThisDoc.layers.length-1; t>=0; t--)
{
ActiveLayer = ThisDoc.layers
; if (ActiveLayer.pageItems.length == 0)
{
try
{
ActiveLayer.remove();
}
catch(err)
{
}
}
}
}
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// *******************************************************************************************************************************
// ********************************************************************************************************
// ** **
// ** building and showing main dialog **
// ** **
// ********************************************************************************************************
function release_to_layers_dialog() {
// Export dialog
release_to_layers_window = new Window('dialog', 'Release objects to layers based on object fill color');
// PANEL with usage instructions
release_to_layers_window.MessagePanel = release_to_layers_window.add('panel', undefined, 'Release Objects to Layers');
// GROUP
var Message_Group = release_to_layers_window.MessagePanel.add('group', undefined, '')
Message_Group.orientation = 'column';
Message_Group.alignment = [ScriptUI.Alignment.LEFT, ScriptUI.Alignment.TOP]
// Labels
var Message_Group_label_1 = Message_Group.add('statictext', undefined, msis_message_1, {multiline:true});
Message_Group_label_1.size = [500,80];
// PANEL with group and compound path handling
release_to_layers_window.OptionsPanel = release_to_layers_window.add ('panel', undefined, 'Group and Compound Path Items:');
// GROUP
var Radio_Group = release_to_layers_window.OptionsPanel.add('group', undefined, '')
Radio_Group.alignChildren = "left";
Radio_Group.size = [500,65];
Radio_Group.orientation = 'column';
// Radio Boxes
var RadioBox1 = Radio_Group.add ("radiobutton", undefined, "Do not process Group and Compound Path Items.");
var RadioBox2 = Radio_Group.add ("radiobutton", undefined, "Release Groups and Compound Path Items by most common color.");
var RadioBox3 = Radio_Group.add ("radiobutton", undefined, "Ungroup Group and Compound Path Items (caution: might alter shape appearance).");
RadioBox2.value = true;
// PANEL with Progressbar
release_to_layers_window.ProgressPanel = release_to_layers_window.add('panel', undefined, 'Status:');
// GROUP
var Progress_Group = release_to_layers_window.ProgressPanel.add('group', undefined, '')
Progress_Group.orientation = 'column';
Progress_Group.alignment = [ScriptUI.Alignment.LEFT, ScriptUI.Alignment.TOP]
Progress_Group.size = [500,5];
// progressbar
release_to_layers_window.ProgressProgressBar = release_to_layers_window.ProgressPanel.add( 'progressbar', undefined, 0, 100 );
release_to_layers_window.ProgressProgressBar.size = [480,10];
// label
release_to_layers_window.ProgressLabel = release_to_layers_window.ProgressPanel.add('statictext', undefined, 'Found ' + Counter + ' objects in this document.' );
release_to_layers_window.ProgressLabel.size = [ 480,20 ];
// Buttons don't have a PANEL
// GROUP
Button_Group = release_to_layers_window.add('group', undefined, '');
Button_Group.orientation = 'row'
Button_Group.cancelBtn = Button_Group.add('button', undefined, 'Cancel', {name:'cancel'});
Button_Group.cancelBtn.onClick = function() { msis_ai_dlg.close() };
Button_Group.okBtn = Button_Group.add('button', undefined, 'Release', {name:'ok'});
Button_Group.okBtn.onClick = function()
{ Button_Group.okBtn.enabled = false;
Button_Group.cancelBtn.enabled = false;
RadioBox1.enabled = false;
RadioBox2.enabled = false;
RadioBox3.enabled = false;
if (RadioBox1.value == true) {ReleaseMethod = 1;}
if (RadioBox2.value == true) {ReleaseMethod = 2;}
if (RadioBox3.value == true) {ReleaseMethod = 3;}
Process_Objects();
};
release_to_layers_window.show();
}
// ********************************************************************************************************
// ** **
// ** execute main routine when clicking the Release button **
// ** **
// ********************************************************************************************************
function Process_Objects()
{
// Release to layers but skip Groups and Compound Paths Items OR move groups and compound paths but keep then as group objects
if (ReleaseMethod == 1 || ReleaseMethod == 2)
{
for ( i=0; i < Counter; i++ ) //123
{
LayerName = "0";
try
{
CurrentItem = doc.pageItems;
if (CurrentItem.typename != "GroupItem" && CurrentItem.typename != "CompoundPathItem" && CurrentItem.parent.typename != "GroupItem" && CurrentItem.parent.typename != "CompoundPathItem") // no need to create layers for objects within grouped items
{
LayerName = ConstructLayerNameBasedOnFillColor(CurrentItem);
CurrentItem.move( app.activeDocument.layers.getByName( LayerName ), ElementPlacement.PLACEATBEGINNING );
}
} // try
catch(err)
{ // except
alert("An error occured processing objects.\n(" + err + ").\n\nFunction: Process_Objects / method=1.");
} // except
release_to_layers_window.ProgressLabel.text = 'Processed ' + i + ' objects out of ' + Counter + ' objects in total.';
release_to_layers_window.ProgressProgressBar.value = (100 / Counter) * i;
release_to_layers_window.update();
} // for
} // end method 1
// Release to layers, move Group and Compound Path Items based on most common fill color
if (ReleaseMethod == 2)
{
// first move all group objects
LayerName = "0";
for ( j=0; j < doc.groupItems.length; j++ )
{
var CurrentGroupItem = doc.groupItems
; var MyArray = new Array;
var TempLayerName = "";
for (m = CurrentGroupItem.pageItems.length-1; m>=0; m--)
{
TempLayerName = ConstructLayerNameBasedOnFillColor( CurrentGroupItem.pageItems
); MyArray.push( TempLayerName );
}
LayerName = GetMostCommonColor(MyArray);
CurrentGroupItem.moveToBeginning(app.activeDocument.layers.getByName( LayerName ) );
release_to_layers_window.ProgressLabel.text = 'Processed ' + j + ' group objects out of ' + Counter + ' objects in total.';
release_to_layers_window.ProgressProgressBar.value = (100 / Counter) * j;
release_to_layers_window.update();
}
// next move all compound path objects
LayerName = "0";
for ( j=0; j < doc.compoundPathItems.length; j++ )
{
var CurrentCompoundItem = doc.compoundPathItems
; var MyArray = new Array;
var TempLayerName = "";
for (m = CurrentCompoundItem.pathItems.length-1; m>=0; m--)
{
TempLayerName = ConstructLayerNameBasedOnFillColor( CurrentCompoundItem.pathItems
); MyArray.push( TempLayerName );
}
LayerName = GetMostCommonColor(MyArray);
CurrentCompoundItem.moveToBeginning(app.activeDocument.layers.getByName( LayerName ) );
release_to_layers_window.ProgressLabel.text = 'Processed ' + j + ' group objects out of ' + Counter + ' objects in total.';
release_to_layers_window.ProgressProgressBar.value = (100 / Counter) * j;
release_to_layers_window.update();
}
}
// Release to layers and ungroup Group and Compound Path Items
if (ReleaseMethod == 3)
{
for ( k=0; k < Counter; k++ ) //123
{
LayerName = "0";
try
{
CurrentItem = doc.pageItems
; LayerName = ConstructLayerNameBasedOnFillColor(CurrentItem);
if (CurrentItem.typename != "GroupItem" && CurrentItem.typename != "CompoundPathItem") // no need to create layers for objects within grouped items
{
CurrentItem.move( app.activeDocument.layers.getByName( LayerName ), ElementPlacement.PLACEATBEGINNING );
} // try
else
{
UnGroupLayer = app.activeDocument.layers.getByName( LayerName );
Ungroup( UnGroupLayer , CurrentItem );
}
}
catch(err)
{ // except
alert("An error occured processing objects.\n(" + err + ").\n\nFunction: Process_Objects / method=3.");
} // except
release_to_layers_window.ProgressLabel.text = 'Processed ' + k + ' objects out of ' + Counter + ' objects in total.';
release_to_layers_window.ProgressProgressBar.value = (100 / Counter) * k;
release_to_layers_window.update();
} // for
} // end methode 3
RemoveEmptyLayers();
release_to_layers_window.close();
}
Copy link to clipboard
Copied
As an option:
function comparingArrays ( a, b ) {
var c = 0;
if ( a.length !== b.length ) return false;
for ( var i = 0; i < b.length; i++ ) if ( a === b ) c++;
if ( c === b.length ) return true; else return false;
}
function parseValArr (a) {
var b = []; for ( var i = 0; i < a.length; i++ ) { b.push( Math.round(a) ); }
return arr;
}
function getColorValues ( color ) {
if ( color === undefined ) return undefined;
else if ( color.typename === 'CMYKColor' ) return [ color.cyan, color.magenta, color.yellow, color.black ];
else if ( color.typename === 'RGBColor' ) return [ color.red, color.green, color.blue ];
else if ( color.typename === 'LabColor' ) return [ color.l, color.a, color.b ];
else if ( color.typename === 'SpotColor' ) return getColorValues( color.spot.color );
else if ( color.typename === 'GrayColor' ) return [ color.gray ];
}
function getSwatchOfTheColor ( color ) {
var s = activeDocument.swatches,
i = s.length,
arr = [];
while ( i-- ) {
if ( s.color.typename === color.typename ) {
if ( comparingArrays( parseValArr( getColorValues(s.color) ) , parseValArr( getColorValues(color) ) ) ) {
arr.push( s );
}
}
}
return arr;
}
alert( getSwatchOfTheColor( selection[0].fillColor )[0].name );
Copy link to clipboard
Copied
Thanks Alexander. Can u please put it into that big script, to rename layers by color swatch name? I can't understend how to compute them.
Copy link to clipboard
Copied
You need all the objects with color - that exists in the swatch, to bring in the individual layers and layers to assign names - names of swatches?
If I understand correctly, then:
function comparingArrays ( a, b ) {
var c = 0;
if ( a.length !== b.length ) return false;
for ( var i = 0; i < b.length; i++ ) if ( a === b ) c++;
if ( c === b.length ) return true; else return false;
}
function parseValArr (a) {
var b = []; for ( var i = 0; i < a.length; i++ ) { b.push( Math.round(a) ); }
return b;
}
function getColorValues ( color ) {
if ( color === undefined ) return undefined;
else if ( color.typename === 'CMYKColor' ) return [ color.cyan, color.magenta, color.yellow, color.black ];
else if ( color.typename === 'RGBColor' ) return [ color.red, color.green, color.blue ];
else if ( color.typename === 'LabColor' ) return [ color.l, color.a, color.b ];
else if ( color.typename === 'SpotColor' ) return getColorValues( color.spot.color );
else if ( color.typename === 'GrayColor' ) return [ color.gray ];
}
function getSwatchOfTheColor ( color ) {
var s = activeDocument.swatches,
i = s.length,
arr = [];
while ( i-- ) {
if ( s.color.typename === color.typename ) {
if ( comparingArrays( parseValArr( getColorValues(s.color) ) , parseValArr( getColorValues(color) ) ) ) {
arr.push( s );
}
}
}
return arr;
}
function setAttr ( obj, options ) {
if ( options ) {
for ( var i in options ) {
for ( var j in obj ) {
if ( i === j && !(options instanceof Function) ) {
obj
= options; }
}
}
}
return obj;
}
function createLayer ( obj, options ) {
var layer = activeDocument.layers.add();
obj.moveToBeginning( layer );
setAttr( layer, options );
}
function process ( objects ) {
for ( var i = 0; i < objects.length; i++ ) {
if ( objects.typename === 'GroupItem' ) {
process( objects.pageItems );
}
else {
var swatchColor = getSwatchOfTheColor( objects.fillColor )[0];
if ( swatchColor ) createLayer( objects, { name: swatchColor.name } );
}
}
}
process( selection );
Copy link to clipboard
Copied
Works! Thank u very much!
Copy link to clipboard
Copied
return getColorValues( color.spot.color );
That's scrumptious!