Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipboard monitoring on Mac OS X | Java

I'm having troubles getting data from the system clipboard on Mac OS X. What I'm trying to do is to listen to the system clipboard and print the content of the clipboard each time new [text based] information is put into it.

The problem: bellow code works perfectly fine on Windows 7 and openSUSE Linux machines, however when I try running the same code on Mac OS X the program fails to print the new content of the clipboard until focus is given to the application. [Nothing is printed until I click on the application icon on the dock...]

My source code:

import java.awt.Toolkit;  
import java.awt.datatransfer.*;  
import java.io.IOException;  

public class ClipboardListener extends Thread implements ClipboardOwner {

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

    public void run(){  
        Transferable selection = systemClipboard.getContents(this);  
        gainOwnership(selection);  
    }  

    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) {
        try {System.out.println((String) clipboard.getData(DataFlavor.stringFlavor));} 
        catch (UnsupportedFlavorException e) {} 
        catch (IOException e) {}
        gainOwnership(contents);  
    }  
}

public class myApp {

    public static void main(String[] args){
        ClipboardListener listener = new ClipboardListener();
        listener.start();
        while(true){}}

}

What I'm missing/doing wrong?

[Update] I found similar problem posted here: Java thread goes to sleep when not in focus on OSX However using the command "java -jar myApp.jar &" didn't work as a workaround for me.

like image 870
Naftaly Avatar asked Apr 17 '12 05:04

Naftaly


People also ask

Does Mac Have a clipboard manager?

The Best Mac Clipboard Managers No more rewriting every time you copy or cut something new—your clipboard manager will remember everything you've cut and copied in a nice long clipboard history. You can therefore avoid going between applications to copy and paste content.

How do you view clipboard history on Mac?

The clipboard is the place where your Mac stores the item you most recently copied. You can see what's stored there by opening Finder and then choosing Edit > Show Clipboard.

How do I manage clipboard on Mac?

A Mac clipboard is one of those macOS programs that runs in the background and has no functional user interface. Although you can find and view the last item copied to the clipboard by activating Finder, then selecting Edit ➙ Show Clipboard from the menu bar.

Can you see what you previously copied Mac?

However, if you want to view the clipboard, it can be located through the Finder menu in the top toolbar. Find and select Show Clipboard to see the last item you copied. It will only show the most recent item. Once you copy something else, the previous item disappears.


1 Answers

This seems to be an open bug, see MACOSX_PORT-511 ClipboardOwner method lostClipboard is not called if app is not focused.

like image 58
siegi Avatar answered Oct 07 '22 08:10

siegi