Couple of questions regarding how-to

Asked by Neo

I made a suite which now can test an app in English and for any other locale. But, as of now I change the locale manually and run the suite. I want to write a .sh file to change the Mac OS language each time my suite runs based on the language I pass as command line arguments. Please see my below questions..

1. How to change the Mac OS language (either using AppleScript/Shell Script) - For MAC. Once it is done for a FR for instance, I can then call my suite passing fr-fr as a argument to run the whole suite for FR. Again the same script to change to other locale and again calling the suite for that locale. I want to put all these in a script file (like batch file in windows) so that clicking on that sh file, all locales will be tested with out manual intervention. Please advice how to achieve this ?

2. The other challenge I am facing is, my Main Script uses middle layer framework file (main script importing the framework file). My framework file importing the locale images. Now, if I pass locale as a command line to main script, how can I say to my framework file to use that locale.
Example:
#Framework file is like this (myFramework.sikuli)
locale = "En-En"
exec("import "+locale)
exec("reload(%s)"%(locale))
exec("from %s import *"%(locale))

#main file is (main.sikuli)
import myFramework
reload(myFramework)
from myFramework import *

myFramework.locale = sys.argv[1]

If I pass argv[1] as FR, the framework file is initializing to FR but, while execution it is looking for EN images only. I guessing this is because of importing 'myFramework' before assigning locale.

Suggest me how to achieve this.

3. I am receiving this error in my report file if I run the script without re-starting the IDE. In the fresh instance of sikuli IDE this error is not seen. Why do we get this error?

ft1.1: Traceback (most recent call last):
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/logging/__init__.py", line 750, in emit
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/logging/__init__.py", line 750, in emit
ValueError: I/O operation on closed file
Traceback (most recent call last):
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/logging/__init__.py", line 750, in emit
  File "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/logging/__init__.py", line 750, in emit
ValueError: I/O operation on closed file

Thanks in advance.
Neo.

Question information

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

--1. changing locale
The only automation possibility (means not programming in Objective-C) is to use the GUI access of Applescript or Sikuli :-), to change the preference settings
the principal way with Applescript:
http://stackoverflow.com/questions/4064647/scripting-system-preferences-language-text
You could run the script from within your Sikuli scripts using os.popen and osascript.

--2: a global in an imported modul
Yes your problem is, that the exec's are processed at import and the change of the locale value afterwords does not effect these imports anymore.
The easiest approach would be to have a def setLocale(locale), that sets the locale inside the module and is called each time, you change the locale.

#Framework file is like this (myFramework.sikuli)
locale = "En-En"
exec("import "+locale)
exec("reload(%s)"%(locale))
exec("from %s import *"%(locale))

def setLocale(loc = "En-En"):
    locale = loc

#main file is (main.sikuli)
import myFramework
reload(myFramework)
from myFramework import *

setLocale(sys.argv[1])

--3. logging error
as far as I know, this is a Jython problem.
I remember, that this problem already went through this board, but I cannot find it again.
And there was some workaround for this.

Does it happen also, when you run your script from command line? or only in IDE?

Revision history for this message
Neo (simamhussain) said :
#2

For the 2nd Question#
It is still same right, using a method call after import statement. exec's would have processed at import.

I tried, it is still looking for EN images, instead of IT (passed as arg)

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

Oh yes really stupid :-((

so you have to use

#Framework file is like this (myFramework.sikuli)
locale = sys.argv[1]
exec("import "+locale)
exec("reload(%s)"%(locale))
exec("from %s import *"%(locale))

or if you want to make it more general:

#Framework file is like this (myFramework.sikuli)
locale = Settings.currentLocale
exec("import "+locale)
exec("reload(%s)"%(locale))
exec("from %s import *"%(locale))

and

#main file is (main.sikuli)
Settings.currentLocale = sys.argv[1]
import myFramework
reload(myFramework)
from myFramework import *

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

Settings is the "global" settings container from Sikuli

Revision history for this message
Neo (simamhussain) said :
#5

Super. It worked. Thanks.
Q1: I will try to write sikuli script to change the locale in system preference and will call it before running the suite. Will let u know the progress on it.

Q2: Solved.

Q3: Yes, it is happening running with IDE alone. Even in IDE, if you re-start it this error wont be there. Thats fine, most of the times, I run from command line.

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

Q3:
then you should put a
reload(logging)
after this import too.