Skip to content

Instantly share code, notes, and snippets.

@brettrowberry
Last active September 15, 2021 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettrowberry/4a738586b626667a17609f7a2233d35c to your computer and use it in GitHub Desktop.
Save brettrowberry/4a738586b626667a17609f7a2233d35c to your computer and use it in GitHub Desktop.
Write to and read from system clipboard in Clojure REPL
;; 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