Skip to content

Instantly share code, notes, and snippets.

@brake
Created September 15, 2016 21:30
Show Gist options
  • Save brake/c944229350e91f295a1762d3274393ef to your computer and use it in GitHub Desktop.
Save brake/c944229350e91f295a1762d3274393ef to your computer and use it in GitHub Desktop.
Text IO for system clipboard with Clojure
(ns clipboard.core
(:import (java.awt.datatransfer DataFlavor Transferable StringSelection)
(java.awt Toolkit)))
(defn get-clipboard
[]
(-> (Toolkit/getDefaultToolkit)
(.getSystemClipboard)))
(defn slurp-clipboard
[]
(when-let [^Transferable clip-text (some-> (get-clipboard)
(.getContents nil))]
(when (.isDataFlavorSupported clip-text DataFlavor/stringFlavor)
(->> clip-text
(#(.getTransferData % DataFlavor/stringFlavor))
(cast String)))))
(defn spit-clipboard
[s]
(let [sel (StringSelection. s)]
(some-> (get-clipboard)
(.setContents sel sel))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment