Skip to content

Commit 92ec0f6

Browse files
committed
first commit
1 parent dbb816d commit 92ec0f6

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
x_*

sikulix4python/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . sxgateway import *
2+
from . sxclasses import *
3+
from . sxundotted import *

sikulix4python/sxclasses.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
from . sxgateway import *
2+
3+
SX = SXPKG.script.SX
4+
SXRegion = SXPKG.script.Region
5+
SXScreen = SXPKG.script.Screen
6+
SXLocation = SXPKG.script.Location
7+
SXImage = SXPKG.script.Image
8+
SXImagePath = SXPKG.script.ImagePath
9+
SXApp = SXPKG.script.App
10+
11+
class SXBase():
12+
13+
SXClass = SX
14+
15+
def __init__(self, *args):
16+
self.instance = self.SXClass.getDefaultInstance4py()
17+
if len(args) > 0:
18+
self.instance = self.SXClass.make4py(convertArgs(args))
19+
20+
def __str__(self):
21+
return self.instance.toString()
22+
23+
def __getattr__(self, item):
24+
currentObject = self.instance;
25+
26+
def temp_method(*args, **kwargs):
27+
mCall = item + "("
28+
mCallError = "" + mCall
29+
countArgs = len(args)
30+
if countArgs > 0:
31+
mCall += "args[0]"
32+
mCallError += "%s" % (args[0])
33+
for nArg in range(1, countArgs):
34+
mCall += ", args[%d]" % (nArg,)
35+
mCallError += ", %s" % (args[nArg])
36+
mCall += ")"
37+
mCallError += ")"
38+
try:
39+
result = eval("currentObject." + mCall, {"currentObject": self.instance, "args": args})
40+
return result
41+
except:
42+
print("Method missing: %s::%s" % (currentObject, mCallError))
43+
return currentObject
44+
45+
return temp_method
46+
47+
class Region(SXBase):
48+
"""
49+
Wrapper for org.sikuli.script.Region
50+
51+
- Region() the region of the primary screen
52+
- Region(x,y,w,h) a region at (x,y) with size (w,h) cropped to the containing screen
53+
- Region(otherRegion) make an object-copy of object otherRegion
54+
"""
55+
56+
SXClass = SXRegion
57+
58+
def hover(self, *args):
59+
"""
60+
Move the mouse pointer to the given target (args[0])
61+
62+
if the target is
63+
- not given, it will be lastMatch or center (if no lastMatch) of this Region
64+
- an image-filename, a Pattern or an Image, it will first be searched and the valid Match's center/targetOffset will be the target
65+
- a Match: target will be center/targetOffset of the Match
66+
- a Region: target will be center of the Region
67+
- a Location: will be the target
68+
69+
:param args: see above
70+
:return: int: 1 if done without errors, 0 otherwise
71+
"""
72+
if len(args) == 0:
73+
return self.instance.hover()
74+
return self.instance.hover(convertArgs(args))
75+
76+
def click(self, *args):
77+
if len(args) == 0:
78+
return self.instance.click()
79+
return self.instance.click(convertArgs(args))
80+
81+
def highlight(self, *args):
82+
"""
83+
show a colored frame around the region for a given time or switch on/off
84+
85+
- **() or (color)** switch on/off with color (default red)
86+
87+
- **(number) or (number, color)** show in color (default red) for number seconds (cut to int)
88+
89+
allowed colors given as string
90+
- a color name out of: black, blue, cyan, gray, green, magenta, orange, pink, red, white, yellow (lowercase and
91+
uppercase can be mixed, internally transformed to all uppercase)
92+
- these colornames exactly written: lightGray, LIGHT_GRAY, darkGray and DARK_GRAY
93+
- a hex value like in HTML: #XXXXXX (max 6 hex digits)
94+
- an RGB specification as: #rrrgggbbb where rrr, ggg, bbb are integer values in range 0 - 255
95+
padded with leading zeros if needed (hence exactly 9 digits)
96+
97+
:param args: a valid combination (see above) or omitted
98+
:return: self
99+
"""
100+
if len(args) == 0:
101+
return self.instance.highlight()
102+
return self.instance.highlight4py(convertArgs(args))
103+
104+
class Screen(Region):
105+
106+
SXClass = SXScreen
107+
108+
class Location(SXBase):
109+
110+
SXClass = SXLocation
111+
112+
class Image(SXBase):
113+
114+
SXClass = SXImage
115+
116+
SCREEN = Screen()
117+
118+
def addImagePath(path):
119+
SXImagePath.add(path)
120+
121+
def openApp(app):
122+
return SXApp(app).open()
123+
124+
def switchApp(app):
125+
return SXApp(app).focus()
126+
127+
def closeApp(app):
128+
return SXApp(app).close()
129+
130+
def click(*args):
131+
return SCREEN.click(*args)
132+
133+
def hover(*args):
134+
"""
135+
**SCREEN::hover** Move the mouse pointer to the given target (args[0])
136+
137+
if the target is
138+
- not given, it will be lastMatch or center (if no lastMatch) of this Region
139+
- an image-filename, a Pattern or an Image, it will first be searched and the valid Match's center/targetOffset will be the target
140+
- a Match: target will be center/targetOffset of the Match
141+
- a Region: target will be center of the Region
142+
- a Location: will be the target
143+
144+
:param args: see above
145+
:return: int: 1 if done without errors, 0 otherwise
146+
"""
147+
return SCREEN.hover(*args)

sikulix4python/sxgateway.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
import time
3+
import os
4+
5+
def convertArgs(args):
6+
sxargs = JavaGW.jvm.java.util.ArrayList()
7+
for arg in args:
8+
try:
9+
sxargs.append(arg)
10+
except:
11+
sxargs.append(arg.instance())
12+
return sxargs
13+
14+
def sxstart():
15+
from py4j.java_gateway import JavaGateway
16+
try:
17+
JG = JavaGateway()
18+
SX = JG.jvm.org.sikuli
19+
return (JG, SX)
20+
except:
21+
print("sxstart: SikuliX not running")
22+
exit(1)
23+
24+
(JavaGW, SXPKG) = sxstart()
25+
26+
def sxClass(className, pkgName = "script"):
27+
classRef = "SXPKG.%s.%s" % (pkgName, className)
28+
theClass = eval(classRef)
29+
try:
30+
theClass.getDefaultInstance4py()
31+
except:
32+
print("sxClass: Class missing: %s" % (classRef))
33+
return None
34+
return theClass
35+
36+
def sxClassHelp(className, pkgName = "script"):
37+
theClass = sxClass(className, pkgName)
38+
if theClass:
39+
print(theClass.__doc__)
40+
return theClass

sikulix4python/sxundotted.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from . sxclasses import *
2+
3+
SCREEN = Screen()
4+
5+
def addImagePath(path):
6+
SXImagePath.add(path)
7+
8+
def openApp(app):
9+
return SXApp(app).open()
10+
11+
def switchApp(app):
12+
return SXApp(app).focus()
13+
14+
def closeApp(app):
15+
return SXApp(app).close()
16+
17+
def click(*args):
18+
return SCREEN.click(*args)
19+
20+
def hover(*args):
21+
"""
22+
**SCREEN::hover** Move the mouse pointer to the given target (args[0])
23+
24+
if the target is
25+
- not given, it will be lastMatch or center (if no lastMatch) of this Region
26+
- an image-filename, a Pattern or an Image, it will first be searched and the valid Match's center/targetOffset will be the target
27+
- a Match: target will be center/targetOffset of the Match
28+
- a Region: target will be center of the Region
29+
- a Location: will be the target
30+
31+
:param args: see above
32+
:return: int: 1 if done without errors, 0 otherwise
33+
"""
34+
return SCREEN.hover(*args)

testPy4j

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from sikulix4python import *
2+
hover()
3+
Screen().getCenter().grow(100).highlight(2)

0 commit comments

Comments
 (0)