copy and paste text from web page to a variable in the script

Asked by surfdork

Is it possible to copy and paste text from a web page into sikuli and assign it as a variable?

Example, I can double click on a specific offset, highlight the string copy it to the clipboard
and that is where the problem lies.
I can get paste(0 returning a 1 but not the string
I can manuallu type ctrl +v an past what was successfully copied.
I even tried to launch an input box then paste with ctrl +v and that failed

code

doubleClick(Pattern("Projectspost-2.png").targetOffset(7,-1))
type("c", KEY_CTRL)
#above works perfect
name = input("Please enter your name to log in:")
paste()
fails

The following fails
doubleClick(Pattern("Projectspost-2.png").targetOffset(7,-1))
type("c", KEY_CTRL)
name = input("Please enter your name to log in:")
click
wait(2)
type("v", KEY_CTRL)

The only thing I've been able to do is paste into a notebpat type of file, I cannot via sikuli in any way add input to the input box nor assign the string copied via ctrl+c to anything

I tried to follow your example on the blog but get lost in your lambda formulations

Is it possible to type("c", KEY_CTRL) to copy text then assign that string resident to a variable or just to past that string into the sikuli script where I can execute a slice operation befor assigning that string to a variable?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

The function you need is
Env.getClipboard()

which returns the current text content of the clipboard.

doubleClick(Pattern("Projectspost-2.png").targetOffset(7,-1))
type("c", KEY_CTRL)
cb = Env.getClipboard()

now cb contains the text, that was copied from the selection produce by the doubleClick using ctrl-c.

This was the good news.

Now the bad news:
Together with paste(), Env.getClipboard() only works correctly until after the first usage of paste(). Then it always returns the string that was last pasted with paste() and not what you expect to be on the clipboard (this is an open bug :-(

So if you need paste() (because the text cannot be used with type() (faq 933)) in your script, Env.getClipboard() will not work for you.

If you can use type() to get something written on the screen, you can use Env.getClipboard().

If you are familiar with Java, you might implement your own clipboard feature together with your own paste() function.

Can you help with this problem?

Provide an answer of your own, or ask surfdork for more information if necessary.

To post a message you must log in.