Skip to main content
platm72
Inspiring
February 28, 2020
Answered

doScript() method questions....

  • February 28, 2020
  • 2 replies
  • 5307 views

Hello,

I'm tryingo to use a method app.doSript() to invoke my fuction main() consist of a few steps in ONE UNDO mode, in case to make it undone by one step, so I wrote such line of code: 

 

 

app.doScript(String(main()),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

 

I have two questions, I can't answer to myself:

1. script works fine, but there is no ONE undo in edit menu of indesign, why? what is done wrong?

2. why the main() function is invoked corectly and the function main() does it's work after stopping runtime, but an error occurs with blue comunicate if the line looks like this:  

 

 

app.doScript(main(),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

Runtime Error: Error Code# 30477: Invalid value for parameter 'script' of method 'doScript'. Expected File, String or JavaScript Function, but received nothing. @ file '~/Desktop/ZZZ_horizontalMirror.jsx' [line:42, col:NaN]
 
many thanks in advance for help with understanding the problem 🙂
regards 
m.

 

 

This topic has been closed for replies.
Correct answer Manan Joshi

Hi,

 

So as per the contract of the doScript method, the first argument can be a File, String or a JavascriptFunction. See the details at

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html#d1e42173__d1e45616

 

Now lets take your first use case

 

app.doScript(String(main()),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

In the above you have called the method main by using the statement main(), so it will be called and the return value of it would be passed to the String constructor and the resulting value is then used to call the doScript method. Seems your method does not return anything in which case it would be undefined and the above call would be equivalent to the following when the doScript method is called

 

app.doScript(String(undefined),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

 The String(undefined) would return a string undefined and thus no code was actually called by doScript and hence you don't see any undo entry in the Edit menu

 

Now in your second use case

 

app.doScript(main(),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

The main method is called and its return type is then used to call the doScript method. This is equivalent to the following when the doScript method is called

 

app.doScript(undefined,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

Thus the error that pops up makes sense.

 

Correct way of calling the doScript method. We generally just use the method name without the invocation braces. So the following should work good

 

app.doScript(main,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

If we need to pass the arguments, we can also use the string form as follows

 

app.doScript("main()",ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

 

Hope this clears out your confusions.

-Manan

2 replies

Community Expert
March 2, 2020

Hi,

 

There is not much explanation on the undo modes that i could find the last time i checked. Based on my experience the safest mode that i have found out has been the entireScript. Fast mode should give a speed boost in case of complex operations, but it requires some thought before being used, i never got into a situation to actually investigate using it personally. Marc has written a blog highlighting the use cases and his observations regarding the undo modes that you can look into and then start experimenting yourself

http://www.indiscripts.com/post/2011/08/notes-on-fastentirescript-undo-mode

 

-Manan

-Manan
platm72
platm72Author
Inspiring
March 3, 2020

Hi Manan, thank you again for pointing me on Marc's article. As always Mark dive deeply into a subject .... but this time his eplanations were understandable in full for me... 🙂 

Best regards 

m.

Manan JoshiCommunity ExpertCorrect answer
Community Expert
February 29, 2020

Hi,

 

So as per the contract of the doScript method, the first argument can be a File, String or a JavascriptFunction. See the details at

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html#d1e42173__d1e45616

 

Now lets take your first use case

 

app.doScript(String(main()),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

In the above you have called the method main by using the statement main(), so it will be called and the return value of it would be passed to the String constructor and the resulting value is then used to call the doScript method. Seems your method does not return anything in which case it would be undefined and the above call would be equivalent to the following when the doScript method is called

 

app.doScript(String(undefined),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

 The String(undefined) would return a string undefined and thus no code was actually called by doScript and hence you don't see any undo entry in the Edit menu

 

Now in your second use case

 

app.doScript(main(),ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

The main method is called and its return type is then used to call the doScript method. This is equivalent to the following when the doScript method is called

 

app.doScript(undefined,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

Thus the error that pops up makes sense.

 

Correct way of calling the doScript method. We generally just use the method name without the invocation braces. So the following should work good

 

app.doScript(main,ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

If we need to pass the arguments, we can also use the string form as follows

 

app.doScript("main()",ScriptLanguage.JAVASCRIPT,[],UndoModes.ENTIRE_SCRIPT,'main');

 

 

Hope this clears out your confusions.

-Manan

-Manan
platm72
platm72Author
Inspiring
March 1, 2020

Thank you Manan 🙂 

Your detailed explanation is very helpful. Thank you !

m.