← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #242941]: How to run a batch file in foreground using sikuli

 

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

RaiMan proposed the following answer:
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.

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