← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #237161]: how to pass a variable between multiple scripts

 

Question #237161 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/237161

Victor posted a new comment:
I know this solution may have issues but here it goes anyway :)

You can always pass information using the Clipboard, the caveat is that
you should use the try() method described here
https://answers.launchpad.net/sikuli/+question/229988 or run the script
from the command line

The problem with using visual triggers is that Sikuli image search
steals focus (havent tried onAppear, but i know that at least click()
and exists() steal window focus). And this can seriously disrupt actions
like mouse clicks and hovers etc. So, how to get around it?

You can write stuff to Clipboard!

Here is the listener

x = 0
while x < 1
    trigger = Env.getClipboard().strip()
    if trigger == 'whateverTriggesTheListener'
        x=2
    else:
        x=x
    wait(1)


And here is what is passing data to the listener

    from java.awt.datatransfer import StringSelection #Stick this in the top section of the script
    from java.awt.datatransfer import Clipboard #Stick this in the top section of the script
    from java.awt import Toolkit #Stick this in the top section of the script
    
    toolkit = Toolkit.getDefaultToolkit() #More info here http://stackoverflow.com/questions/10392033/clearing-windows-clipboard-while-using-sikuli-or-jython
    clipboard = toolkit.getSystemClipboard()
    
    vtVarOne = Env.getClipboard().strip() #just takes whatever is in the clipboard. Preferably text though...
    vtVarTwo = Env.getClipboard().strip() #Just to show that jou can call the clipboard several times
    vtVarThree = 'whateverTriggesTheListener' #This is the trigger or whatever
    
    strOut = vtVarOne + " first read worked " + vtVarTwo + " and so did the other"
    popup(strOut)
    
    clipboard.setContents(StringSelection(vtVarThree), None) #Throws in vtVarThree in the Clipboard
    
    switchApp("Notepad") #Assuming you have Notepad or Notepad++ open...
    type("v", KEY_CTRL) #Demonstrating that indeed, the clipboard worked :)
    
So you can use timed events to get this done and then use the Clipboard to send stuff from one script to the other. Its similar to writing to a file... but without needing a file.


If you arent running the scripts in parallell but rather in tandem you should use the argv method.

Simply put, in your active script type:

    vtVarOne = "TheTextYouWantToPassWithoutSpaces"
    vtVarTwo = "OtherVar"
    cmd = r'C:\--PATH TO--\SikuliX\runIDE.cmd -r G:\-- PATH TO SCRIPT --\SCRIPT.sikuli --args ' + str(vtVarOne) + ' ' + str(vtVarTwo)
    openApp(cmd)

And in the receiver you type:

    import sys 
    rVarOne = int(sys.argv[1])
    rVarTwo = int(sys.argv[2])
    rReallyReallyLongString = Env.getClipboard().strip() #The clipboard is just so convenient
    #... and so on


Now you have two more ways to solve an old problem.

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.