How to avoid black-windows using system()?

Hi,

I’m just discovering Ruby, so, be indulgent!

When I use this code :

cmd = “d:\mes documents\___3D\imageMagick\montage.exe”
system( [cmd, cmd], “-background”, “#000000”, “-geometry”, “100%%”,
“1.jpg”, “2.jpg”, “3.jpg”)

A black-window like the jpg in attachment opens itself for half a
second.

Due to an iteration, this black-window opens itself dozen of times!

It’s not very professional.

Is there a way to avoid that with Ruby ?
Or must I absolutely look for that with the called-program ? (sort of
silent mode)

Thanks

Regards.

Rémi Cools wrote:

Hi,

I’m just discovering Ruby, so, be indulgent!

When I use this code :

cmd = “d:\mes documents\___3D\imageMagick\montage.exe”
system( [cmd, cmd], “-background”, “#000000”, “-geometry”, “100%%”,
“1.jpg”, “2.jpg”, “3.jpg”)

A black-window like the jpg in attachment opens itself for half a
second.

Due to an iteration, this black-window opens itself dozen of times!

It’s not very professional.

Is there a way to avoid that with Ruby ?

If you run it from ruby within a command prompt it seems to work.
Also appears if you use IO.popen it also works.

Roger P. wrote:

system( [cmd, cmd], “-background”, “#000000”, “-geometry”, “100%%”,
“1.jpg”, “2.jpg”, “3.jpg”)
A black-window like the jpg in attachment opens itself for half a
second.

system uses cmd to run the command, and cmd launches a virtual-DOS
environment. It’s possible (but not from Ruby, I think) to launch
cmd with no window - my team figured out how some years back - but
I’d have to search to get the details.

Is there a way to avoid that with Ruby ?

The only way I can think of is to change the SHELL environment
variable to designate a wrapper for cmd that sets the “no-window”
option.

Sorry it’s not a solution, but I thought the pointer might help.

Clifford H…

Hi Clifford H. & Roger P.,

thanks for you answers.

I don’t understand wath you say, perhaps I didn’t explain my problem
correctly.

I want to avoid that system() call cmd.exe

in :
system( [cmd, cmd], “-background”, “#000000”, “-geometry”, “100%%”
cmd is a String :
cmd = “d:\mes documents\___3D\imageMagick\montage.exe”
not the cmd.exe shell

I thought that when you call system() with only one parameter, cmd.exe
is used but when you call system() with more than one parameter, the
program is directly launched without cmd.exe

Can you confirm that ?

thanks & regards.

RC> I thought that when you call system() with only one parameter,
cmd.exe
RC> is used but when you call system() with more than one parameter, the
RC> program is directly launched without cmd.exe

RC> Can you confirm that ?

I think I know where the confusion lies.
from [1] it reads

" If exec is given a single argument, that argument is taken as a line
that is subject to shell expansion before being executed. If multiple
arguments are given, the second and subsequent arguments are passed as
parameters to command with no shell expansion"

The shell expansion it is referring to here is things like expanding
paths, ex:
system(“ls $HOME”) gets expanded to system(“ls /home/username”)

however

system(“ls”, “$HOME”)

does not get auto-expanded.

They both require “sub-shell” in windows because the process you are
executing (montage.exe) requires somewhere to output its data to, so it
pops up. It is possible to make a program “not require an output shell”
but most programs aren’t setup that way (rubyw.exe is, ruby.exe isn’t,
for example).

Recommendation: use IO.popen(“whatever”).read to avoid popping it up.
GL.
-r

[1] module Kernel - RDoc Documentation

RC> thanks & regards.

I’m not sure I follow you …

Have you tried the “back tick” processing to do what you want?
d:/mes documents/___3D/imageMagick/montage.exe

Rémi,

Thursday, July 22, 2010, 3:37:50 AM, you wrote:

RC> Hi Clifford H. & Roger P.,

RC> thanks for you answers.

RC> I don’t understand wath you say, perhaps I didn’t explain my problem
RC> correctly.

RC> I want to avoid that system() call cmd.exe

RC> in :
RC> system( [cmd, cmd], “-background”, “#000000”, “-geometry”,
“100%%”
RC> cmd is a String :
RC> cmd = “d:\mes documents\___3D\imageMagick\montage.exe”
RC> not the cmd.exe shell

RC> I thought that when you call system() with only one parameter,
cmd.exe
RC> is used but when you call system() with more than one parameter, the
RC> program is directly launched without cmd.exe

RC> Can you confirm that ?

RC> thanks & regards.

I’m not sure I follow you …

Have you tried the “back tick” processing to do what you want?
d:/mes documents/___3D/imageMagick/montage.exe

I think 1.9’s Process.spawn also works for this, since it inherits
parent stderr and stdout by default, AFAIK.

require ‘win32/process’
Process.create({:app_name => cmd, :startup_info=>
{:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW}})

I’ve put a few more thoughts [editable] here:

http://en.wikibooks.org/wiki/Ruby_Programming/Running_Multiple_Processes#Windows:_how_to_run_ruby_without_opening_up_a_command_window