2

I have a java program, that runs in the background and monitors the system clipboard for changes (i do this through polling, as it seems to be the only way besides the "ownership-variant", where i have to reset the content all the time to become the owner). If it discovers an input text in an specific format, it processes that text and overwrites the clipboard with the result (so i can copy the input and right after it paste the result while the program is running in background).

This worked fine so far on Windows, but when running the same program on Mac OS X, the behavior is a little bit strange. As long as i don't copy my results into the system clipboard, the polling mechanism itself works as expected. But at the moment i set the clipboard content out of the java program the first time, it recognizes future extern changes only while becoming active. So i can't just let it run in the background, but instead i have to "copy input -> switch to java-program -> switch back -> paste result" all the time.

As that is annoying and thats exactly the thing i wanted to avoid by this "clipboard monitoring -> result pasting"-method, i would be very happy for any ideas how to fix that issue.

Edit: some code-fragements

public void setClipboardText(String text) {
  if (text == null) {
    throw new NullPointerException();
  }

  synchronized (this.lastFoundTextLock) {
    this.lastFoundText = text;

    Toolkit.getDefaultToolkit().getSystemClipboard()
        .setContents(new StringSelection(text), null);
  }
}

public String getClipboardText() {
  Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().
                       getContents(null);

  try {
    if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      String text = (String) t.getTransferData(DataFlavor.stringFlavor);
      return text;
    }
  } catch (UnsupportedFlavorException e) {
  } catch (IOException e) {
  }

  return null;
}

public void run() {
  while (true) {
    String currentClipboardText = getClipboardText();
    boolean isNew;

    synchronized (this.lastFoundTextLock) {
      isNew = ((this.lastFoundText != null) || (currentClipboardText != null))
            && ((currentClipboardText == null) || !currentClipboardText
                .equals(this.lastFoundText));

      if (isNew) {
        this.lastFoundText = currentClipboardText;
      }
    }

    if (isNew && currentClipboardText != null) {
      //new text found
      fireNewClipboardTextFound(currentClipboardText);
    }

    try {
      Thread.sleep(this.automaticCheckInterval);
    } catch (InterruptedException e) {
      // ignore
    }

    synchronized (this.monitorRunningLock) {
      if (!this.monitorRunning) {
        break;
      }
    }
  }
}
boldimor
  • 21
  • 3

1 Answers1

1

I see that several others have attempted what you're trying ( Can't copy to a clipboard from a background java application on MAC OSX ) and had marginal success ( Copying to Clipboard in Java ) and few good answers ( java/swing: clipboard paste ) but you might want to investigate further... Can anyone else comment on the changes in Java 6 wrt this issue?

Community
  • 1
  • 1
sventechie
  • 1,859
  • 1
  • 22
  • 51