Skip to main content
Lordrhavin
Inspiring
April 8, 2024
Question

doScript is async. How do I pass an object?

  • April 8, 2024
  • 2 replies
  • 586 views

I need to call a function in my script and afterwards undo, what it has done. I can do this with

app.doScript(fn)

But how do I pass an object to this asnc function? Is a skript-global array of those objects the only way?

Or is there a synchronous way (that would be preferred) to call a function and then undo, what is has done?

This topic has been closed for replies.

2 replies

Robert at ID-Tasker
Legend
April 8, 2024
quote

[...]
But how do I pass an object to this asnc function?

[...]


By @Lordrhavin

 

What kind of object?

 

m1b
Community Expert
Community Expert
April 8, 2024

Hi @Lordrhavin, I'm assuming you're using ExtendScript because you didn't tag UXP.

 

Application.doScript() is synchronous. You can see here:

 

function main() {
    $.sleep(500)
    alert('main')
};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

alert('end');

 

 

You can supposedly pass arguments to the function (that's the undefined parameter above), but it seems to expect an Array of Strings, which limits things a bit. However, I use doScript all the time for managing single Undos and it seems fairly unproblematic to handle scope. You can call the app.doScript from inside a function, after all.

 

Does that help? I'm really not super knowledgable on this, so I hope I haven't misunderstood your question.

- Mark

 

Edit to more directly answer your question:

> "But how do I pass an object to this asnc function? Is a skript-global array of those objects the only way?"

You can simply use doScript to execute a function that itself has the scope you need.

Stefan Rakete
Inspiring
April 8, 2024

Hi,
never used the argument for parameters in the doScript. You can do instead:

app.scriptArgs.setValue("key1", Value);
app.doScript(script);

and in "script" get the value 
app.scriptArgs.get("key1);

Hope this helps

m1b
Community Expert
Community Expert
April 9, 2024

Hi @Stefan Rakete, thanks, I didn't know about that. In the docs it says that `value` (in .setValue) is a String. Can it accept an Object? (If not, then it would be simpler to send the argument via the app.doScript call.)

- Mark