Bennifits of using an object to run a script

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

Moderators: Kukurykus, Tom, xbytor

csuebele

Bennifits of using an object to run a script

Post by csuebele »

What's the advantage of using objects to run scripts rather than just calling functions? I was looking at Image Processor and noticed that most the script is based on assigning the functions to an object: var myObject = function scriptToRun().

Mike Hale

Bennifits of using an object to run a script

Post by Mike Hale »

Xbytor is better at this kind of question than I am but here are my thoughts.

1. It's a matter of style. I think people who learned programming in other languages like java or C think in terms of object, classes, etc. General OOP.

2. You can use the variables in different functions without passing them as arguments or making them global. this.params is an example from Image Priocessor. It's used by several functions.

3. As Adobe and others add to the startup scripts, defining your own class helps with var name clashes. With Image Processor for example, Tom can use any name he wants in the ImageProcessor class without worrying if it has already in scope from some other script.

For what it's worth, when I first started scripting( without a programming background ) I didn't think making your own class was worth the effort. But now I try to do so unless it's a simple script or an example I post for someone.

csuebele

Bennifits of using an object to run a script

Post by csuebele »

Thanks, Mike. That makes sense. You comment did bring another question I had. In the image processor, Tom used var like gFilesFromBridge, which he states are defined in photoshop.jsx. How does that work? You mentioned about variables clashing. How would you use a variable from another script like he used gFilesFromBridge?

Mike Hale

Bennifits of using an object to run a script

Post by Mike Hale »

Yes, gFilesFromBridge is an array of files that in defined in the Photoshop startup script photoshop.jsx. That script is mostly cross-DOM stuff and has a few bugs so you want to test before using something in there in your scripts.

But gFilesFromBridge is defined in several of the cross-DOM methods and you can't really use it because it's not defined until the function is called and then only if it's called from Bridge.

You can make your own startup script to do something similar. It's not easy but it can be done. I made one for web gallery script.

Because the startup scripts are loaded into memory and stay there until Photoshop closes you should make your own class to avoid name clashes.

csuebele

Bennifits of using an object to run a script

Post by csuebele »

Thanks, Mike!

vaster
Posts: 1
Joined: Tue Jun 25, 2019 1:06 pm

Re: Bennifits of using an object to run a script

Post by vaster »

Functions that are stored in object properties are called “methods”.
Methods allow objects to “act” like object.doSomething().
Methods can reference the object as this.
The value of this is defined at run-time.

When a function is declared, it may use this, but that this has no value until the function is called.
A function can be copied between objects.
When a function is called in the “method” syntax: object.method(), the value of this during the call is object.