How to run a batch file in foreground using sikuli

Asked by Rubynator

Okay, I can use the sikuli integrated method App.open to execute external commands but when I try to do the same thing for batch (bat) file (WINDOWS) according to the LOGs It executes the bat file in the background fine. Problem is my bat file is highly visual and I want it to run in the foreground so I can see it telling me the steps and percentages as it's going thru.
Just like executing the bat file manually.
So I run App.open(C:\sikuli\something.bat)
I get [log] App.open something.bat(5844)

Which is a success, it runs fine, but all happens in the background and I don't see the CMD windows popping up and showing me all the steps of the process. That's what I need.
I tried subprocess as well, It executes the the bat file in the background, and displays the results in the MSG screen, not in a separate standalone CMD window.
Is there any parameters to the App object or how do I do it?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
obiwan-92
Solved:
Last query:
Last reply:
Revision history for this message
Best obiwan-92 (obiwan-92) said :
#1

Hello.

Use this :
def exeCmd(title, cmd):
    App.open('CMD /k start \"'+title+'\" '+cmd)

exeCmd("yourCMDtitle", "C:\sikuli\something.bat")

Regards

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

Take care:

exeCmd("yourCMDtitle", "C:\\sikuli\\something.bat")

or:

exeCmd("yourCMDtitle", r"C:\sikuli\something.bat")

Revision history for this message
obiwan-92 (obiwan-92) said :
#3

Yes, sorry.
I'm not perfectly awake, this morning. ;)

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

@obiwan
Ok, then today I had some headstart with 3 portions of coffee ;-)
All the best and great thanks for your valuable contributions.

Revision history for this message
Rubynator (datagatepdx) said :
#5

This works flawlessly, Thank you obiwan-92 and RaiMan.
I'm new to Sikuli, assuming this is a chunk of python code. Correct?
For a better understanding can you explain the above code step by step.

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

the key to success is the statement:

App.open( 'CMD /k start \"' + title + '\" ' + cmd )

which could be written also as:

App.open( 'CMD /k start "' + title + '" ' + cmd)

App.open(text_parameter) is the used function, which goes down to the system and advises to run the program given as text_parameter (something as you might write it on the command line).

so for your case, as a one time usage you could have written: C:\sikuli\something.bat

App.open( 'CMD /k start "Running something.bat" "C:\sikuli\something.bat" ')

The special setting obiwan used, was only to make it reusable.

the call to App.open() is put in a function, that can now be used from everywhere, by simply giving the 2 variables as strings when calling the function, so with

App.open( 'CMD /k start "' + title + '" ' + cmd)
the given parameters are assembled with the fixed parts to the final command to be used.
So in this case the + is used as the concatenation operator for strings.

A word about quoting (using apostrophes):
in Python there are basically 2 quoting characters: " double quote (DQ) and ' single quote (SQ)
if in a string you need as part of the text one of these quoting characters, then you either have to use the other as string delimiter or you have to escape the quote with a \ backslash (what obi wan has done, though not necessary in this case).
all this works:
1. SQDQSQ
2. DQSQDQ
3. DQSQ\DQSQDQ
4. SQ\SQDQ\SQSQ

SQ and DQ are used here only for better readability, replace with ' and "

My solution would have used the % operator, that allows string formatting using variables and I would suggest, to quote the given command too, since this might contain blanks too:

App.open( 'CMD /k start "%s" "%s"' % (title, cmd))

the %s are placeholders, that are replaced with the given variables after the % in a list () from left to right.
the advantage: the %x placeholders have some more options, that allows to pimp up the final string.

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

uuuups, made the same mistake again:

App.open(r 'CMD /k start "Running something.bat" "C:\sikuli\something.bat" ')

the contained \ have either to be escaped as \\ or the string has to be set as raw string with r'...'

Revision history for this message
obiwan-92 (obiwan-92) said :
#8

Hello,

@RaiMan
Don't you take your coffee this morning ? ;-)

@Rubynator
The explanation is pretty clear about the Python part.

About the Batch part :
CMD /k start "myTitle" "C:\sikuli\something.bat"
launch a new cmd windows with the title myTitle and execute the something.bat command.

cmd : shortcut to execute a batch command
/K : parameter of cmd command to maintain the window after executed the command (cmd /? to have more info)
Start : launch the window

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

@ obiwan
some shit happens having had coffee or not ;-)
have a nice day.

Revision history for this message
Rubynator (datagatepdx) said :
#10

Thanks obiwan-92, that solved my question.

Revision history for this message
Abilashkar (abilashkar) said :
#11

How in Java to open a bat file ?