Login  Register

Re: Is it possible to send a command to a running copy of ImageJ via a socket?

Posted by Stein Rørvik on Oct 22, 2020; 10:50pm
URL: http://imagej.273.s1.nabble.com/Is-it-possible-to-send-a-command-to-a-running-copy-of-ImageJ-via-a-socket-tp5024089p5024106.html

Can't you just open ImageJ via the OS, with the multiple instance listener active?
Then subsequent commands will use the same instance.
You can use of the -port option to pass the command to one of several instances.

The following works as expected in a Windows command batch file:

start ImageJ.exe -eval "eval('script', 'IJ.getInstance().setTitle(\"ImageJ default\")');"
start ImageJ.exe -port1 -eval "eval('script', 'IJ.getInstance().setTitle(\"ImageJ at port 1\")');"
start ImageJ.exe -port2 -eval "eval('script', 'IJ.getInstance().setTitle(\"ImageJ at port 2\")');"
timeout 10
start ImageJ.exe -eval "print('hello from ImageJ at the default port')"
start ImageJ.exe -port1 -eval "print('hello from ImageJ at port 1')"
start ImageJ.exe -port2 -eval "print('hello from ImageJ at port 2')"
timeout 3
start ImageJ.exe -eval "print('another hello from ImageJ the default port')"
start ImageJ.exe -port1 -eval "print('another hello from ImageJ at port 1')"
start ImageJ.exe -port2 -eval "print('another hello from ImageJ at port 2')"

Here we have three instances running; I start each instance with a small JavaScript to rename the main window in order to easier tell the difference between them. Then we can pass commands, specifying which instance we want each of the commands to execute in. The "timeout 10" command inserts a 10 seconds delay to give sufficient time for all three instances of ImageJ to launch, before we start passing commands to each of them. Opening an image or running a macro in each instance will work the same way.

I use the "start" command to launch ImageJ asynchronously, or else the Windows script would wait until each instance of ImageJ has quit. Since this runs asynchronously, we need to add an appropriate delay between subsequent commands to the same instance, to allow whatever command you passed to it to finish; in this case 3 seconds.

Stein

-----Original Message-----
From: ImageJ Interest Group <[hidden email]> On Behalf Of Michael Ellis
Sent: 19. oktober 2020 14:37
To: [hidden email]
Subject: Is it possible to send a command to a running copy of ImageJ via a socket?

Hello all.

Is it possible to send a command to a running copy of ImageJ via a socket?

The sort of thing I had in mind could be illustrated with the unix 'nc' command as follows:

 $ echo 'open /path/to/file/Meta1.tif' | nc localhost 57294

I can see that ImageJ can use Socket ports to prevent multiple instances of ImageJ from running but I cannot see how to explicitly send a command via a Socket.

My final use case is from my own standalone java application to be able to request an already running copy of ImageJ open an image file.

If this is not possible is there already an ImageJ plugin that already written that does the job?

As ever thanks in advance for any help.
        Michael Ellis

        Digital Scientific UK Ltd

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no%7Ccd3da405f538449585dd08d8742bec55%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C0%7C637387079284701433%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl%2BoiiUJk4%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html