28775

Can't monitor system clipboard changes from a background java application on Mac OS X

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; } } } }

Answer1:

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?

Recommend

  • How come struts property tag converts java.util.Date in m/d/yy format
  • C# Getting window's title by handle in Hebrew return question marks
  • Programmatically set PIN/Password used to unlock device
  • convert json date to javascript date format
  • C++ reference variable across files
  • How can I train an intent in Watson conversation to accept any number?
  • Cannot convert a char value to money. The char value has incorrect syntax
  • Is there a chance to get -splash: work for SWT applications that require -XstartOnFirstThread?
  • help('modules') crashing? Not sure how to fix
  • pygame.init() shows as undefined variable after installing Pygame
  • include dlls in visual studio c++ 2008
  • Can I use AllJoyn Framework for Wifi Direct in iOS?
  • Compare struct to a constant in C
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • R Split data.frame using a column that represents and on/off switch
  • C++ pointer value changes with static_cast
  • Android application: how to use the camera and grab the image bytes?
  • How can I sort a a table with VBA with given text condition?
  • PHP buffered output depending on server setting?
  • Functions in global context
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • QLineEdit password safety
  • Is possible to count alias result on mysql
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Check if a string to interpolate provides expected placeholders
  • ILMerge & Keep Assembly Name
  • Large data - storage and query
  • How do you troubleshoot character encoding problems?
  • Apache 2.4 - remove | delete | uninstall
  • WOWZA + RTMP + HTML5 Playback?
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to set the response of a form post action to a iframe source?
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Django query for large number of relationships
  • reshape alternating columns in less time and using less memory