Copy link to clipboard
Copied
I've been working on .Net application and ran into a little performance snag. I have a simple method that loops through the channel layers by index, grabs the indexed channel name, and if it matches a predetermined variable then it simple deletes that channel. It works as written but is painfully SLOW. Being fairly new at programming I figured it was just me and opened up ExtendScript to run a few test.
var docChannels = app.activeDocument.channels;
for (i = 0; i < docChannels.length; i++ ){
alert(docChannels.name);
}
To run this script in a document containing only the RGB channels plus 3 other alpha channels takes approximately 30 to 40 seconds. Running the same script modified to cycle through about 20 document layers only takes about 5 seconds.
I don't know if this is a bug in Photoshop CC or not. I don't have any earlier versions to test on. I've can accomplish the programing method by using a Try Catch statement and directly trying to delete the layer by name. This seems to run a little bit faster but not by much and is not really a proper use of a Try Catch statement.
Any insight into this problem or a better way to impliment my programing method would be most appreciated.
I am not sure about .Net apps but I think the reason the javascript Object Model can be slow when working with layers and channels it every time you create a layer or channel object all the properties of that object created. I think building the histogram property is the biggest reason for the slow down. Especially is the document has a large canvas.
Action Manger can be much faster because you can get only the property you need and avoid creating a DOM object and building the histogram. Try this
...Copy link to clipboard
Copied
I am not sure about .Net apps but I think the reason the javascript Object Model can be slow when working with layers and channels it every time you create a layer or channel object all the properties of that object created. I think building the histogram property is the biggest reason for the slow down. Especially is the document has a large canvas.
Action Manger can be much faster because you can get only the property you need and avoid creating a DOM object and building the histogram. Try this to see if faster getting the channel names.
function getProperty( psClass, psKey, index ){// integer:Class, integer:key
var ref = new ActionReference();
if( psKey != undefined ) ref.putProperty( charIDToTypeID( "Prpr" ), psKey );
if(index != undefined ){
ref.putIndex( psClass, index );
}else{
ref.putEnumerated( psClass , charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
}
try{
var desc = executeActionGet(ref);
}catch(e){ return; }// return on error
if(desc.count == 0) return;// return undefined if property doesn't exists
var dataType = desc.getType(psKey);
switch(dataType){// not all types supported - returns undefined if not supported
case DescValueType.INTEGERTYPE:
return desc.getInteger(psKey);
break;
case DescValueType.ALIASTYPE:
return desc.getPath(psKey);
break;
case DescValueType.BOOLEANTYPE:
return desc.getBoolean(psKey);
break;
case DescValueType.BOOLEANTYPE:
return desc.getBoolean(psKey);
break;
case DescValueType.UNITDOUBLE:
return desc.getUnitDoubleValue(psKey);
break;
case DescValueType.STRINGTYPE:
return desc.getString(psKey);
break;
case DescValueType.OBJECTTYPE:
return desc.getObjectValue(psKey);
break;
case DescValueType.LISTTYPE:
return desc.getList(psKey);
break;
case DescValueType.ENUMERATEDTYPE:
return desc.getEnumerationValue(psKey);
break;
}
};
var channelCount = app.activeDocument.channels.length;
var channelNames = [];
for(var channelIndex=1;channelIndex<=channelCount;channelIndex++){
channelNames.push(getProperty(charIDToTypeID("Chnl"),charIDToTypeID("ChnN"),channelIndex));
}
alert(channelNames);
Copy link to clipboard
Copied
Thanks for your Help Michael. I have things running much faster now.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now