Return value to command line

Asked by Peter Weller

Is it possible to run a script from command line AND return some value?

I want to run my script from another Application and return the coordinates that sikuli finds to it.

Example:
I run "Sikuli-IDE.exe test.skl"

the script does the following:

searches for an image
x = find("image")

and return the x and y coordinates to the command line
return x.get(X) + " - " + x.getY();

Question information

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

since script language is python syntax:
- no need for a ; at end of line
- getX(), getY(), ... return integers, so x.getX() + " - " will give an error
- return on main script level not allowed

this should run in IDE (first step: produce the return value):
ret = "-1 - 0" # not found
setThrowException(False) # script continues after not Found
x=find("path-to-an-image")
if x:
   ret = "%d - %d" % (x.getX(), x.getY()) # short for formatted printing
print ret # it shows up in message area

Alternative using exception handling:
try:
   x=find("path-to-an-image")
   ret = "%d - %d" % (x.getX(), x.getY()) # short for formatted printing
except FindFailed:
   ret = "-1 - 0" # not found
print ret # it shows up in message area

when exported as executable and run from command line, the print will produce the last line on stdout.

So if command line is a bash, we have an easy solution:
this works (Sorry, I'm a Mac, so you have to substitute how you start an .skl)
#!/bin/sh
retVal=`/Applications/Sikuli-IDE.app/Contents/MacOS/JavaApplicationStub ~/test.skl | tail -n -1`
echo $retVal

stdout of the .skl is piped into tail, which isolates the last line.
The variable retVal contains the return value, and you can do on the command line with it, what you want.

I guess in a windows cmd.exe environment, this will not work. But may be it helps you to get creative. I will check other options and come back if I have an idea for cmd.exe.

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

there seems to be a possibility with cmd.exe, that uses "for /F", to use the approach above. In the end you will have a variable that contains the returned information.
information on "for /F": http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?pf=true

I have not yet tested it on my Win7, but it seems to be possible.

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

I tested on Win7 and it works.
Some room for optimization (using 2 .bat).

in the script I changed the print to:
   ret = "### %d - %d" % (x.getX(), x.getY()) # short for formatted printing
to have a chance to filter the last line with find

1. .bat to run the .skl (my filename is sik_call.bat)
@echo off
"c:\Program Files\Sikuli\Sikuli-IDE.bat" test.skl | find "###"

2. .bat to set the variable retVal
@echo off
for /F "tokens=2,4" %%x in ('sik_call.bat') do @set retVal=%%x - %%y

calling second bat, you will find that after completion, the environment will show retVal=xxx - yyy.

have fun.

Revision history for this message
Peter Weller (gbk4life) said :
#4

Thanks RainMan, works great!