Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

evalScript not working with parameters

Engaged ,
Dec 27, 2019 Dec 27, 2019

Currently in js with jsx I'm doing the following:

 

      const exc = `setupPlateTemplate(${orders})`;
      csiRun.evalScript(exc);

 

Where orders is an array of data.  It doesn't work.

For kicks I tried this: 

 

      let testMe = 'something'
      const exc = `setupPlateTemplate(${testMe})`;
      csiRun.evalScript(exc);

 

Does not work.

This does work: 

 

      const exc = `setupPlateTemplate()`;
      csiRun.evalScript(exc);

 

 How can I pass parameters?

TOPICS
Scripting
3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Dec 30, 2019 Dec 30, 2019

Hi, you need to wrap the parameters in a quote:

 

cs.evalScript(`someFunction('${param}')`)

 

Notice the quotes inside the round brackets. If you need parameters, best to JSON.stringify them before passing (evalScript can only handle strings) then parse them once in JSX.

 

Example here, notice evalScript is an imported (promised wrapper) function from a small util library I call cluecumber, here.

 

Make sure to eval/run a JSON lib in JSX prior to calling JSON.parse in your JSX file though.

Translate
Adobe
LEGEND ,
Dec 28, 2019 Dec 28, 2019

Never used JS in Illustrator, but based on other experience: why do you evaluate twice (once with `` quotes, once with evalscript)? Just use a a simple string and see if it works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 28, 2019 Dec 28, 2019

Thanks for your response.  Because orders in the first is an array of data.  So it's not just a string I'm trying to pass.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 28, 2019 Dec 28, 2019

So, explain why you use the back quotes? Bear in mind the results of any backquoted operation is a string, which you specifically said you want to avoid. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 28, 2019 Dec 28, 2019

Those aren't backslashes.  It's JavaScript es6 they're used so I can allow template literals.  {$somevariable} in a js string

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 28, 2019 Dec 28, 2019

I didn't say backslash. I said backquote, which I know are used to create template literals. Now, I've never used backquotes in JavaScript but it seems to me that the result of a template literal is a string, so of course it will fail.

 

And consider this example:

let testMe = 'something'
      const exc = `setupPlateTemplate(${testMe})`;
      csiRun.evalScript(exc);

so... would this script work:

setupPlateTemplate(something)
rather than
setupPlateTemplate('something')
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 28, 2019 Dec 28, 2019

In js it would be 

let myArray = ["hi", "sun", "blah"]
let putVar = 'hi joe, ${myArray}'

 

then if we console logged that it be the array.  So that's what I'm trying to do here in js to pass the array, in the original example orders is an array of objects.  

tryinf to get my data to go to the function in jsx.  Sorry if I'm making it more confusing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 28, 2019 Dec 28, 2019

edit: someone else answered

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 28, 2019 Dec 28, 2019

Please explain what you mean when you say testMe is not defined.  It's in the code let testMe = "something"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 28, 2019 Dec 28, 2019

edit: someone else answered

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 28, 2019 Dec 28, 2019

Generally speaking how would you pass a js object from js to jsx?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 28, 2019 Dec 28, 2019

edit: someone else answered

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 28, 2019 Dec 28, 2019

This ${} is es6 js template literal

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Dec 30, 2019 Dec 30, 2019
LATEST

Hi, you need to wrap the parameters in a quote:

 

cs.evalScript(`someFunction('${param}')`)

 

Notice the quotes inside the round brackets. If you need parameters, best to JSON.stringify them before passing (evalScript can only handle strings) then parse them once in JSX.

 

Example here, notice evalScript is an imported (promised wrapper) function from a small util library I call cluecumber, here.

 

Make sure to eval/run a JSON lib in JSX prior to calling JSON.parse in your JSX file though.

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