Skip to content

Instantly share code, notes, and snippets.

@xitude
Last active May 10, 2017 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xitude/ec346b39ec9a1f6bebaa125e40c9077a to your computer and use it in GitHub Desktop.
Save xitude/ec346b39ec9a1f6bebaa125e40c9077a to your computer and use it in GitHub Desktop.
Accessing Smart Objects in Photoshop
#target photoshop
if (app.documents.length > 0) {
var existingColor = prompt("Colour to change: color(r,g,b - e.g. 255,255,255 or Hex - e.g. FFFFFF)", "255,255,255");
var newColor = prompt("New colour: color(r,g,b - e.g. 255,255,255 or Hex - e.g. FFFFFF)", "255,255,255");
var newRed = 0, newGreen = 0, newBlue = 0;
if (newColor.indexOf(",") > 0) {
var list = newColor.split(",");
newRed = parseFloat(list[0]);
newGreen = parseFloat(list[1]);
newBlue = parseFloat(list[2]);
} else {
newRed = parseInt(newColor.substr(0, 2), 16);
newGreen = parseInt(newColor.substr(2, 2), 16);
newBlue = parseInt(newColor.substr(4, 2), 16);
}
var existingRed = 0, existingGreen = 0, existingBlue = 0;
if (existingColor.indexOf(",") > 0) {
var list = existingColor.split(",");
existingRed = parseFloat(list[0]);
existingGreen = parseFloat(list[1]);
existingBlue = parseFloat(list[2]);
} else {
existingRed = parseInt(existingColor.substr(0, 2), 16);
existingGreen = parseInt(existingColor.substr(2, 2), 16);
existingBlue = parseInt(existingColor.substr(4, 2), 16);
}
var myDocument = app.activeDocument;
selectAllLayers();
var theLayers = getSelectedSoidColorLayersIdentifier( existingRed, existingGreen, existingBlue );
for (var m = 0; m < theLayers.length; m++) {
if ( activeLayer.kind != LayerKind.SMARTOBJECT ){
var smartObjectDoc = Stdlib.editSmartObject(doc, activeLayer);
}else{
changeSolidColor( theLayers[m], newRed, newGreen, newBlue );
}
}
deselectLayers();
};
//////////////////////////////////////////
function deselectLayers() {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(stringIDToTypeID('selectNoLayers'), desc1, DialogModes.NO);
}
function selectAllLayers() {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(stringIDToTypeID('selectAllLayers'), desc1, DialogModes.NO);
}
function dump(v, howDisplay, recursionLevel) {
howDisplay = (typeof howDisplay === 'undefined') ? "alert" : howDisplay;
recursionLevel = (typeof recursionLevel !== 'number') ? 0 : recursionLevel;
var vType = typeof v;
var out = vType;
switch (vType) {
case "number":
/* there is absolutely no way in JS to distinguish 2 from 2.0
so 'number' is the best that you can do. The following doesn't work:
var er = /^[0-9]+$/;
if (!isNaN(v) && v % 1 === 0 && er.test(3.0))
out = 'int';*/
case "boolean":
out += ": " + v;
break;
case "string":
out += "(" + v.length + '): "' + v + '"';
break;
case "object":
//check if null
if (v === null) {
out = "null";
}
//If using jQuery: if ($.isArray(v))
//If using IE: if (isArray(v))
//this should work for all browsers according to the ECMAScript standard:
else if (Object.prototype.toString.call(v) === '[object Array]') {
out = 'array(' + v.length + '): {\n';
for (var i = 0; i < v.length; i++) {
out += repeatString(' ', recursionLevel) + " [" + i + "]: " +
dump(v[i], "none", recursionLevel + 1) + "\n";
}
out += repeatString(' ', recursionLevel) + "}";
}
else { //if object
sContents = "{\n";
cnt = 0;
for (var member in v) {
//No way to know the original data type of member, since JS
//always converts it to a string and no other way to parse objects.
sContents += repeatString(' ', recursionLevel) + " " + member +
": " + dump(v[member], "none", recursionLevel + 1) + "\n";
cnt++;
}
sContents += repeatString(' ', recursionLevel) + "}";
out += "(" + cnt + "): " + sContents;
}
break;
}
if (howDisplay == 'body') {
var pre = document.createElement('pre');
pre.innerHTML = out;
document.body.appendChild(pre)
}
else if (howDisplay == 'alert') {
alert(out);
}
return out;
}
function repeatString(str, num) {
out = '';
for (var i = 0; i < num; i++) {
out += str;
}
return out;
}
////// based on code by paul mr //////
function getSelectedSoidColorLayersIdentifier( existingRed, existingGreen, existingBlue ){
var selectedLayers = new Array();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
// run through selected layers;
for(var i=0;i<c;i++){
try{activeDocument.backgroundLayer;
var theIndex = desc.getReference( i ).getIndex();
}catch(e){var theIndex = desc.getReference( i ).getIndex()+1 };
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theIdentifier = layerDesc.getInteger(stringIDToTypeID("layerID"));
var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
//var theColors = adjList.getObjectValue(0).getObjectValue(charIDToTypeID('Clr '));
var textColor = new SolidColor;
textColor.rgb.red = theColors.getInteger(charIDToTypeID('Rd '));
textColor.rgb.green = theColors.getInteger(charIDToTypeID('Grn '));
textColor.rgb.blue = theColors.getInteger(charIDToTypeID('Bl '));
if( textColor.rgb.red == existingRed && textColor.rgb.green == existingGreen && textColor.rgb.blue == existingBlue ) {
selectedLayers.push( theIdentifier );
}
} catch (e) {
//alert(e.message)
};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var adjList = layerDesc.getList(stringIDToTypeID('adjustment'));
var theColors = adjList.getObjectValue(0).getObjectValue(stringIDToTypeID('color'));
selectedLayers = [theIdentifier]
} catch (e) {};
};
return selectedLayers;
};
////// change color of solid color layer //////
function changeSolidColor( theIdentifier, theR, theG, theB, existingRed, existingGreen, existingBlue ) {
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc4 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
ref1.putIdentifier ( stringIDToTypeID( "contentLayer" ), theIdentifier );
desc4.putReference( idnull, ref1 );
var idT = charIDToTypeID( "T " );
var desc5 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc6 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc6.putDouble( idRd, theR );
var idGrn = charIDToTypeID( "Grn " );
desc6.putDouble( idGrn, theG );
var idBl = charIDToTypeID( "Bl " );
desc6.putDouble( idBl, theB );
var idRGBC = charIDToTypeID( "RGBC" );
desc5.putObject( idClr, idRGBC, desc6 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc4.putObject( idT, idsolidColorLayer, desc5 );
executeAction( idsetd, desc4, DialogModes.NO );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment