Pasing a parameter to photoshop.core.executeAsModal () within a UI component

Hello everyone and thank you for your support.

I’m creating a component that makes a button. The component takes as a parameter the function to be executed when the button is pressed and creates a sp-action-button that performs that action when pressed.

in this example the function dBCurve() is received by the component as paramter “FunctionToBeExecuted” and should be launched, from inside the component, via photoshop.core.executeAsModal () to which in turn is passed as a parameter . The name of the function that should be executed is dBCurve () which the ActionButton component handles as the FunctionToBeExecuted parameter passing it again as a parameter to require(“photoshop”).core.executeAsModal(()

Unfortunately, however, the dBCurve () function is not performed whan passed as a parameter to photoshop.core.executeAsModal () . The ActionButton component works correctly (verified via UI) and correctly receives the dBCurve () parameter as the name of the function to be executed (verified via log) but the function correctly passed as a parameter to the component and passed by it as a parameter to photoshop.core.executeAsModal () is not executed

If, on the other hand, photoshop.core.executeAsModal () is executed in the same component by directly inserting the name of the function to execute, that is to say, dBCurve (), instead of the FunctionToBeExecuted parameter which represents dBCurve (), the dBCurve () function is executed correctly when the button is pressed.

This is the code. Do you have any suggestion ?

 import { dBCurve } from "../../../utilities/Photoshop/Trasversali/DodgeBurn"

  const ActionButton = ( {FunctionToBeExecuted, children} ) => {
  console.log("ActionButton executeAsmodal should execute parameter..." + FunctionToBeExecuted ) // log confirm parameter is OK !!
  const clickHandler = async () => {
    await require("photoshop").core.executeAsModal(() => FunctionToBeExecuted )  // DOESN'T WORKS
    await require("photoshop").core.executeAsModal(() => dBCurve()  )  // WORKS
  }

return ( 
    <sp-action-button onClick={clickHandler}>
      { children }
    </sp-action-button>
   ); //  
  }
 
export default ActionButton;

This is the piece of code that activates the ActionButton component by passing it the name of the function to execute, i.e. dBcurve ()

<ActionButton FunctionToBeExecuted="dBCurve()" >Test myActionButton</ActionButton>

Thanks in advance for your kind reply

I see two confusing parts here

First:

await require("photoshop").core.executeAsModal(() => FunctionToBeExecuted )  // DOESN'T WORKS

You’re not executing FunctionToBeExecuted (missing ()) the function. I believe it should be either

executeAsModal(FunctionToBeExecuted)

or

executeAsModal(() => FunctionToBeExecuted())

Second one:

<ActionButton FunctionToBeExecuted="dBCurve()" >Test myActionButton</ActionButton>

You’re passing not the dBCurve as a function, but the result of the dBCurve(). I think you want to pass just a function - dBCurve instead of dBCurve()

thanks @Karmalakas,

I have tried all your suggestions, in all possible combinations but it still doesn’t work: nothing happen, there is no message in the console and the program flow continue executing the following instructions (the instructions following the executeModal instruction, that has no effect)

I have tried both passing dBCurve() or just dBCurve in both the executeAsModal ways that you suggested (that I would consider correct) but in all cases nothing happen

Looking at your code, there should only be one message in console - during rendering of the ActionButton. So maybe the problem is in dBCurve function?

thanks @Karmalakas

I’m pretty sure that there is no problem in dBcurve() because it is properly executed, as I wrote in the first post, when directly launched via await require(“photoshop”).core.executeAsModal(() => dBCurve() within the same component for testing purposes. Layers are created as supposed and console confirm execution through the message that I’m sending to the console for the purpose. The problem arise when dBcurve() - with or without brackets - is passed as a FunctionToBeExecuted paramenter to executeAsModal (whatever of the two differnt syntax that you kindly suggested)

I confirm that there is only one message in the console, related to the rendering of ActionButton

Not sure why it doesn’t work…
Could you try one more time?

<ActionButton FunctionToBeExecuted="dBCurve" >Test myActionButton</ActionButton>

And in the component:

await require("photoshop").core.executeAsModal(FunctionToBeExecuted) 

Notice no brackets in both lines for passed functions
If it doesn’t work, would need to know at least the return value type of dBCurve() :thinking:

In both these cases only a string is being passed, isn’t it? Not sure how this should execute the function then. I’d write it in curly brackets to pass the variable reference (pointing to the function), instead of a string.

<ActionButton FunctionToBeExecuted={dBCurve} >...</ActionButton>

:man_facepalming: Yes, that’s exactly what I wanted to suggest

@Trasatti Btw, if something isn’t working as expected and nothing is logged by default, just log the batchPlay result (and maybe also put it in a try/catch). You would’ve immediately found the issue then:

console.log(await require("photoshop").core.executeAsModal("string"))

image

(or - and I can’t say this enough - just use TypeScript, which wouldn’t let you do such mistakes in the first place :smiley:)

1 Like

Thanks @simonhenke and @Karmalakas it works ! :boom: :pray: :pray: :pray:

Here are the working instructions:

<ActionButton FunctionToBeExecuted = {dBCurve} >Test myActionButton</ActionButton>

AND

await require("photoshop").core.executeAsModal(FunctionToBeExecuted) ```

@simonhenke , @Karmalakas

I’ ve now modified the function dbCurve() to accept a parameter representing the blend mode of the layer to be created (tested; it works properly when called from outside the component)

If I’m not going too far with asking… I would be happy to know how I should modify the two working instructions above so that the instruction

<ActionButton FunctionToBeExecuted = {dBCurve} >Test myActionButton</ActionButton>

… passes parameters to the funtion ‘FunctionToBeExecuted’ (namely dBCurve) contained in

await require("photoshop").core.executeAsModal(FunctionToBeExecuted) 

That is to say, not only the pointer to dBCurve should be passed but also the execution parameters.

And executeAsModal should be ableto receive the paramenters and apply them to the execution of FunctionToBeExecuted (namely dBCurve)

Thanks

Do you want to define the parameters inside or outside of the ActionButton component?

Outside:
<ActionButton FunctionToBeExecuted = {() => dBCurve(some, param)} />

await require("photoshop").core.executeAsModal(FunctionToBeExecuted)

Inside:
<ActionButton FunctionToBeExecuted = {dBCurve} />

await require("photoshop").core.executeAsModal(() => FunctionToBeExecuted(some, param))

1 Like

perfect, it works

I wanted to define the parameters outside the component

The more I begin to delve into the various themes, the more I discover how powerful this development environment is !

Thanks