Skip to main content
Participant
November 1, 2007
Answered

Passing arguments to the jsx file from command line

  • November 1, 2007
  • 9 replies
  • 35859 views
Thanks for taking my question.

I am using the following to be able to run my script from the command line.In case you were wondering on why i would be doing this- i would need to invoke the javascript from a server side code(like java,php etc. in my case it is java)

"c:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit\ExtendScript Toolkit.exe" "c:\path to script\myscript.jsx"

Anyways, i have been successful in running the myscript.jsx from the command line but i am not able to figure out how i could pass arguments to "myscript.jsx" from the command line, and be able to refer to these arguments within the script.

I have tried the following

"c:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit\ExtendScript Toolkit.exe" "c:\path to script\myscript.jsx" "argument1" "argument2"

and tried to refer these arguments within the script using arguments[0] and arguments[1] . But looks like this does not work.

Any thoughts?????
This topic has been closed for replies.
Correct answer _Allen_Jeng_

Not sure if people are still looking for ways to pass in arguments to javascript, the following solution works for Photoshop (you have to go through VB Script) Assuming that your Javascript lives in c:\myscript

runjavascript.vbs
-------------

Option Explicit
On Error Resume Next

Dim appRef
Dim javaScriptFile

Set appRef = CreateObject("Photoshop.Application")

appRef.BringToFront

javaScriptFile = "C:\myscript\test.jsx"

call appRef.DoJavaScriptFile(javaScriptFile, Array(Array("one","two"), Array("three")), 1)


------
DoJavaScriptFile takes 3 params, the first being the script, the 2nd is an array of params. In my example, you can have an array of arrays of params. When you receive params from javascript side, it will see param 0 as (one, two), param 1 is (three). For most people, simple case is fine: eg:

call appRef.DoJavaScriptFile(javaScriptFile, Array("one","two", "three"), 1)


The javascript would look like this:

test.jsx
---------

#target photoshop

if (arguments.length > 0)
alert("ARGUMENT 0 = " + arguments[0]);


----
from the jsx side, there's a magical variable called "arguments" of array type. When you pass variables in, the 1st one will be arguments[0], the next one would be arguments[1], and so forth.

To execute this from command line (assuming everything lives in c:\myscript):

cscript c:\myscript\runjavascript.vbs


Hope that helps!

-Allen

9 replies

Participant
July 29, 2019

I am using the following Command  to run my script from the command line.

"C:\Program Files (x86)\Adobe\Adobe ExtendScript Toolkit CC\ExtendScript Toolkit.exe" -run %JSX_OUT_FILE%

 

Now i can run my jsx file from the command line. But I am unable to find out how to use command line arguments to "export to binary" and "save" the jsx file.

 

If available please provide any documentation of the Adobe ExtendScript Toolkit regarding the command line arguments

Participant
January 21, 2009

Thanks for to all for the previous posts on getting command line arguments to jsx. I'm using CS3 and post #7 does not work. I generalized another post to have `generic` runjavascript.vbs.

Here's what I came up with.

command line example: runjavascript.vbs c:\mydirectory\test.jsx arg0 arg1 arg2 arg3

===== start runjavascript.vbs =====

 

Set vbsArguments = WScript.Arguments

If vbsArguments.Length = 0 Then
	WScript.Echo "Argument(0) is `your script to execute"
	WScript.Echo "Arguments(0+n) are passed to your script as argument(0) to argument(n-1)"
Else
	ReDim jsxArguments(vbsArguments.length-2)

	for i = 1 to vbsArguments.length - 1
		jsxArguments(i-1) = vbsArguments(i)
	Next

	Set photoshop = CreateObject("Photoshop.Application")
	photoshop.BringToFront

	'DoJavaScript has 3 parameters
	'syntax DoJavaScript(arg[0], arg[1], arg[2]]
	'arg[0] == javascript file to execute, full pathname
	'arg[1] == an array of arguments to past to the javascript
	'arg[2] == AiJavaScriptExecutionMode: aiNeverShowDebugger = 1, aiDebuggerOnError = 2, aiBeforeRunning = 3
	'only use 1
	Call photoshop.DoJavaScriptFile(vbsArguments(0), jsxArguments, 1)
End IF

 

===== start test.jsx =====

 

#target photoshop

for(n = 0; n < arguments.length; n++){
	alert("argument(" + n + ")= " + arguments);
}

 

Known Participant
July 13, 2019

Roy, I cannot thank you enough it was dogging me trying to figure out how to pass parameters to the JSX files.  I had some workarounds but I hated the idea of hitting the harddrive for them.  It is a slow workaround and not necessary.  Even using AHK to autoinput JS prompts was not what I was looking for, this should work great!  Thanks again

Participant
December 30, 2008
Is it by any chance also possible for the script to return a result to the command line?
e.g. return 1 or return 0?

If I put return codes outside functions in the script, indesign will complain about unexpected returns.
Participant
December 4, 2008

Hello guys! It is possible to get values from command line.

Let's see this example:

testclient -host localhost:6666 myscript.jsx "x=123456"


To access "x" from command line just type:

var my_x = app.scriptArgs.getValue("x");


Good Luck 🙂

---
http://www.kikuchi.adm.br

_Allen_Jeng_Correct answer
Participant
June 20, 2008

Not sure if people are still looking for ways to pass in arguments to javascript, the following solution works for Photoshop (you have to go through VB Script) Assuming that your Javascript lives in c:\myscript

runjavascript.vbs
-------------

Option Explicit
On Error Resume Next

Dim appRef
Dim javaScriptFile

Set appRef = CreateObject("Photoshop.Application")

appRef.BringToFront

javaScriptFile = "C:\myscript\test.jsx"

call appRef.DoJavaScriptFile(javaScriptFile, Array(Array("one","two"), Array("three")), 1)


------
DoJavaScriptFile takes 3 params, the first being the script, the 2nd is an array of params. In my example, you can have an array of arrays of params. When you receive params from javascript side, it will see param 0 as (one, two), param 1 is (three). For most people, simple case is fine: eg:

call appRef.DoJavaScriptFile(javaScriptFile, Array("one","two", "three"), 1)


The javascript would look like this:

test.jsx
---------

#target photoshop

if (arguments.length > 0)
alert("ARGUMENT 0 = " + arguments[0]);


----
from the jsx side, there's a magical variable called "arguments" of array type. When you pass variables in, the 1st one will be arguments[0], the next one would be arguments[1], and so forth.

To execute this from command line (assuming everything lives in c:\myscript):

cscript c:\myscript\runjavascript.vbs


Hope that helps!

-Allen

Participant
June 20, 2008
Hi,
i have connected to the windows machine from linux machine using ssh.I want to run one java script (*.js) program on this prompt.
But it is giving error to run the java script program.
Does any one know that how to run java script program on remote windows machine from linux machine using ssh.
Known Participant
June 20, 2008
_arti_tambe_ wrote:

Hi,
i have connected to the windows machine from linux machine using ssh.I want to run one java script (*.js) program on this prompt.
But it is giving error to run the java script program.
Does any one know that how to run java script program on remote windows machine from linux machine using ssh.

 


Specify the complete pathname to the photoshop executeable and the complete
pathname to the script as a parameter. If you need to pass arguments to the
script, write them to a text file that the script automatically reads when the
script starts up.

 


-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com

Participant
May 7, 2008

To run JavaScript from the prompt using ExtendScript Toolkit 1.0.3 or 2.0.2 you need to do the following:
(add the line #target indesign to the top of your script otherwise ESTK will open without executing the script. Example)

#target indesign

//MakeDocumentWithParameters.jsx
//An InDesign CS2 JavaScript
//Shows how to use the parameters of the document.open method.
//The first parameter (showingWindow) controls the visibility of the
//document. Hidden documents are not minimized, and will not appear until
//you add a new window to the document. The second parameter (documentPreset)
//specifies the document preset to use. The following line assumes that
//you have a document preset named "Flyer" on your system.
var myDocument = app.documents.add(true, app.documentPresets.item("Flyer"));

//SaveDocumentAs.jsx
//An InDesign CS2 JavaScript
//If the active document has not been saved (ever), save it.
if(app.activeDocument.saved == false){
	//If you do not provide a file name, InDesign will display the Save dialog box.
	app.activeDocument.save(new File("/c/temp/myTestDocument.indd"));
}


Ensure Indesign is open. Execute the following command:

"C:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit\ExtendScript Toolkit.exe" -run "[path to script]\script.jsx"

For example:

"C:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit\ExtendScript Toolkit.exe" -run "C:\Program Files\Adobe\Adobe InDesign CS2\Presets\Scripts\test.jsx"

This command can be easily called from Java or any other third party application of your choice.

It took me a while to find this information, so I thought I'd share it with everyone.

Good luck!

Participating Frequently
March 1, 2008

hi Tim Stoddard,
nice to meet you, I am sunil kumar. I am also using the follwing application that passing arguments to the jsx file from command line. Can you tell me how to do that? My javascript is given below. Please go through that, and give me the applicaiton.

//DataMerge.jsx
//An InDesign CS3 JavaScript
//
//Performs a data merge given an InDesign template
//file and a data file.

main();
function main()
{
	//You'll have to fill in valid files names on your system
	//for the data file and the InDesign template file.
	var myDocument = app.open(File("/d/DataMerge/DataMerge1.indd"));
	var myDataSource = File("/d/DataMerge/datasheet.csv");
	myDocument.dataMergeProperties.selectDataSource(myDataSource);
	myDocument.dataMergeProperties.mergeRecords();
	myDocument=myDocument.save("d:\kumar\datamerge3.indd");
	myDocument.close();
}



Can you tell me how to run javascript in command prompt? I tried ur way, but I am not able to run the script. Can you tell how to run the javascript from command prompt?

Known Participant
November 1, 2007
@_Tim_Stoddard_ wrote:

I have tried the following

"c:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit\ExtendScript Toolkit.exe" "c:\path to script\myscript.jsx" "argument1" "argument2"

and tried to refer these arguments within the script using arguments[0] and arguments[1] . But looks like this does not work.



That doesn't work. In your server-side script, write the arguments to
predetermined text file (e.g. "~/myscript.ini"), the read from that file in your
jsx script. Your choice as to whether you use ini, xml, or some other format.
I typically use ini format. It's easy enough to read and generate.


-X
--
for photoshop scripting solutions of all sorts
contact: xbytor@gmail.com