Skip to main content
Known Participant
November 7, 2020
Question

How can I create and use a Javascript class in a PhotoShop script?

  • November 7, 2020
  • 3 replies
  • 3235 views

I am working on a PhotoShop script that will add a frame around my photograph.  There are quite a few parameters I want the user to enter before the frame is generated (frame width, title size, title position, etc.).  I have the dialog set up the way I want it, but I realized that it would make my life quite a bit easier if it was wrapped inside a class.  So, I tried to begin by creating an empty class:

class FrameDialog
{

}

 

And then, in my main script file, the first line is:

#include "FrameDialog.js"

When I run the main script file, I get an error saying "Invalid use of reserved word class".

 

Can I use classes in PhotoShop scripts?  Can I include them from other files?  If so, how?

 

Thanks very much.

 

This topic has been closed for replies.

3 replies

willcampbell7
Legend
November 11, 2020

ExtendScript is ES3, doesn't implement class keyword as does ES6 used by most browsers now. From what you describe, it doesn't seem you even need a class. If you want to store a collection of variables just make an object to store it.

person = {
  name: "Joe",
  age : 30,
  gender: "male"
};

 

Then to use...

var gender = person.gender;

 

William Campbell
Known Participant
November 11, 2020

Cool!

Could you also add functions to that?

Kukurykus
Legend
November 11, 2020

 

function someFunction(){alert(callee.name)}
({objctFrstItm: someFunction}).objctFrstItm()

 

Known Participant
November 9, 2020

It's possible, but not in the way you might want/think...

As Kukurykus said, ExtendScript is about 20 yrs old and something called a Class isn't available, at least, the keyword Class doesn't mean much.

 

This is a way to make something that behaves like a class:

 

(function() {
    this.MyClass = (function() {
        int prop1 = 0;
        function MyClass(param) {
           prop1 = param; 
        };

        MyClass.prototype.MyFunction = function(param1, param2) {
            prop1 = param1;
            return param2;
        };
        return MyClass;
    })();
}).call(this);

 

This part --> (function(){/*... the 'class' content...*/}).call(this);  turns an anonymous function into a "class.

 

And here's an example of how you would call it:

 

#include "./somefolders/MyClass.jsx";
var MyScript = (function() {
    var HoldMyBeer_Instance = "HoldMyBeer will be an instance of MyClass... (sort of.)";

    function constructor() {
        HoldMyBeer_Instance = new MyClass(5);
    };

    constructor.prototype.MyThing = function() {
        return HoldMyBeer_Instance.MyFunction(1,2);
    };
    return constructor;
})();

 

No need to explain the include part, you already got that. 🙂

You can think of The MyScript variable as the Program class in other languages.

In function constructor(){...}, constructor is an arbitrary name, not a reserved keyword.

To attach methods/properties to your class, use constructor.prototype.nameofyourthing = ...

In the example, in MyThing() will call the function MyFunction of the instance of our data class.

Kukurykus
Legend
November 9, 2020

I tried it in ExtendScript ToolKit and got error: "illegal use of reserved word 'int'".

Known Participant
November 9, 2020

My bad... Replace int with var of course...

Kukurykus
Legend
November 7, 2020

ExtendScript uses old implementaion from about 20 years.

Known Participant
November 7, 2020

Thank you.  So, is there a way to code an object in a PhotoShop script today?  Some old syntax I don't know about that will accomplish the same thing?

Kukurykus
Legend
November 7, 2020

Read documentation to see what is possible, also refer to version from 1997 year of JavaScript.