I'm writing a small tool in clojure and want to know when there's been a change on the clipboard. Here's a simplified version of what's going on.
(:import java.awt.Toolkit)
(:import (java.awt.datatransfer Clipboard
ClipboardOwner
Transferable
StringSelection
DataFlavor
FlavorListener))
(defn get-clipboard [] (. (Toolkit/getDefaultToolkit)
(getSystemClipboard)))
(defn get-content []
(.getContents (get-clipboard) nil))
(def content (agent (get-content)))
(defn watch [key f]
(add-watch content key f))
(defn -main []
(while (not= content "banana-man")
(watch :watcher
(fn [key agent old-state new-state]
(prn "-- agent Changed --")
(prn "key" key)
(prn "atom" agent)
(prn "old-state" old-state)
(prn "new-state" new-state)))))
I've added in a while loop just to keep the main function from shutting down immediately.
This runs without throwing any errors, but does not report when changes have been made on the clipboard or stop the while loop when I copy bannan-man to the clipboard. I've been struggling with this for a few weeks now and I'm sure I'm missing something simple. If anyone has some advice I would really appreciate it!