0

Is there a way to copy a string to the clipboard through Scala?

Similar to Python's:

from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('this is my text')
r.destroy()

This question walks through how to copy a string to the clipboard in Swing, but I need something that will work with Scala as well: Copying to Clipboard in Java

Community
  • 1
  • 1
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • 1
    Check out the answer to the similar java question http://stackoverflow.com/questions/3591945/copying-to-clipboard-in-java – 4lex1v Jul 03 '13 at 20:05
  • @Alexlv I've tried, but I'm not very familiar with how to use java packages in Scala. Just the first line itself would make scala throw a fit: StringSelection selection = new StringSelection("test"). And yes, I also imported java.awt.datatransfer.StringSelection – Stupid.Fat.Cat Jul 03 '13 at 20:10

1 Answers1

5

The linked answer translates directly

val clipboard = java.awt.Toolkit.getDefaultToolkit.getSystemClipboard
val sel = new java.awt.datatransfer.StringSelection("test")
clipboard.setContents(sel, sel)
0__
  • 66,707
  • 21
  • 171
  • 266
  • @spydon yes it does. I just tested it (Debian) – 0__ Feb 08 '17 at 19:58
  • Sorry, badly worded comment, I meant that it doesn't work reliably on *nix. It doesn't work for me on Arch for example. Seems to be another SO thread about it here: http://stackoverflow.com/questions/14242719/copying-to-global-clipboard-does-not-work-with-java-in-ubuntu – spydon Feb 09 '17 at 10:32