2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Clojureでクリップボードを使ってみる

Posted at

Clojureでクリップボードを使うのは簡単にできます。

clipboard.clj
(ns use-clipboard.core
  (:import [java.awt Toolkit]
           [java.awt.datatransfer Clipboard DataFlavor StringSelection]))

(def ^Clipboard clip (.getSystemClipboard (Toolkit/getDefaultToolkit)))

(defn get-string []
  (when (.isDataFlavorAvailable clip DataFlavor/stringFlavor)
    (.getData clip DataFlavor/stringFlavor)))

(defn get-filelist []
  (when (.isDataFlavorAvailable clip DataFlavor/javaFileListFlavor)
    (.getData clip DataFlavor/javaFileListFlavor)))

(defn set-string [s]
  (let [ss (StringSelection. (print-str s))]
    (.setContents clip ss ss)))

上記get-stringではjava.lang.Stringが返り、get-filelistではjava.util.List<java.io.File>が返ります。

Excelのセルをコピーしたものもget-stringで取れるので何かと便利です。

上記には実装してませんが、他にもImage(コピーしている画像)を取得したり、クリップボードの変化の監視ができたりするようです。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?