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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Michael P Ellis
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: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

Stein Rørvik
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
Reply | Threaded
Open this post in threaded view
|

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

Michael P Ellis
Hi and thanks for your reply.

The problem with his approach for my use case is that I wish to open images in ImageJ from another Java application. I could use a java.lang.Runtime.exec(String command) but this would be messy necessitating different OS-specific command strings to constructed for each platform.

Since ImageJ already can interpret various scripting languages (its own macro scripts and also javascript), it seems a shame that there is not just a socket one can send a piece of javascript or macro scripting language to drive ImageJ remotely.

A nice benefit is that an ImageJ could take a command-line argument to determine whether the sockets accepted requests for local and or remote access thus making it possible to drive ImageJ remotely from a different computer.

I might consider implementing this as a plugin if this functionality does not already exist. I have used this approach elsewhere. I just dod not want to start reinventing the wheel if ImageJ already had this.

-- Michael Ellis




October 23, 2020 1:30 PM, "Stein Rørvik" <[hidden email]> wrote:

> 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://imagej.nih.gov/ij/list.html&data=04|01|st
> [hidden email]|cd3da405f538449585dd08d8742bec55|e1f00f39604145b0b309e0210d8b32af|1|0|6373870792
> 4701433|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|1000&sd
> ta=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl+oiiUJk4=&reserved=0
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

Curtis Rueden-2
Hi Michael,

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

You could take a look at the ImageJ Server:
https://github.com/imagej/imagej-server

It's a REST API.

Regards,
Curtis

--
Curtis Rueden
Software architect, LOCI/Eliceiri lab - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
Have you tried the Image.sc Forum? https://forum.image.sc/

On Fri, Oct 23, 2020 at 6:21 AM Michael Ellis <[hidden email]>
wrote:

> Hi and thanks for your reply.
>
> The problem with his approach for my use case is that I wish to open
> images in ImageJ from another Java application. I could use a
> java.lang.Runtime.exec(String command) but this would be messy
> necessitating different OS-specific command strings to constructed for each
> platform.
>
> Since ImageJ already can interpret various scripting languages (its own
> macro scripts and also javascript), it seems a shame that there is not just
> a socket one can send a piece of javascript or macro scripting language to
> drive ImageJ remotely.
>
> A nice benefit is that an ImageJ could take a command-line argument to
> determine whether the sockets accepted requests for local and or remote
> access thus making it possible to drive ImageJ remotely from a different
> computer.
>
> I might consider implementing this as a plugin if this functionality does
> not already exist. I have used this approach elsewhere. I just dod not want
> to start reinventing the wheel if ImageJ already had this.
>
> -- Michael Ellis
>
>
>
>
> October 23, 2020 1:30 PM, "Stein Rørvik" <[hidden email]> wrote:
>
> > 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://imagej.nih.gov/ij/list.html&data=04|01|st
> > [hidden email]
> |cd3da405f538449585dd08d8742bec55|e1f00f39604145b0b309e0210d8b32af|1|0|6373870792
> >
> 4701433|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|1000&sd
> > ta=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl+oiiUJk4=&reserved=0
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

Albert Cardona-2
Hi Michael,

There is an example of using sockets in TrakEM2:

https://github.com/trakem2/TrakEM2/blob/master/src/main/java/ini/trakem2/utils/RedPhone.java

When the UI freezes up, the automatically launched red phone enables the user via sockets to e.g. trigger a save command, or flush the cache, etc.

It’s quite easy to setup.

Hope this helps.

Albert

> On Oct 23, 2020, at 7:23 PM, Curtis Rueden <[hidden email]> wrote:
>
> Hi Michael,
>
>> Is it possible to send a command to a running copy of ImageJ via a socket?
>
> You could take a look at the ImageJ Server:
> https://github.com/imagej/imagej-server
>
> It's a REST API.
>
> Regards,
> Curtis
>
> --
> Curtis Rueden
> Software architect, LOCI/Eliceiri lab - https://loci.wisc.edu/software
> ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
> Have you tried the Image.sc Forum? https://forum.image.sc/
>
>> On Fri, Oct 23, 2020 at 6:21 AM Michael Ellis <[hidden email]>
>> wrote:
>>
>> Hi and thanks for your reply.
>>
>> The problem with his approach for my use case is that I wish to open
>> images in ImageJ from another Java application. I could use a
>> java.lang.Runtime.exec(String command) but this would be messy
>> necessitating different OS-specific command strings to constructed for each
>> platform.
>>
>> Since ImageJ already can interpret various scripting languages (its own
>> macro scripts and also javascript), it seems a shame that there is not just
>> a socket one can send a piece of javascript or macro scripting language to
>> drive ImageJ remotely.
>>
>> A nice benefit is that an ImageJ could take a command-line argument to
>> determine whether the sockets accepted requests for local and or remote
>> access thus making it possible to drive ImageJ remotely from a different
>> computer.
>>
>> I might consider implementing this as a plugin if this functionality does
>> not already exist. I have used this approach elsewhere. I just dod not want
>> to start reinventing the wheel if ImageJ already had this.
>>
>> -- Michael Ellis
>>
>>
>>
>>
>> October 23, 2020 1:30 PM, "Stein Rørvik" <[hidden email]> wrote:
>>
>>> 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://imagej.nih.gov/ij/list.html&data=04|01|st
>>> [hidden email]
>> |cd3da405f538449585dd08d8742bec55|e1f00f39604145b0b309e0210d8b32af|1|0|6373870792
>>>
>> 4701433|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|1000&sd
>>> ta=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl+oiiUJk4=&reserved=0
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

Stein Rørvik
In reply to this post by Michael P Ellis
I am not sure of this is helpful as I am using Windows and am pretty clueless about how Java works,

but I tried to see if my proposed method of using the -port option to pass different commands to multiple ImageJ instances works across multiple Java runtimes with different versions and yes it does:

CD /D "%~dp0"
SET ImageJ_default=ImageJ.exe
SET ImageJ_Java8=jre.18\bin\java -jar ij.jar -Xmx1024m
SET ImageJ_Java6=jre.16\bin\java -jar ij.jar -Xmx1024m
start %ImageJ_default% -eval "eval('script', 'IJ.getInstance().setTitle(\"ImageJ default\")');"
start %ImageJ_Java8% -port1 -eval "eval('script', 'IJ.getInstance().setTitle(\"ImageJ at port 1\")');"
start %ImageJ_Java6% -port2 -eval "eval('script', 'IJ.getInstance().setTitle(\"ImageJ at port 2\")');"
timeout 10
start %ImageJ_default% -eval "print('hello from ImageJ at the default port')"
start %ImageJ_Java8% -port1 -eval "print('hello from ImageJ at port 1')"
start %ImageJ_Java6% -port2 -eval "print('hello from ImageJ at port 2')"
timeout 3
start %ImageJ_default% -eval "print('another hello from ImageJ the default port')"
start %ImageJ_Java6% -port1 -eval "print('another hello from ImageJ at port 1')"
start %ImageJ_Java8% -port2 -eval "print('another hello from ImageJ at port 2')"

Here I am using three different Java runtimes; default is the default exe launcher pointing to the new Java 8 distribution; port 1 is at the old ImageJ included Java 8 distribution and port 2 is at the very old ImageJ included Java 6 distribution. In the three last lines I switch to Java 6 when listening at port1 and Java 8 in port2 and it still works; the output is as specified. I get an error message though with eval('script'... in the Java 6 instance warning that the script engine requires Java 8, so the window is not renamed.

Not what you asked for; but I think this is still interesting as I did not expect it to work.

Stein

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

Hi and thanks for your reply.

The problem with his approach for my use case is that I wish to open images in ImageJ from another Java application. I could use a java.lang.Runtime.exec(String command) but this would be messy necessitating different OS-specific command strings to constructed for each platform.

Since ImageJ already can interpret various scripting languages (its own macro scripts and also javascript), it seems a shame that there is not just a socket one can send a piece of javascript or macro scripting language to drive ImageJ remotely.

A nice benefit is that an ImageJ could take a command-line argument to determine whether the sockets accepted requests for local and or remote access thus making it possible to drive ImageJ remotely from a different computer.

I might consider implementing this as a plugin if this functionality does not already exist. I have used this approach elsewhere. I just dod not want to start reinventing the wheel if ImageJ already had this.

-- Michael Ellis




October 23, 2020 1:30 PM, "Stein Rørvik" <[hidden email]> wrote:

> 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://imagej.nih.
> gov/ij/list.html&data=04%7C01%7Cst
> [hidden email]|cd3da405f538449585dd08d8742bec55|e1f00f39604145b0b
> 309e0210d8b32af|1|0|6373870792
> 4701433|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTi
> 4701433|Unknown|I6Ik1haWwiLCJXVCI6Mn0=|1000&sd
> ta=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl+oiiUJk4=&reserved=0
>
> --
> ImageJ mailing list:
> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimage
> j.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no
> %7C1124b6f39ee343ad0e3c08d87745dfa8%7Ce1f00f39604145b0b309e0210d8b32af
> %7C1%7C0%7C637390489283449932%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=i6
> HQFSO5qRc%2BMbRzrdyI5nxM9cPxo8SxMkQyrdovNtM%3D&amp;reserved=0

--
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%7C1124b6f39ee343ad0e3c08d87745dfa8%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C0%7C637390489283459889%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=ER2huY048%2Bkuej0jjA%2FeX6q8fig0F0ZHt74VjzPJUbw%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

Michael P Ellis
In reply to this post by Albert Cardona-2
Many thanks to Stein Rørvik, Curtis Rueden, Albert Cardona fo your replies to my question!

Regards -- Michael Ellis


October 24, 2020 12:06 PM, "Albert Cardona" <[hidden email]> wrote:

> Hi Michael,
>
> There is an example of using sockets in TrakEM2:
>
> https://github.com/trakem2/TrakEM2/blob/master/src/main/java/ini/trakem2/utils/RedPhone.java
>
> When the UI freezes up, the automatically launched red phone enables the user via sockets to e.g.
> trigger a save command, or flush the cache, etc.
>
> It’s quite easy to setup.
>
> Hope this helps.
>
> Albert
>
>> On Oct 23, 2020, at 7:23 PM, Curtis Rueden <[hidden email]> wrote:
>>
>> Hi Michael,
>>
>>> Is it possible to send a command to a running copy of ImageJ via a socket?
>>
>> You could take a look at the ImageJ Server:
>> https://github.com/imagej/imagej-server
>>
>> It's a REST API.
>>
>> Regards,
>> Curtis
>>
>> --
>> Curtis Rueden
>> Software architect, LOCI/Eliceiri lab - https://loci.wisc.edu/software
>> ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
>> Have you tried the Image.sc Forum? https://forum.image.sc
>>
>>> On Fri, Oct 23, 2020 at 6:21 AM Michael Ellis <[hidden email]>
>>> wrote:
>>>
>>> Hi and thanks for your reply.
>>>
>>> The problem with his approach for my use case is that I wish to open
>>> images in ImageJ from another Java application. I could use a
>>> java.lang.Runtime.exec(String command) but this would be messy
>>> necessitating different OS-specific command strings to constructed for each
>>> platform.
>>>
>>> Since ImageJ already can interpret various scripting languages (its own
>>> macro scripts and also javascript), it seems a shame that there is not just
>>> a socket one can send a piece of javascript or macro scripting language to
>>> drive ImageJ remotely.
>>>
>>> A nice benefit is that an ImageJ could take a command-line argument to
>>> determine whether the sockets accepted requests for local and or remote
>>> access thus making it possible to drive ImageJ remotely from a different
>>> computer.
>>>
>>> I might consider implementing this as a plugin if this functionality does
>>> not already exist. I have used this approach elsewhere. I just dod not want
>>> to start reinventing the wheel if ImageJ already had this.
>>>
>>> -- Michael Ellis
>>>
>>> October 23, 2020 1:30 PM, "Stein Rørvik" <[hidden email]> wrote:
>>
>> 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://imagej.nih.gov/ij/list.html&data=04|01|st
>>
>> [hidden email]
>>> |cd3da405f538449585dd08d8742bec55|e1f00f39604145b0b309e0210d8b32af|1|0|6373870792
>>
>>> 4701433|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|1000&sd
>>
>> ta=gQxlzYvUrFDJMgQqUKXnLjR1woFAOmpdxjl+oiiUJk4=&reserved=0
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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