Extending ArtLayer

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

reimund

Extending ArtLayer

Post by reimund »

Is it possible to add methods to ArtLayer via prototype?

If so, how?

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Extending ArtLayer

Post by Paul MR »

Something like this?

Code: Select allvar ArtLayer = function () {}
ArtLayer.prototype.showOpacity = function() {
    alert(this.opacity);
}
activeDocument.activeLayer.showOpacity();
reimund

Extending ArtLayer

Post by reimund »

My god! Yes!

I don't get that first line in there though, what does it do exactly?
Paul MR

Extending ArtLayer

Post by Paul MR »

For some reason it is needed to first create a constructor for the ArtLayer class.
reimund

Extending ArtLayer

Post by reimund »

It doesn't seem to work when scripts are run from Bridge:

Code: Select all#target bridge
if ( BridgeTalk.appName == "bridge" )  {
   var menu_el = new MenuElement("command","Try prototype","at the beginning of Thumbnail");
   menu_el.onSelect = function () {eval(ah_getTScriptString());}
   function ah_getTScriptString() {
      return "function runscript_ps() {\
         var bt = new BridgeTalk ();\
         bt.target = 'photoshop';\
         bt.body = 'var ArtLayer = function () {}; ArtLayer.prototype.showOpacity = function() { alert(this.opacity);}activeDocument.activeLayer.showOpacity();' \
         bt.send();\
         BridgeTalk.bringToFront ('photoshop');\
      }\
      runscript_ps();"
   }   
}

Any idea what's going on?
reimund

Extending ArtLayer

Post by reimund »

Ok, so Code: Select allvar ArtLayer = function () {}; needs to be run in a global context it seems.

The following does indeed work:

Code: Select all#target bridge
var ArtLayer = function () {};
if ( BridgeTalk.appName == "bridge" )  {
   var menu_el = new MenuElement("command","Try prototype","at the beginning of Thumbnail");
   menu_el.onSelect = function () {eval(ah_getTScriptString());}
   function ah_getTScriptString() {

      return "function runscript_ps() {\
         var bt = new BridgeTalk ();\
         bt.target = 'photoshop';\
         bt.body = 'ArtLayer.prototype.showOpacity = function() { alert(this.opacity);}activeDocument.activeLayer.showOpacity();' \
         bt.send();\
         BridgeTalk.bringToFront ('photoshop');\
      }\
      runscript_ps();"
   }   
}

EDIT: I've been getting mixed results. Sometimes the code in the previous post works as well. Sometimes it doesn't. Not quite sure what's going on.
Paul MR

Extending ArtLayer

Post by Paul MR »

Please try this ...

Code: Select allif ( BridgeTalk.appName == "bridge" )  {
   var menu_el = new MenuElement("command","Try prototype","at the beginning of Thumbnail");
   menu_el.onSelect = function () {
         var bt = new BridgeTalk();
         bt.target = 'photoshop';
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
         bt.send(4);
}

function script(){
app.bringToFront();
ArtLayer = function () {};
ArtLayer.prototype.showOpacity = function() {
   alert(this.opacity);
};
activeDocument.activeLayer.showOpacity();
};
};
reimund

Extending ArtLayer

Post by reimund »

I think I've managed to break it down:

Bridge script:
Code: Select allif ( BridgeTalk.appName == "bridge" )  {
   var menu_el = new MenuElement("command","Try Prototype","at the beginning of Thumbnail");
   menu_el.onSelect = function () {
         var bt = new BridgeTalk();
         bt.target = 'photoshop';
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
         bt.send(4);
    }

    function script(){
        var ps_dir, f, s;
        ps_dir = new File(BridgeTalk.getAppPath('photoshop')).parent;
        ps_dir.changePath('Presets/Scripts/test.jsx');
        f = new File(ps_dir.fsName);
        $.writeln('Running file from Bridge: ' + ps_dir.fsName);
        app.bringToFront();
           
        if (f.open('r'))
             s = f.read();
        else
            throw('failed to open script'+f.fsName);
           
        eval(s);
    };
};

/Presets/Scripts/test.jsx:
Code: Select allvar ArtLayer = function () {}
ArtLayer.prototype.showOpacity = function() {
    alert(this.opacity);
}
$.writeln('Running script from Bridge.');
$.writeln('Active layer: ' + activeDocument.activeLayer);
$.writeln('Prototype: ' + activeDocument.activeLayer.showOpacity);
activeDocument.activeLayer.showOpacity();


Output:
Code: Select allRunning file from Bridge: /Applications/Adobe Photoshop CS5/Presets/Scripts/test.jsx
Running script from Bridge.
Active layer: [ArtLayer Layer 1]
Prototype: undefined


EDIT: Simplified the example.
reimund

Extending ArtLayer

Post by reimund »

Further testing shows that if I once define the prototype, it will stay defined for consequent exections. That's probably what got me confused, seeing it would sometimes work, and sometimes not. To make sure it works the script should therefore be run with a newly started Photoshop.
Paul MR

Extending ArtLayer

Post by Paul MR »

Glad you got it sorted!