Forum: Ruby How to avoid black-windows using system() ?

Posted by Rémi Cools (beton04f)
on 2010-07-21 01:54
Attachment: BLACK.jpg (43,4 KB)
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.
Posted by Roger Pack (rogerdpack)
on 2010-07-21 20:28
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.
Posted by Clifford Heath (Guest)
on 2010-07-22 02:30
(Received via mailing list)
Roger Pack 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 Heath.
Posted by Rémi Cools (beton04f)
on 2010-07-22 11:34
Hi Clifford Heath & Roger Pack,

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.



Posted by Ralph Shnelvar (ralphshnelvar)
on 2010-07-22 20:22
(Received via mailing list)
Rémi,


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

RC> Hi Clifford Heath & Roger Pack,

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`
Posted by Roger Pack (rogerdpack)
on 2010-07-22 21:37
> 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] http://ruby-doc.org/core/classes/Kernel.html#M005968

> 
> 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`
Posted by adam hegge (Guest)
on 2010-07-22 22:02
(Received via mailing list)
require 'win32/process'
Process.create({:app_name => cmd,  :startup_info=>
{:sw_flags=>Process::SW_HIDE , :startf_flags =>
Process::STARTF_USESHOWWINDOW}})
Posted by Roger Pack (rogerdpack)
on 2011-05-04 11:23
I think 1.9's Process.spawn also works for this, since it inherits 
parent stderr and stdout by default, AFAIK.
Posted by Roger Pack (rogerdpack)
on 2011-05-05 14:54
I've put a few more thoughts [editable] here:

http://en.wikibooks.org/wiki/Ruby_Programming/Runn...
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.