Clipboard issues, old data isn't cleared, encoding problems

Bug #1317813 reported by Victor
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SikuliX
In Progress
Medium
RaiMan

Bug Description

Hi.

I ran across an issue (it occurs in about 1 out of 30 trials) where old data gets passed from the clipboard to the paste function.

Code:
        tripleClick(Pattern("1392988992662.png").targetOffset(70,2))
        type("c", KEY_CTRL)
        adress = Env.getClipboard().encode('iso-8859-15').strip()

and paste part... about 350 SLOC later:
        paste(u"V\xC4GD 1")

For some reason it picks a random object from the clipboard (and sometimes its not even the last copied item either...)

And why am I doin it this way? Well, its because special characters (like ÅÄÖ) arent very popular and cant be written using the type() function. But paste(u"") works.

Is this a problem in SikuliX or the Clipboard or somewhere else?

Machine = Win7 Pro SP1

Revision history for this message
RaiMan (raimund-hocke) wrote :

There are some known issues with paste.

In this case, you could try to guard your paste this way:

pasteText = u"V\xC4GD 1"
paste(pasteText)
if pasteText != Env.getClipboard().encode('iso-8859-15'):
    print "some error message"
    exit(1)

BTW:
the IDE editor window accepts utf-8 characters produced by your keyboard and shows them as such.
Python at script run is told, that the script is encoded in utf-8

So you could use:
pasteText = u"VÄGD 1"

… and why are you not completely working in utf-8?

Changed in sikuli:
status: New → In Progress
importance: Undecided → Medium
assignee: nobody → RaiMan (raimund-hocke)
Revision history for this message
Victor (victor1986) wrote :

I tried it. This is the result

My code:

    # -*- coding: utf-8 -*-
    import locale <-- tried without this as well
    locale.setlocale(locale.LC_ALL, '') <-- tried without this as well

    def tripleClick(PSMRL):
        hover(PSMRL)
        for x in xrange(3):
            mouseDown(Button.LEFT)
            mouseUp()

    tripleClick("1399632526648.png")

    type("c", KEY_CTRL)
    testString = Env.getClipboard().encode('utf-8').strip()
    popup(str(testString))

String:
    ÅÄÖåäö with UTF-8 encoding
    ÅÄÖåäö with ISO encoding

If there is a simpler way to do this I'm all ears. :) Im just new to Java and Python

Revision history for this message
Victor (victor1986) wrote :

Thanks for the tip on the clipboard validation. Since I dont take no for an answer in my script I would place it i a loop.

x = 3
while x > 2
    pasteText = u"V\xC4GD 1"
    paste(pasteText)
    if pasteText != Env.getClipboard().encode('iso-8859-15'):
        x = x
        deleteFieldContents #function for just looping backspace a set number of times
    else:
        x = 1

Its not the prettiest solution but it, and similar fixes, works wonders ;)

Revision history for this message
RaiMan (raimund-hocke) wrote :

# -*- coding: utf-8 -*-
is not needed in scripts run with Sikuli, since this is added automatically at runtime

Other things I admit, are not documented yet:

popup() finally takes the text as unicode string. Since from clipboard it is returned as unicode string already from the Java level, this should work in your case:

# any code, that puts "ÄÖÜäöü" on the clipboard (I am on a german keyboard, tested on Mac ;-)
text = Env.getClipboard()
popup(text)

should show up as expected
print text should complain non-ASCII

print text.encode("utf-8") # works on command line
but prints question marks to the IDE's message area (has to be fixed)

there is a helper:
uprint(text) internally does the encoding to UTF-8
and can be used the same way as print

Another quirk:
popup("ÄÖÜäöü") does not work

but this works:
popup(unicode("ÄÖÜäöü", "utf-8"))

and again there is a helper:
popup(unicd("ÄÖÜäöü"))

conclusion:
just staying with utf-8 should work.

Strings, that are generated on the script level (like text = "ÄÖÜäöü") have to be unicode strings when given down to the Java layer (like with popup()) and hence to be converted using unicode(string, "utf-8") (helper: unicd() )

RaiMan (raimund-hocke)
summary: - Clipboard issues, old data isnt cleared
+ Clipboard issues, old data isn't cleared, encoding problems
Revision history for this message
Victor (victor1986) wrote :

Wow. That was awesome!

It totally works.

Why did I try it the hard way? :(

Thank you very much.

Revision history for this message
RaiMan (raimund-hocke) wrote :

Thanks for feedback.

… and for trying the hard way (how should you have known - not in the docs yet ;-)

… but asking here at an early stage of trying something is always a good idea (there are no stupid questions - only stupid answers ;-)

Victor (victor1986)
information type: Public → Public Security
information type: Public Security → Public
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.