Interact with another process

Does anyone know how can I make my program send/read text from another program? Let's say that my active window is Notepad. I would like to be able to insert text and also read what has been typed (i.e. it would copy & paste).

Any macro program can do that, but I want to treat the data with Java.

Thanks.

[330 byte] By [Achy94a] at [2007-11-27 2:10:41]
# 1
You cannot generally read data or receive GUI events on other applications. Allowing a program to do this is a serious security problem
tjacobs01a at 2007-7-12 2:03:06 > top of Java-index,Java Essentials,Java Programming...
# 2

If what you want is just a copy&paste tool wich can interact with other programs, you can try the api clipboard from awt:

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.ClipboardOwner;

import java.awt.datatransfer.Transferable;

import java.awt.datatransfer.StringSelection;

import java.awt.datatransfer.DataFlavor;

import java.awt.datatransfer.UnsupportedFlavorException;

import java.awt.Toolkit;

it can be useful:

// clipboard

public void getClipboardContents() throws Exception

{

BufferedReader in;

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

Transferable contents = clipboard.getContents(null);

boolean hasTransferableText =(contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor);

if ( hasTransferableText )

{

in = new BufferedReader (new StringReader((String)contents.getTransferData(DataFlavor.stringFlavor)));

String linea = in.readLine();

while (linea!=null)

{

// ...

linea = in.readLine();

}

in.close();

}

}

public void setClipboardContents( String aString )

{

StringSelection stringSelection = new StringSelection( aString );

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

clipboard.setContents( stringSelection, this );

}

the clipboard can be fullfilled from any app and read from java because it's a sysyem pool, but you cannot receive events from windows apps like notepad, you can copy notepad's text and paste it to your java app.

LoAlexa at 2007-7-12 2:03:06 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks LoAlex, this is exactly what I wanted.
Achy94a at 2007-7-12 2:03:06 > top of Java-index,Java Essentials,Java Programming...