adobe-photoshop / generator-core

Core Node.js library for Adobe Photoshop CC's Generator extensibility layer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot get artboard background color

canoztokmak opened this issue · comments

commented

By using neither generator nor extend script features, we cannot retrieve the background color for artboards.

Is this intentionally left out or am I missing something ?

Hi @canoztokmak. I wouldn't say it is intentionally left out, but we haven't done the coding to add that since that functionality was introduced relatively recently. The workaround is to add an additional locked shape layer that sits at the bottom of the artboard group. I'll mark this as a feature request. Hopefully it is an option we can easily add.

commented

ok, thanks @chadrolfs .. looking forward to it !

@canoztokmak Here is some extendscript I cobbled together from a few sources to show how the artboard background color can be fetched in the current version of photoshop. This sample script assumes an artboard layer is selected in the open document. Let me if you have trouble getting this approach to work, or if it is not sufficient for your needs.

var classLayer = charIDToTypeID('Lyr ');
var idnull = stringIDToTypeID( "null" );

var classProperty = charIDToTypeID('Prpr');
var typeOrdinal = charIDToTypeID('Ordn');
var enumTarget = charIDToTypeID('Trgt'); 

var idred = charIDToTypeID( 'Rd  ' );
var idgreen = charIDToTypeID( 'Grn ' );
var idblue = charIDToTypeID( 'Bl  ' );
var actionGet = charIDToTypeID( "getd" );

var idtop = stringIDToTypeID( "top" );
var idleft = stringIDToTypeID( "left" );
var idright = stringIDToTypeID( "right" );
var idbottom = stringIDToTypeID( "bottom" );

var idartboard = stringIDToTypeID( "artboard" );
var idartboardRect = stringIDToTypeID( "artboardRect" );
var idartboardColor = stringIDToTypeID( "color" );

function TargetLayerRef(inProp) {
	var theRef = new ActionReference();
	theRef.putProperty(classProperty, inProp);
	theRef.putEnumerated(classLayer, typeOrdinal, enumTarget);

	return theRef;
}

function RefArtboardRect(theRef) {
	var getDescriptor = new ActionDescriptor();
	getDescriptor.putReference(idnull,theRef);
	var artboardRect = executeAction( actionGet, getDescriptor, DialogModes.NO )
						.getObjectValue(idartboard).getObjectValue(idartboardRect);

	return {
		top: artboardRect.getInteger(idtop), 
		left: artboardRect.getInteger(idleft),
		bottom: artboardRect.getInteger(idbottom),
		right: artboardRect.getInteger(idright)
	};
}

function TargetArtboardRect() {
	return RefArtboardRect(TargetLayerRef(idartboard));
}

function RefArtboardColor(theRef) {
	var getDescriptor = new ActionDescriptor();
	getDescriptor.putReference(idnull,theRef);

	var artboardColor = executeAction( actionGet, getDescriptor, DialogModes.NO )
			.getObjectValue(idartboard).getObjectValue(idartboardColor);

	return {
		red:artboardColor.getInteger(idred),
		green:artboardColor.getInteger(idgreen),
		blue:artboardColor.getInteger(idblue)
	};
}

function TargetArtboardColor() {
	return RefArtboardColor(TargetLayerRef(idartboard));
}

// =======================================================

var propArtboardEnabled = stringIDToTypeID( "artboardEnabled" );

function TargetIsArtboard() {
	var getDescriptor = new ActionDescriptor();
	getDescriptor.putReference(idnull,TargetLayerRef(propArtboardEnabled));
	return executeAction( actionGet, getDescriptor, DialogModes.NO ).getBoolean(propArtboardEnabled);
}

function RectToString(inRect) {
	return "" + inRect.top + ","  + inRect.left + ","  + inRect.bottom + "," + inRect.right;
	}

// =======================================================

function RGBToString(inRGB) {
	return "" + inRGB.red + ","  + inRGB.green + ","  + inRGB.blue;
	}


// =======================================================
// Display artboard rectangle of selected artboard.

function DisplayTargetArtboard() {
	if (TargetIsArtboard()) {
		var artboardRect = TargetArtboardRect(),
			artboardColor = TargetArtboardColor();
		alert("Artboard Rect: " + RectToString(artboardRect) + "\nBackground Color: " + RGBToString(artboardColor) );
	}
}

// =======================================================

DisplayTargetArtboard();
commented

hey @mcilroyc ! thanks for the workaround.. it works like a charm, much appreciated !

It seems that the script works only for custom backgrounds. It returns unpredictable results for preset values like "White", "Black" or "Transparent" (

commented

@GDreyV you can distinguish those different types of background colors by retrieving it from artboard object. See below snippet:

var idartboard = stringIDToTypeID( "artboard" );
var idartboardBgType = stringIDToTypeID( "artboardBackgroundType" );
var artboard = executeAction( actionGet, getDescriptor, DialogModes.NO).getObjectValue(idartboard);
var bgType = artboard.getInteger(idartboardBgType);

Great! Thanks a lot!

hey @mcilroyc. Can I take the alpha of the background too?

I'm not sure what you mean @antonisaantoniou. see previous comment about determining if the artboard background is "Transparent" using artboardBackgroundType