Skip to main content
joef19359312
Participating Frequently
September 27, 2020
Answered

Photoshop's javascript API

  • September 27, 2020
  • 5 replies
  • 791 views

I am fairly new to writing javascript code for photoshop. I have read the documentation from adobe about the API and the DOM objects. I have written a number of scripts now and the core of them are built using the output from the script listener tool.  My question is, do most people use the code from the script listener or do they try to use the methods in the API?  For example if I want to load a saved selection using the API, I can write:

var selRef = app.activeDocument.selection;
selRef.load(app.activeDocument.channels["c1"], SelectionType.REPLACE);

or use the 14 lines generated by the script listener. 

 

var idsetd = charIDToTypeID( "setd" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref1.putProperty( idChnl, idfsel );
desc5.putReference( idnull, ref1 );
var idT = charIDToTypeID( "T   " );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
ref2.putName( idChnl, "c1" );
desc5.putReference( idT, ref2 );
executeAction( idsetd, desc5, DialogModes.NO );

 

This topic has been closed for replies.
Correct answer Chuck Uebele

The code from scriptListener tends to run faster, so a lot of people try and use that, whenever possible. Some things have to be done using Action Manager code. DOM code is easier to write and read. I tend to use both.

5 replies

joef19359312
Participating Frequently
September 28, 2020

Thanks everyone for the guidence.  This is pretty much what I have been doing. I wrap the scriptlistener code into functions that I call with readable names from the main function.  I have also been using include statements in order to reuse common code. Is this standard practice as well?

Inspiring
September 28, 2020

In my experience & to the best of my knowledge... Absolutely!

To me it seems you are very much on the right track to learn and grow and make amazing things.

Looking forward to seeing your work out in the wild! 🙂

joef19359312
Participating Frequently
September 28, 2020

Thanks everyone for the guidence.  This is pretty much what I have been doing. I wrap the scriptlistener code into functions that I call with readable names from the main function.  I have also been using include statements in order to reuse common code. Is this standard practice as well? 

 

 

c.pfaffenbichler
Community Expert
Community Expert
September 28, 2020

I concur with the previous posters’ opinion that a combination of AM and DOM code makes sense. 

 

The difference in speed may not be noticable with many tasks, but with Scripts that repeat an operation often (be it duplicating and transforming Smart Objects, placing Layers, applying multiple Filters, …) the difference can become relevant indeed. 

 

The library-approach is certainly prudent (xbytor used to be big on that, if I remember correctly) but I personally barely use them. My lack of discipline in that regard should not count as a recommendation to do likewise, though. 

Inspiring
September 28, 2020

I agree on the reference to xbytor.

Especially if you don't know who is going to be using your scripts using the DOM as much as possible is very valuable.

 

If you want to distribute your scripts, and are usingScriptListener code, it is extremely important to handle things like Photoshop's units of measurement, default colors, bit depth ... ScriptListener will -at times- not write a log of a setting if that setting was already set to the right value*. That's why I wrap my frequently used functions in a "library", it handles the exceptions I've come across in the past. 

With DOM you won't have that problem, it will do that for you, although sometimes a bit slower.

 

If you're automating your personal workflow, and you don't intend to share the scripts, or if you distribute your scripts in a controlled environment (your photo studio, some of your colleagues/friends) you can also afford not to check for everything. Although a programmer looking at your code might have an anxiety attack, it will work.

Inspiring
September 28, 2020

As Chuck said... I use both as well.

ScriptListener code because it's generally faster, DOM code for more complex stuff. (talking to the internet from photoshop)

I've wrapped the code I use frequently into a small library. It's cleaner/easier to read and it is structured the same way my brains thinks about the scripts. I use that for every script I make.

Given enough  interest, I'd be willing to clean it up a bit and put it on github.

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
September 27, 2020

The code from scriptListener tends to run faster, so a lot of people try and use that, whenever possible. Some things have to be done using Action Manager code. DOM code is easier to write and read. I tend to use both.