This discussion is archived
0 Replies Latest reply: Apr 20, 2012 2:22 AM by 932043 RSS

System clipboard on Mac OS X

932043 Newbie
Currently Being Moderated

I'm having troubles getting data from the clipboard on Mac OS X.
What I want to achieve: printing new data that was entered into the clipboard. [if it is text based]
The problem: I'm tracking changes in system clipboard by tracking clipboard ownership changes, my idea works fine on both Windows and Linux [openSuse] however on Mac OS X my app can't print the content of the clipboard until it gets focus.
import java.awt.Toolkit;  
import java.awt.datatransfer.*;  
public class ClipboardListener extends Thread implements ClipboardOwner {
    Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();  
    public void run(){  
        Transferable selection = systemClipboard.getContents(this);  
        while (true) {}
    public void gainOwnership(Transferable t){ 
         try {this.sleep(100);} 
         catch (InterruptedException e) {e.printStackTrace();}
        systemClipboard.setContents(t, this);  
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
         String s;
        try {System.out.println((String) clipboard.getData(DataFlavor.stringFlavor));} 
        catch (UnsupportedFlavorException e) {} 
        catch (IOException e) {}
public class myApp {

     public static void main(String[] args){
          ClipboardListener listener = new ClipboardListener();
What I'm missing/doing wrong?

Edited by: 929040 on Apr 20, 2012 2:21 AM


  • Correct Answers - 10 points
  • Helpful Answers - 5 points