Is it possible to add methods to ArtLayer via prototype?
If so, how?
Extending ArtLayer
Extending ArtLayer
Something like this?
Code: Select allvar ArtLayer = function () {}
ArtLayer.prototype.showOpacity = function() {
alert(this.opacity);
}
activeDocument.activeLayer.showOpacity();
Code: Select allvar ArtLayer = function () {}
ArtLayer.prototype.showOpacity = function() {
alert(this.opacity);
}
activeDocument.activeLayer.showOpacity();
Extending ArtLayer
My god! Yes!
I don't get that first line in there though, what does it do exactly?
I don't get that first line in there though, what does it do exactly?
Extending ArtLayer
For some reason it is needed to first create a constructor for the ArtLayer class.
Extending ArtLayer
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?
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?
Extending ArtLayer
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.
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.
Extending ArtLayer
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();
};
};
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();
};
};
Extending ArtLayer
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.
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.
Extending ArtLayer
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.