Created
September 15, 2016 21:30
-
-
Save brake/c944229350e91f295a1762d3274393ef to your computer and use it in GitHub Desktop.
Text IO for system clipboard with Clojure
This file contains 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
(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