[Java] Set clipboard without actually pasting

Asked by Willie

Hey is there any way that I can use a command like paste() to just load some text into the clipboard without actually pasting it yet?

I'm trying to paste text into a command prompt, but when I use paste() it just gives me one of those ^V's. I can get around it by hitting enter after that and then pasting using mouse clicks (right click -> paste), but I'd rather not have the ^V show up at all.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
RaiMan Edit question
Solved by:
Willie
Solved:
Last query:
Last reply:
Revision history for this message
Willie (willie-owens91) said :
#1

Sorry, found it:

Clipboard.putText("text")

Revision history for this message
Willie (willie-owens91) said :
#2

Ok, I'm coding using Sikuli in Java and although the first time I set the clipboard it seems to work, after that I get a "java.lang.IllegalStateException: cannot open system clipboard"

Anyone know why?

Revision history for this message
RaiMan (raimund-hocke) said :
#3

You are using the Sikuli implementation of clipboard handling, which is based on the Java classes
- java.awt.datatransfer.Clipboard
- java.awt.Toolkit
and related interfaces, that have to be implemented.

But the Sikuli implementation seems to have a problem, since intermixing of paste() (sets the clipboard) and Env.getClipboard() (gets the clipboard), makes it to not work anymore. Your symptoms seem to be the same.
see bug 769837

Conclusion:
Since you are on Java, try to implement your own clipboard handling.
A good example: http://www.javapractices.com/topic/TopicAction.do?Id=82

Revision history for this message
Willie (willie-owens91) said :
#4

Same error. Guess this is a java issue.

Revision history for this message
Willie (willie-owens91) said :
#5

Ok I found that it only happens when I try to paste a large string. Looks like I'll just split it up for now

Revision history for this message
Willie (willie-owens91) said :
#6

I'm getting the error when a string I'm trying to copy to the clipboard is around 35 characters long

Revision history for this message
Willie (willie-owens91) said :
#7

Wow. After merely looping a try-catch block to attempt the setClipboard operation... it worked! Guess there's no option, but this solved my problem

Revision history for this message
William Wilkins (enduserstudios) said :
#8

Can I see your script? I am trying to use it my self but I get the error:

[error] Stopped
[error] An error occurs at line 95
[error] Error message: Traceback (most recent call last):
 File "C:\Users\Catagris\AppData\Local\Temp\sikuli-tmp3315727912022406462.py", line 95, in
 Clipboard.putText("='" + month + " Daily Totals'!A34 ='" + month + " Daily Totals'!B34 ='" + month + " Daily Totals'!D34 ='" + month + " Daily Totals'!E34")
NameError: name 'Clipboard' is not defined

Any help?

Revision history for this message
RaiMan (raimund-hocke) said :
#9

@William
in the current version of Sikuli this feature is simply not useable as you try it.

You have to implement something based on comment #3 (import the respective Java classes in your script) or wait for the new Version (will have: App.getClipboard and App.setClipboard).

Revision history for this message
Vyacheslav Kolesnik (uzb310) said :
#10

Is it a possible to register the clipboard format?
I need prepare some data into the word table format then put it trough Sikuli into some place of win32 app what accepts only word table data.
According to Microsoft ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx ) I need register clipboard format to "Rich Text Format". Then put data in RTF format into clipboard.

Revision history for this message
RaiMan (raimund-hocke) said :
#11

@Vyacheslav
Not a feature currently.

To get this, you have to implement your own clipboard handler using the respective Java features.

Revision history for this message
Vyacheslav Kolesnik (uzb310) said :
#12

OK, thank you, RaiMan!

Revision history for this message
Vyacheslav Kolesnik (uzb310) said :
#13

@RaiMan
My Sikuli script was prepared in Jython.
I'm not good with Java. I have found the following example what can do what I need: http://stackoverflow.com/questions/14908629/formatted-cliboard-java
Can you help me to include that class to Jython based Sikuli script?

Revision history for this message
RaiMan (raimund-hocke) said :
#14

Good finding.

I do not have any time for that the next 2 weeks. sorry.

You surely find helpful information in the Jython wiki about using Java stuff in Jython.
In short: you simply have to transcript the Java syntax to Python syntax and add the relevant imports.

Revision history for this message
Vyacheslav Kolesnik (uzb310) said :
#15

Yes, thank you!
I'm going on a same way.