Jython: how to paste chinese characters (unicode characters)

Asked by coldtest

This solution only applies to scripts run by using a Jython interpreter directly or other Java based scripting solutions like JRuby.

For Jython you need 2 things:
--1. Python encoding directive
in the first or second line of your script you need a magic comment that defines the encoding used for the script:
# coding=utf-8
Of course: your script has to be stored as file with utf-8 encoding by your script editor.
--2. you have to tell the JVM, the same thing by giving this as a parameter when calling the JVM:
-Dfile.encoding=utf-8

Below in comments #4 and #5 find a description of a successful test with plain Jython by coldtest(https://launchpad.net/~tesger)

This is not needed with scripts run with the support of Sikuli IDE or run with one of the approaches from http://sikuli.org/docx/faq/010-command-line.html.
----------------------------------------------------------------------------------------------------------------------------------------------------------------

 I used the paste function to input the chinese to notepad in jruby script ,but it can't be correct in the notepad. they are distorted Character Code , the script as follow:
require 'rubygems'
require 'java'
require 'iconv'

java_import "org.sikuli.script.SikuliScript"
java_import "org.sikuli.script.Region"
java_import "org.sikuli.script.Screen"
java_import "org.sikuli.script.App"

screen=Screen.new
App.focus("notepad")
sleep(1)
str = "梅西"
sleep(2);
screen.paste("1.png",str)

but it is correct when I use paste function in sikuli IDE ,the script as fellow:

App.focus("notepad")
sleep(3)
paste( ,"梅西")

Question information

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

I am not the expert in JRuby.

But: You have to make sure, that the string is a unicode string, if it is not plain ASCII, when using paste().

In Python (Sikuli script) you would have to write
paste(u"some-unicode-characters")

hope it helps.

Revision history for this message
coldtest (tesger) said :
#2

Today I download jython and write a scrip to do the same thing as the jruby script.
the jython script is :

# coding=utf-8
import sys

#print sys.getdefaultencoding()
#reload(sys)
#sys.setdefaultencoding('utf-8')
#print sys.getdefaultencoding()

#print sys.path

from sikuli.Sikuli import *

switchApp("C:\Program Files\Internet Explorer\IEXPLORE.EXE")
wait("D:\\script\\jy\\img\\url_input.png",10)
paste("D:\\script\\jy\\img\\url_input.png","http://www.google.com.hk")
sleep(2)
type(Key.ENTER)
sleep(3)
#seach_key = unicode('梅西','gbk')
seach_key = u'梅西'
paste("D:\\script\\jy\\img\\key_search.png",seach_key)
click("D:\\script\\jy\\img\\search_button.png",0)
sleep(3)
closeApp("Google");

I save the script file as UTF-8 ,if I don't do this ,I must meet a error:
D:\script\jy>jython test.py
  File "<string>", line None
SyntaxError: Illegal character in file 'test.py' for encoding 'utf-8'

after I save the script as UTF-8 ,I can run the script successly ,but I also met the same problem as jruby ,
It is distorted Character Code which was inputed in google search . and I can't search the correct result. hope for you help ,thanks .

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

I confirm, that your approach does not solve the problem.

But since I know, that it works in the Sikuli IDE, I had a look at the sources to find out, how they manage it.

You need 2 things:

--1. Python encoding directive (you did it already)
in the first or second line os your script you need a magic comment that defines the encoding used for the script:
# coding=utf-8

there are some other options, but for a Jython script this works.

--2. you have to tell the JVM, the same thing by giving this as a parameter when calling the JVM:
-Dfile.encoding=utf-8

When I did this with the Sikuli contained Jython 2.5.1 it worked, the same way as it does in the Sikuli-IDE:

java -Dfile.encoding=utf-8 -cp absolute-path-to\sikuli-script.jar org.python.util.jython utftest.py

The script successfully pastes a unicode character into a unicode aware inputfield, e.g.
paste("<é>")

So you have to find out, how to add this parameter to the Java/Jython environment of Jython2.5.2 (I remember, that the comments in the jython.bat give some hints about the recognized environment variables).

hope it helps.

Revision history for this message
coldtest (tesger) said :
#4

 Thanks you soooooo much ,RaiMan , I resoved the problem as your suggestion
, at first ,I modify the jython.bat as :

set _FULL_CMD=%_JAVA_CMD% -Dfile.encoding=utf-8 %_JAVA_OPTS% %_JAVA_MEM%
%_JAVA_STACK% -Dpython.home=%_JYTHON_HOME% -Dpython.executable="%~f0"
%_BOOT_CP% -classpath "%CLASSPATH%" org.python.util.jython %_JYTHON_OPTS%
%_JYTHON_ARGS% %_ARGS%
echo %_FULL_CMD%

I add the paramter "-Dfile.encoding=utf-8 " in jython.bat

at second , I modify the test.py :
chage "seach_key = u'梅西' " to seach_key = '梅西'

when I run the test.rb, yes , It's successly to search what I want .

D:\script\jy>jython test.py
"C:\Program Files\Java\jre6\bin\java" -Dfile.encoding=utf-8 -Xmx512m
-Xss1152k
 -Dpython.home="D:\jython2.5.2"
-Dpython.executable="D:\jython2.5.2\jython.bat"
 -classpath
"D:\jython2.5.2\jython.jar;.;D:\jython2.5.2\sikuli-script.jar;D:\Pro
gram Files\Sikuli\sikuli-script.jar" org.python.util.jython test.py
[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
[log] App.focus C:\Program Files\Internet Explorer\IEXPLORE.EXE(0) #0
[log] CLICK on (509,126)
[log] TYPE "
"
[log] CLICK on (668,389)
[log] CLICK on (829,428)

thanks you again . you are so kind , sikuli is so good and I love it very
much .

2011/5/8 RaiMan <email address hidden>

> Your question #156443 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/156443
>
> Status: Open => Answered
>
> RaiMan proposed the following answer:
> I confirm, that your approach does not solve the problem.
>
> But since I know, that it works in the Sikuli IDE, I had a look at the
> sources to find out, how they manage it.
>
> You need 2 things:
>
> --1. Python encoding directive (you did it already)
> in the first or second line os your script you need a magic comment that
> defines the encoding used for the script:
> # coding=utf-8
>
> there are some other options, but for a Jython script this works.
>
> --2. you have to tell the JVM, the same thing by giving this as a parameter
> when calling the JVM:
> -Dfile.encoding=utf-8
>
> When I did this with the Sikuli contained Jython 2.5.1 it worked, the
> same way as it does in the Sikuli-IDE:
>
> java -Dfile.encoding=utf-8 -cp absolute-path-to\sikuli-script.jar
> org.python.util.jython utftest.py
>
> The script successfully pastes a unicode character into a unicode aware
> inputfield, e.g.
> paste("<é>")
>
> So you have to find out, how to add this parameter to the Java/Jython
> environment of Jython2.5.2 (I remember, that the comments in the
> jython.bat give some hints about the recognized environment variables).
>
> hope it helps.
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/sikuli/+question/156443/+confirm?answer_id=2
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/sikuli/+question/156443
>
> You received this question notification because you asked the question.
>

Revision history for this message
coldtest (tesger) said :
#5

I'm sorry to make a mistake because I'm too exciting . the correct change of code is :

at second , I modify the test.py :
modify the sentence : seach_key = u'梅西' to seach_key = '梅西', it must delete the 'u' before the unicode words.

when I run the test.py, yes , It's successly to search what I want .
D:\script\jy>jython test.py
"C:\Program Files\Java\jre6\bin\java" -Dfile.encoding=utf-8 -Xmx512m
-Xss1152k
 -Dpython.home="D:\jython2.5.2"
-Dpython.executable="D:\jython2.5.2\jython.bat"
 -classpath
"D:\jython2.5.2\jython.jar;.;D:\jython2.5.2\sikuli-script.jar;D:\Pro
gram Files\Sikuli\sikuli-script.jar" org.python.util.jython test.py
[info] Sikuli vision engine loaded.
[info] Windows utilities loaded.
[info] VDictProxy loaded.
[log] App.focus C:\Program Files\Internet Explorer\IEXPLORE.EXE(0) #0
[log] CLICK on (509,126)
[log] TYPE "
"
[log] CLICK on (668,389)
[log] CLICK on (829,428)

Revision history for this message
coldtest (tesger) said :
#6

Thanks RaiMan, that solved my question.

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

Thanks for the feedback.

As you might have already noticed, I changed the title and put the main things into the top of the question.

Revision history for this message
coldtest (tesger) said :
#8

yes,I have noticed it , thnaks . ^-^

Revision history for this message
coldtest (tesger) said :
#9

I just have resolved this poblem about how to input unicode by post method
in jruby yesteday , the key steps are :
1) It must save the rb file as no-bom UTF ( I saved it with bom UTF-8
file before I sent the email to you )
2) use : jruby -J-Dfile.encoding=UTF-8

I use ultraEdit as the IDE and save the rb file as UTF-8 ,but the file has
BOM , so I use the notepad ++ to save the file as no-bom UTF-8 file and it
can be excuted successly .

thanks

2011/5/9 coldtest <email address hidden>

> Your question #156443 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/156443
>
> coldtest posted a new comment:
> yes,I have noticed it , thnaks . ^-^
>
> --
> You received this question notification because you asked the question.
>

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

thanks for the feedback.

I will have a look at JRuby (since I am a Ruby fan) and integrate the usage in the docs besides Jython.

Revision history for this message
brian (brian-ckh) said :
#11

I wrote a simple sikuli script in java.

import org.sikuli.script.*;
import java.lang.String;
public class TestSkiuli{
   Screen s = new Screen(100);
   try{
              s.Click("imng/1317125108412.png",o);
              s.type(null"RealPlayer");
         }catch (FindFalse e)
 }
    e.printStackTrace();
}
it can work.
But when i change "RealPlayer"to chinese character,it will produce decoding character.
"Exception in thread "main" java.lang.IllegalArgumentException: Cannot convert character"
what can i do???

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

@brian
type() only accepts ASCII characters.
see faq 933

BTW: your comment (question) has nothing to do with the original question.
pls. post questions as new ones.

Revision history for this message
brian (brian-ckh) said :
#13

can i use paste() or other methods to solve my problem?

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

@ Brian
paste() should work

Revision history for this message
brian (brian-ckh) said :
#15

i change type() to paste().
                 ....
                 ....
                 s.paste(null ,"記事本");

it shows me decoding characters and are not correct chinese characters.
how can i do?

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

@Brian
try paste("記事本") in the Sikuli IDE.
If that works, you have to encode the pasted string in Java as utf-8.