dragDrop does not work using Jython script

Asked by Mohammed Abualrob

dragDrop(img1, img2) works perfectly fine using IDE however, the same call from a jython script does not work,
for example:

from org.sikuli.script import *

S = Screen()
S.dragDrop('1.png', '2.png')

This code does not work, it only find the first image, hover on top of it, then move to the next image and hover on top of it.
The actual dragging never happens, I tried all tricks, from changing delays in Settings to playing with wait to breaking the drag into a series of mouse operations, never works. I ran out of options, I saw a similar question in the forums, but my problems though look the same still I am referring to a jython script not IDE, not Java

any hints are appreciated
M.

Question information

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

focus problem?

before trying the drag, click into an area with no reaction, to get the image to be dragged to the front.

Revision history for this message
Mohammed Abualrob (maburrub) said :
#2

it is not a focus problem, I always make sure the area is active before any drag.
by the way, just to provide more info, sikuli actually clicks the first image but it seems like it does not keep the mouse down while the mouse is moving, that is why maybe the drag fails. Few on the internet are using Sikuli from a jython script, it is a one line of code, if anyone can try it will see the issue.

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

drag does not click, but only hovers and presses/holds left mouse button.

You might try a step by step solution:

src = s.find("image-of-source.png")
tgt = s.find("image-of-target.png")
s.hover(src)
wait(0.2)
mouseDown(Button.LEFT)
wait(0.2)
hover(tgt)
wait(0.2)
mouseUp()

You might vary the waits and put into a def if it works.

Revision history for this message
Mohammed Abualrob (maburrub) said :
#4

thanks for the code, but sorry it does not solve the problem.
I worked with Sikuli a lot and I know many tricks and how it works, this drag and drop feature is not working from a jython script, the only way to see it in action is to install jython, point to the sikuli jar file, import the modules, create a few lines of code and you will see it in front of your eyes. I did not give up quickly, I have been trying to make it work for months but it seems there is either a bug or something missing that I am not aware of. I created an account and posted the question after I ran out of all options. I read all related posts and searched extensively for similar issues. looks no or few people are using the api from python.
M.

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

I made tests on Mac and Windows and could not reproduce your problems.

just the plain dragDrop() works in all situations with the same workflow in IDE and running the script.py from command line with plain Jython.

--- script on Mac:
try:
  switchApp("Safari")
except:
  import sys
  import os
  dir = os.path.dirname(sys.argv[0])
  import org.sikuli.basics.SikulixForJython
  from sikuli import *
  setBundlePath(dir)
  switchApp("Safari")

hover("logo.png")
src = Mouse.at()
tgt = src.offset(0, -250)
hover(tgt)
tgt = Mouse.at()

# first dragDrop
dragDrop(src, tgt)

# second dragDrop with basic mouse actions
hover(src)
mouseDown(Button.LEFT)
hover(tgt)
mouseUp()
click(tgt)

--- script on Windows
try:
  hover()
except:
  import sys
  import os
  dir = os.path.dirname(sys.argv[0])
  import org.sikuli.basics.SikulixForJython
  from sikuli import *
  setBundlePath(dir)

popup("move mouse to source and press enter")
src = Mouse.at()
hover(src.offset(0,50))
hover(src)
popup("move mouse to target and press enter")
tgt = Mouse.at()
hover(tgt.offset(0,50))
hover(tgt)
click(src)
dragDrop(src, tgt)

The initial try:except: is to detect wether we are running in IDE or from plain Jython and to set the bundle path in case of Jython.

To run Jython I use the built-in:
java -cp <path-to>sikulix.jar org.python.util.jython <path-to>script.sikuli/script.py

I tested with the latest version 1.1.0 (nightly build from later today), which contains Jython 2.7b4

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

comment on the scenarios:

Mac: drags the Sikuli logo rom the browser window to a terminal window, which drops the link to the image

Windows: I used it with 2 explorer windows, dragging a file from one window to the other

Revision history for this message
Mohammed Abualrob (maburrub) said :
#7

Two things I am doing differently
1. I am not using the built in jython, I am using Jython 2.7b4 downloaded and installed from the website
2. I was pointing to sikuli-ide.jar and not sikulix.jar
I am not sure if that could be the reason why things are not working on my side. I will give it a try and update the ticket.
thanks for having the time to try my scenario
M.

Revision history for this message
Mohammed Abualrob (maburrub) said :
#8

Yes. now it is working, using sikuli-ide.jar seems to cause the problem. I never paid attention to that.
does that make sense ?

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

With external Jython, using the jar's containing the bundled Jython never makes sense, but it does not make sense at all using externally Jython 2.7 with sikuli-ide.jar, that contains Jython 2.5, which surely will lead to clashes on the class path and python path.

That it now works with sikulix.jar from version 1.1.0 sure is because the internal Jython also is the 2.7b3 or b4.
I recommend to using sikulixapi.jar according to
https://github.com/RaiMan/SikuliX-2014/wiki/Usage-in-Java-programming

With sikulix.jar it does not make sense to use an external Jython.
If one wants to have the "pure" Jython level, then just running
java -cp sikulix.jar org.python.util.jython does the job (this starts the same class as when you run the python command file).