How do I get clipboard access (read/write) on Kotl...
# announcements
b
How do I get clipboard access (read/write) on Kotlin, not Android, not AWT, not JavaFX, pure Kotlin for OS usage? I searched everywhere, the best answer so far has been to force users to install xclip, and call it from Runtime(), which is not a great solution.
s
How do you expect this to work without a library like AWT? at some point you’re going to need to access a given platform’s specific clipboard API, and those libraries already handle detection and translation for you
There’s no point in JetBrains providing it for Kotlin/Native specifically because a) it’s kinda niche b) it’s hard to maintain and test across platforms
also doesn’t everyone with X11 already have
xclip
installed?
I feel like for the majority of people using the system clipboard this is a non-issue
e
not all environments have clipboard, take embedded for example
s
This is true, but then I think that’d mean @Bernardo Ferrari’s code wouldn’t work at all
if you have X11 installed, you almost certainly have
xclip
unless you opted not to install the metapackage and just installed, idk like,
xorg-server
on its own
b
I was testing against macOS, so I needed to install xclip.
s
you can use
pbcopy
on macOS
b
I mean, Python, Ruby, etc all have a clipboard library to take care of everything. I just looked at this, might not be hard to translate into kotlin: https://github.com/asweigart/pyperclip/blob/master/src/pyperclip/__init__.py
s
pyperclip
isn’t in the stdlib though
and it literally has a hard dependency on xclip/pbcopy as documented in the leading docstring
I would assume there’s no JVM analog because AWT and JavaFX exist already
So, like, what do you really want here
b
I am playing with scripts
Do I have access to AWT out of nothing on everywhere?
s
you don’t, but you also don’t have access to xclip or pbcopy everywhere either
but I’m preeeetty sure you have AWT access on the major platforms if that’s sufficient
b
let me test, hold on
Well.. it is not easy or trivial to setup, but kind of works. The only weird thing is that it opens java (like a real program) and closes really fast. Thanks.
c
Be careful with awt, there is an unresolved bug: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6606476
b
I saw that @Czar , but I think it is the only way. Do you know any other?