Skip to content

Instantly share code, notes, and snippets.

@pikariop
Forked from exupero/clipboard.clj
Last active December 15, 2023 14:48
Show Gist options
  • Save pikariop/4b7474ebe4e6de89cc7f186b7e002254 to your computer and use it in GitHub Desktop.
Save pikariop/4b7474ebe4e6de89cc7f186b7e002254 to your computer and use it in GitHub Desktop.
Clojure code to interact with the system clipboard
(require '[clojure.pprint :as pp])
(import '[java.awt.datatransfer DataFlavor StringSelection Transferable])
(defn clipboard []
(.getSystemClipboard (java.awt.Toolkit/getDefaultToolkit)))
(defn slurp-clipboard []
(try
(.getTransferData (.getContents (clipboard) nil) (DataFlavor/stringFlavor))
(catch java.lang.NullPointerException e nil)))
(defn spit-clipboard [text]
(let [selection (StringSelection. text)]
(.setContents (clipboard) selection selection)))
(defn pp-to-clipboard [x]
(let [out (java.io.StringWriter.)]
(pp/pprint x out)
(spit-clipboard (.toString out))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment