Skip to main content
владимирк76249718
Known Participant
August 4, 2018
Answered

How to pass a variable from js to jsx?

  • August 4, 2018
  • 4 replies
  • 4277 views

Good day dear audience!

Sorry for my bad English. I ask for help in writing a script for the extension panel.

There is a script button and a slider:

<button class="tool" id="gb_3b"><div class="inst">Start</div></button>

<b>Middle radius: <span style="font-size: 1.2em" id="gb3rval"></span></b>

<div id="gblur3rad"></div>

Running the script and initializing the slider in JS:

$("#gb_3b").button().click(function(a) {

    csInterface.evalScript('$._ext.evalFile("' + extensionRoot + 'work/Smart_Frequency_3_Band_GB.jsxbin")')

});

$(function() {

            $('#gblur3rad').slider({   

                min: 0.1,

                max: 20,

                step: 0.1,

                value: 5,

                create: setInputsFromSlider1,

                slide: setInputsFromSlider1,

                stop: setInputsFromSlider3

            });           

            function setInputsFromSlider1() {

                $('#gb3rval').text($('#gblur3rad').slider("value"));

            };

            function setInputsFromSlider3() {

                var radius = $('#gblur3rad').slider("value");

                csInterface.evalScript('GBlur3S("'+radius+'")', function() {csInterface.evalScript('GBlur3S2("'+radius+'")');});

            };

};

main.jsx which is specified in the manifest:

function GBlur3S(radius) {

   

var idslct = charIDToTypeID( "slct" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref5 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref5.putName( idLyr, "Base Middle GB" );

    desc16.putReference( idnull, ref5 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc16.putBoolean( idMkVs, false );

    var idLyrI = charIDToTypeID( "LyrI" );

        var list3 = new ActionList();

        list3.putInteger( 3 );

    desc16.putList( idLyrI, list3 );

executeAction( idslct, desc16, DialogModes.NO );

var idsetd = charIDToTypeID( "setd" );

    var desc34236 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref236 = new ActionReference();

        var idfilterFX = stringIDToTypeID( "filterFX" );

        ref236.putIndex( idfilterFX, 1 );

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref236.putEnumerated( idLyr, idOrdn, idTrgt );

    desc34236.putReference( idnull, ref236 );

    var idfilterFX = stringIDToTypeID( "filterFX" );

        var desc34237 = new ActionDescriptor();

        var idFltr = charIDToTypeID( "Fltr" );

            var desc34238 = new ActionDescriptor();

            var idRds = charIDToTypeID( "Rds " );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc34238.putUnitDouble( idRds, idPxl, radius );

        var idGsnB = charIDToTypeID( "GsnB" );

        desc34237.putObject( idFltr, idGsnB, desc34238 );

    var idfilterFX = stringIDToTypeID( "filterFX" );

    desc34236.putObject( idfilterFX, idfilterFX, desc34237 );

executeAction( idsetd, desc34236, DialogModes.NO );

};

function GBlur3S2(radius) {

   

var idslct = charIDToTypeID( "slct" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref5 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref5.putName( idLyr, "Base Middle GB (H)" );

    desc16.putReference( idnull, ref5 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc16.putBoolean( idMkVs, false );

    var idLyrI = charIDToTypeID( "LyrI" );

        var list3 = new ActionList();

        list3.putInteger( 3 );

    desc16.putList( idLyrI, list3 );

executeAction( idslct, desc16, DialogModes.NO );

var idsetd = charIDToTypeID( "setd" );

    var desc34236 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref236 = new ActionReference();

        var idfilterFX = stringIDToTypeID( "filterFX" );

        ref236.putIndex( idfilterFX, 1 );

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref236.putEnumerated( idLyr, idOrdn, idTrgt );

    desc34236.putReference( idnull, ref236 );

    var idfilterFX = stringIDToTypeID( "filterFX" );

        var desc34237 = new ActionDescriptor();

        var idFltr = charIDToTypeID( "Fltr" );

            var desc34238 = new ActionDescriptor();

            var idRds = charIDToTypeID( "Rds " );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc34238.putUnitDouble( idRds, idPxl, radius );

        var idGsnB = charIDToTypeID( "GsnB" );

        desc34237.putObject( idFltr, idGsnB, desc34238 );

    var idfilterFX = stringIDToTypeID( "filterFX" );

    desc34236.putObject( idfilterFX, idfilterFX, desc34237 );

executeAction( idsetd, desc34236, DialogModes.NO );

};

After executing the script 2 smart layers with blur are formed. When you move the slider, the blur radius changes simultaneously in both layers.

Until then, everything works. But I would like to make it possible to set the value of the radius slider and this value was transferred to the external JSX.

Here is the part of JSX code where you need to insert a variable from JS:

//

// SmartFrequency3_BandGB.jsx

//

//

// Generated Thu Aug 02 2018 01:02:07 GMT+0300

//

cTID = function(s) { return app.charIDToTypeID(s); };

sTID = function(s) { return app.stringIDToTypeID(s); };

//

//==================== Smart Frequency 3-Band GB ==============

//

function SmartFrequency3_BandGB() {

  function step1(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putProperty(cTID('Prpr'), cTID('CchP'));

    ref1.putEnumerated(cTID('capp'), cTID('Ordn'), cTID('Trgt'));

    desc1.putReference(cTID('null'), ref1);

    var desc2 = new ActionDescriptor();

    desc2.putInteger(cTID('HsSt'), 1);

    desc1.putObject(cTID('T   '), cTID('CchP'), desc2);

    executeAction(sTID('set'), desc1, dialogMode);

  };

// code

   function step21(enabled, withDialog) {

    if (enabled != undefined && !enabled)

      return;

    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);

    var desc1 = new ActionDescriptor();

    desc1.putUnitDouble(cTID('Rds '), cTID('#Pxl'), radius);

    executeAction(sTID('gaussianBlur'), desc1, dialogMode);

  };

  step1();

// code

  step21();

};

//=========================================

//                    SmartFrequency3_BandGB.main

//=========================================

//

SmartFrequency3_BandGB.main = function () {

  SmartFrequency3_BandGB();

};

SmartFrequency3_BandGB.main();

// EOF

"SmartFrequency3_BandGB.jsx"

// EOF

How to pass "radius" variable from JS to JSX?

This topic has been closed for replies.
Correct answer r-bin

Thank you very much for your help. But I guess I'm too stupid. When I included the presented code in my host script, all other buttons in the panel stopped working.


Я тоже тупой. В расширениях вообще не шарю)).

Но мне попалось одно демо-расширение.

Там был такой код на кнопку. в js-файле.

$("#rgb_c").button().click(function(a)  { csInterface.evalScript('$._ext.evalFile("'+extensionRoot+'tools/RGB.jsx")')});

Я изменил его на вот такой.

$("#rgb_c").button().click(function(a)  { csInterface.evalScript('var xxx= 1; $._ext.evalFile("'+extensionRoot+'tools/RGB.jsx")')});

В файле RGB.jsx воткнул такой код

alert(xxx)

Запустил фотошоп, нажал кнопку, получил правильный алерт ))

Может это как раз то, что надо?

4 replies

Ten A
Community Expert
Community Expert
August 16, 2018

This thread moved from Photoshop Scripting to Extensions / Add-ons Development.

Silly-V
Legend
August 11, 2018

You must have a function in JSX which accepts an argument.

JS:

$("#TEST").click(function(){

    var str = "Hello World";

    var myString = "runMyScript('" + str + "')";

    csInterface.evalScript(myString);

});

JSX:

function runMyScript(message){

    alert(message);

};

владимирк76249718
Known Participant
August 12, 2018

Yes. In the example above, I did just that. And it works. The question was a bit different. How to pass a variable to any other path, not to the JSX path that is written in the manifest file.

I have a lot of JSX in the extension. Here is an example of how they are called from JS:

$("#TEST").button().click(function(a) {

    csInterface.evalScript('$._ext.evalFile("' + extensionRoot + 'jsx/test.jsx")')

});

Due to poor knowledge of scripts I can not understand how to pass a variable in such JSX

Silly-V
Legend
August 12, 2018

Oh, I see. What if you try this:

Just have your main JSX function that you pass in data into from the JS.

Except, this time, pass in your jsx path to the final jsx script you wish to run as well as the variables you with to send.

In fact, use not the evalFile but use the evalScript.

Then in your JSX that is described in the manifest that is being called, you have one 'distribution' function which reads in the target jsx path via eval and uses other pieces of your data to do things like choose which function to run and the arguments to that function.

Legend
August 11, 2018

Do I understand it correctly that the .js file is web browser JavaScript?

If so, I don't see why it should not be possible to pass the radius variable to the .jsx (which is Photoshop JavaScript) as an argument when you call the .jsx file.

владимирк76249718
Known Participant
August 10, 2018

Is that impossible? Or is my English so bad?

Legend
August 11, 2018

I do not use and do not understand anything in extensions. But try to use the search.
For example, maybe this will help

Re: How to pass information between JS Extension and JSX host