Hello,
in the never-ending process of learning (Photoshop scripting like everything else), I've always tried to write code that works - which is sometimes a problem itself
Now I'm quite interested in finding out how others solve a problem, why they choose a particular solution, etc.
I've not been officially trained as a programmer - and only very recently I've deepened the study of JS besides PS scripting (I've a fascination with patterns now) - so I'm currently trying to find my own way.
Looking at someone else's code I've found that when ScriptUI windows are involved, there are basically two schools of thought: the Imperative (at least I think it's called imperative) and the Object Oriented.
The first one (as an example for those who owns it, the "Making a Complex Dialog" from Jeffrey Tranberry's Power Speed & Automation in Photoshop) is lain down more or less like that:
Code: Select all/*********************************************
* SETUP
*********************************************/
// Dialog stage #1 - build the dialog
// create the dialog object and its UI objects
var theDialog = createHighPassDialog();
// hook ou the dialog to respond to events
initHighPassDialog(theDialog);
/*********************************************
* MAIN
********************************************/
// Dialog stage #2 - display the dialog
if (runHighPassDialog(theDialog) == 1) {
// user pressed OK button
// Dialog stage #3 - process the dialog results
processHighPassResults(theDialog);
}
/*********************************************
* FUNCTIONS
********************************************/
// create a dialog for creating a High Pass layer
// aka creates the GUI
function createHighPassDialog() {
var myDialog = new Window('dialog', "Create a High Pass Layer");
// ...
return myDialog;
}
// initialize a High Pass dialog
// with EVENT HANDLERS
function initHighPassDialog(myDialog) {
//...
}
// SHOW the dialog and wait for its return value
function runHighPassDialog(myDialog) {
return myDialog.show();
}
// COLLECTS GUI data and run the ACTUAL ROUTINE
function processHighPassResults(myDialog) {
//...
}
// ACTUAL ROUTINE
function HighPassOperation(curFile, radius, blendMode, bIsSharpen, bFlatten) {
//...
};
Others (and possibly this is the case for every Adobe's own scripts) are strongly object oriented - see the Fit Image.jsx for instance.
An object encapsulates all the functions, it is instantiated like var gIP = new FitImage() etc.
I wonder what are the actual pros and cons of both approaches - besides one's personal preferences. Why would you prefer an OO architecture over the imperative one - if you think the difference even matters?
Thank you!
Davide Barranca
http://www.davidebarranca.com
Coding choices (OO / imperative)
Coding choices (OO / imperative)
Hey, Davide. I guess I tend to use the older code like:
Code: Select allvar dlg = new Window('dialog','UI name');
dlg.firstCheckBx = dlg.add('checkbox',undefined,'First Check Box');
dlg.show();
It looks like the code from the Fit Image.jsx is more compact, but I just haven't gotten around to really applying it - I guest I'm set in my ways.
Code: Select allvar dlg = new Window('dialog','UI name');
dlg.firstCheckBx = dlg.add('checkbox',undefined,'First Check Box');
dlg.show();
It looks like the code from the Fit Image.jsx is more compact, but I just haven't gotten around to really applying it - I guest I'm set in my ways.
Coding choices (OO / imperative)
Some 45 years ago my manager through two books at me. The 360 principal of operations and 360 basic assembler language. He also gave me a green card and said John your now a programmer. That was the extent of my programming training. For the next 35 years I earned my living as a programmer. Truth be told I did more system support, system test, designing and implementing test tools and integration. Then implementing user applications code. Most or my coding was in assembler however I did do prototyping in a scripting language named Rexx which I then converted to PL1. I worked on and in many a mainframe OS. Then onto PC s. The only OS that was object oriented was OS2 which Microsoft gave up on and passed on to IBM to slow them down.... So I'm not an OO person never learned Java C or Javascript. However I have hack a quite a few Javascripts starting mostly using cut and paste from other's code. Most of the dialog I have seen are like the one csuebele posted.
I think I might still have that green card though I have not been near a main frame in twenty years. At one time I could read a hex dump as if I was reading 360 assembler code I would even know where I was in the OS if it was a DOS dump.... I have to believe the mainframe DOS is dead by now one more thing I have seen come an go. The question now is will I out live the Tablet.... Cant believe Google glass will replace them....
I think I might still have that green card though I have not been near a main frame in twenty years. At one time I could read a hex dump as if I was reading 360 assembler code I would even know where I was in the OS if it was a DOS dump.... I have to believe the mainframe DOS is dead by now one more thing I have seen come an go. The question now is will I out live the Tablet.... Cant believe Google glass will replace them....
Coding choices (OO / imperative)
Fit Image and other similar script are still really procedural scripts, not object oriented, even though they do have one main function with methods and properties. Fit Image has 15 functions and almost 2 dozen global variables. I think most oop developers would shutter at calling it object oriented.
I think it is more a matter of style rather than procedural/oop. For small or simple scripts I write them similar to the code you posted from Jeff Tranberry. For more complex script I code them like Fit Image or Image Processor.
I think it is more a matter of style rather than procedural/oop. For small or simple scripts I write them similar to the code you posted from Jeff Tranberry. For more complex script I code them like Fit Image or Image Processor.