Hello,
Is there a way to set a CSXSLibrary event listener to capture when a layer is created, renamed or removed?
Currently I have this:
Code: Select all//here is the part that is not working
//I believe I am missing the right way to capture the last listener
ExternalInterface.addCallback("PhotoshopCallback" + CSXSInterface.instance.getExtensionId(), PhotoshopCallback);
//I got this part from the forums here
public function PhotoshopCallback(eventID:Number, descID:Number):void{
if(eventID == charToInteger("Mk ") ||
eventID == charToInteger("Cls ") ||
eventID == charToInteger("Opn ") ||
eventID == charToInteger("undo") ||
eventID == charToInteger("setd") ||
eventID == charToInteger("slct") ){
getAlert();
}
}
public function getAlert():void{
var reqResult:SyncRequestResult = CSXSInterface.instance.evalScript("getAlert");
}
With the code above, nothing happens. If I enable this line:
Code: Select allCSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("slct").toString());
I start to receive alerts (from the getAlert function in a jsx) when I click every tool but not when a layer is created or renamed.
EventListener to check when a layer is created...
-
c.buliarca
EventListener to check when a layer is created...
First you should register for the 'setd' ( this is used when you rename the layer), 'Mk '( when you are creating a new layer) and 'Dlt '( when deleting).
The code should look like this:
Code: Select all
private function init():void{
CSXSInterface.instance.evalScript("PhotoshopUnPersistent");
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("setd").toString());
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("Mk ").toString());
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("Dlt ").toString());
ExternalInterface.addCallback("PhotoshopCallback"+ CSXSInterface.getInstance().getExtensionId(), PhotoshopCallback);
CSXSInterface.getInstance().addEventListener(StateChangeEvent.WINDOW_CLOSE, windowClose)
}
public function PhotoshopCallback(eventID:Number, descID:Number):void{
if(eventID == charToInteger("Mk ") || eventID == charToInteger("Dlt ") || eventID == charToInteger("setd")){
CSXSInterface.instance.evalScript("descriptorA", descID.toString());
}
}
public function charToInteger(keyword:String):Number{
var value:Number;
value = keyword.charCodeAt(0) * 256 * 256 * 256;
value += keyword.charCodeAt(1) * 256 * 256;
value += keyword.charCodeAt(2) * 256;
value += keyword.charCodeAt(3);
return value;
}
public function windowClose(StateChangeEvent):void{
CSXSInterface.instance.evalScript("PhotoshopUnRegisterEvent", charToInteger("setd").toString());
CSXSInterface.instance.evalScript("PhotoshopUnRegisterEvent", charToInteger("Mk ").toString());
CSXSInterface.instance.evalScript("PhotoshopUnRegisterEvent", charToInteger("Dlt ").toString());
}
and the descriptorA function from your .jsx file:
Code: Select allfunction descriptorA( descA ){
var desc = new ActionDescriptor();
desc.fromID(parseInt(descA));
// code adapted from: http://forums.adobe.com/thread/1258886?tstart=0
// get the list of what was modifyed
var list = desc.getList(charIDToTypeID('null'));
// get the actionReferences from the list
var ref = list.getReference(0);
var psClass = ref.getDesiredClass();
// make sure it was a layer that was modifyed
if(psClass == charIDToTypeID('Lyr ')){
// check to see what is in the reference
var dataEnum = ref.getForm();
// should either be an enum if activeLayer
if(dataEnum == ReferenceFormType.ENUMERATED) {
alert("DOM: " + app.activeDocument.activeLayer.name);
}else{// or the layer name
alert(ref.getName());
}
}
}
The code should look like this:
Code: Select all
private function init():void{
CSXSInterface.instance.evalScript("PhotoshopUnPersistent");
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("setd").toString());
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("Mk ").toString());
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", charToInteger("Dlt ").toString());
ExternalInterface.addCallback("PhotoshopCallback"+ CSXSInterface.getInstance().getExtensionId(), PhotoshopCallback);
CSXSInterface.getInstance().addEventListener(StateChangeEvent.WINDOW_CLOSE, windowClose)
}
public function PhotoshopCallback(eventID:Number, descID:Number):void{
if(eventID == charToInteger("Mk ") || eventID == charToInteger("Dlt ") || eventID == charToInteger("setd")){
CSXSInterface.instance.evalScript("descriptorA", descID.toString());
}
}
public function charToInteger(keyword:String):Number{
var value:Number;
value = keyword.charCodeAt(0) * 256 * 256 * 256;
value += keyword.charCodeAt(1) * 256 * 256;
value += keyword.charCodeAt(2) * 256;
value += keyword.charCodeAt(3);
return value;
}
public function windowClose(StateChangeEvent):void{
CSXSInterface.instance.evalScript("PhotoshopUnRegisterEvent", charToInteger("setd").toString());
CSXSInterface.instance.evalScript("PhotoshopUnRegisterEvent", charToInteger("Mk ").toString());
CSXSInterface.instance.evalScript("PhotoshopUnRegisterEvent", charToInteger("Dlt ").toString());
}
and the descriptorA function from your .jsx file:
Code: Select allfunction descriptorA( descA ){
var desc = new ActionDescriptor();
desc.fromID(parseInt(descA));
// code adapted from: http://forums.adobe.com/thread/1258886?tstart=0
// get the list of what was modifyed
var list = desc.getList(charIDToTypeID('null'));
// get the actionReferences from the list
var ref = list.getReference(0);
var psClass = ref.getDesiredClass();
// make sure it was a layer that was modifyed
if(psClass == charIDToTypeID('Lyr ')){
// check to see what is in the reference
var dataEnum = ref.getForm();
// should either be an enum if activeLayer
if(dataEnum == ReferenceFormType.ENUMERATED) {
alert("DOM: " + app.activeDocument.activeLayer.name);
}else{// or the layer name
alert(ref.getName());
}
}
}