Highlighted

Photoshop's javascript API

Community Beginner ,
Sep 26, 2020

Copy link to clipboard

Copied

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 );

 

TOPICS
Actions and scripting, Performance

Views

150

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Photoshop's javascript API

Community Beginner ,
Sep 26, 2020

Copy link to clipboard

Copied

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 );

 

TOPICS
Actions and scripting, Performance

Views

151

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 26, 2020 0
Adobe Community Professional ,
Sep 26, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 26, 2020 3
Participant ,
Sep 28, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 2
LEGEND ,
Sep 28, 2020

Copy link to clipboard

Copied

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. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 2
Participant ,
Sep 28, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 1
Community Beginner ,
Sep 28, 2020

Copy link to clipboard

Copied

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? 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 0
Community Beginner ,
Sep 28, 2020

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 0
Participant ,
Sep 28, 2020

Copy link to clipboard

Copied

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! 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 28, 2020 0