Last active
September 15, 2021 19:51
-
-
Save brettrowberry/4a738586b626667a17609f7a2233d35c to your computer and use it in GitHub Desktop.
Write to and read from system clipboard in Clojure REPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Credit, Nick Nichols | |
;; only tested on macOS Big Sur | |
(import '[java.awt.datatransfer DataFlavor StringSelection Transferable]) | |
(defn clipboard [] | |
(.getSystemClipboard (java.awt.Toolkit/getDefaultToolkit))) | |
(defn clipboard->str | |
[] | |
(.getTransferData (.getContents (clipboard) nil) (DataFlavor/stringFlavor))) | |
(defn str->clipboard [text] | |
(let [selection (StringSelection. text)] | |
(.setContents (clipboard) selection selection))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment