executeAsModal with argument

Hello guys,

I am trying to pass array as argument to function which is executedAsModal.
Any idea why the code bellow doesn’t work?

let array = ["Entry0", "Entry1", "Entry3"]
await require("photoshop").core.executeAsModal(targetFunction, {"commandName": `Running function`, "descriptor": `${array}`});

I would suggest taking a closer look at the arguments for executeAsModal, as described in the docs. The descriptor is supposed to be an object containing your arguments. So you should probably try passing the array as a member of the descriptor. Also looking at your code it looks like you’re turning your array into a string of sorts. Have you tried something like this?:

let myArray = ["Entry0", "Entry1", "Entry3"];
let desc = {"myArg" : myArray};

Please take my advice with an abundance of caution because I am pretty amateur but @dotproduct provided the link to the docs, and if I am understanding correctly, see highlighted section of Docs ,
"The descriptor contains the values provided to the descriptor property in the options argument to executeAsModal."

|and they give the example code
async function targetFunction(executionContext, descriptor)

It also says the
descriptor (optional): An object with command arguments. See documentation for targetFunction below.

I did something different adjusting some generated code from alchemist, which worked and was:
async function runModalFunction(filePath) { await executeAsModal(() => actionCommands(filePath), {"commandName": "Action Commands"}); }

which I gather the local scope filePath is available to that fat arrow anonymous function so it could get passed to actionCommands, and I sort of think that is only necessary because its expecting a function object. I don’t know for sure but the docs say:
targetFunction: The JavaScript function to execute after Photoshop enters a modal state.

So idk if any of that is helpful, but provided for possibly informational purposes.

See this post:

Wasn’t able to make it work.
desc is not iterable.

Ended up building array directly in the function without passing it as argument.
Workaround but at least it works.

Well… You never showed how your targetFunction() looks like, so quite difficult to guess, because how you provide the argument seems correct, so most likely issue is with accepting that argument

Would be worth posting the code you tried out? I don’t think you actually got what I meant in my answer. You’re not supposed to pass the array as descriptor, but as a member of the descriptor, hence no need to iterate over the descriptor. In any case, glad you got it working in some way.

What about:

let array = ["Entry0", "Entry1", "Entry3"]
await require("photoshop").core.executeAsModal(targetFunction, {"commandName": `Running function`, "descriptor": array});

OK, this worked to pass an array via executeAsModal:

let array = ["red", "green", "blue"];
    console.log(array)  //  ["red", "green", "blue"];

await require("photoshop").core.executeAsModal(targetFunction, {"commandName": `Running function`, "descriptor": {"myArg": array} });

async function targetFunction(executionControl, string) {
     console.log(string.myArg);  //["red", "green", "blue"]
 }
3 Likes