Starting a process in background using Ruby system()
-
I couldn't find anything else about this in the Forum, so I thought I would pass it along.
In previous versions of SketchUp, we used Win32API to start processes.
We changed all these to use the Ruby system() command for SU 2014.
Sometime we want to start a process where ruby does not wait for it to complete.
You can do this with the DOS start command:
system('start ' + scommand_line)
The the program in the command line will execute and ruby will continue without waiting for it. (Note the command line contains the name of the .exe and also some command arguments)
However, due to quirk with "start", this fails if the command line starts with double quotes. I found this comment on the web:
"START has a peculiarity involving double quotes around the first parameter. If the first parameter has double quotes it uses that as the optional TITLE for the new window. "
So, just adding a blank set of double quotes fixed our problem. This works whether the command line actually starts with a double quote or not
system('start "" ' + scommand_line)
-
Wow, that's one of them thing that could have you stuck for hours. Thanks for sharing!
-
... but it is PC only correct ?
-
How do you handle folder name with spaces?
cmd = "c:\program files\..."
system('start "" ' + cmd)
produces an error message which says that Windows cannot find 'c:\program'
-
given:
cmd = "c:\\program files\\..."
one of::
system( "start \"#{cmd}\"" )
or:
system( %[start "#{cmd}"] )
or:
system( "start", %["#{cmd}"] )
or:
%x[start "#{cmd}"]
See Ruby docs on literal strings:
http://ruby-doc.org/core-2.0.0/doc/syntax/literals_rdoc.html#label-Strings -
@dan rathbun said:
given:
cmd = "c:\\program files\\..."
one of::
system( "start \"#{cmd}\"" )
or:
system( %[start "#{cmd}"] )
or:
system( "start", %["#{cmd}"] )
or:
%x[start "#{cmd}"]
See Ruby docs on literal strings:
http://ruby-doc.org/core-2.0.0/doc/syntax/literals_rdoc.html#label-StringsThanks Dan. Each of these options do not fail but they do not succeed either. What I was trying to do is create a plugin that would allow me to pick a plugin from a list and create a .rbz file from it using the command line version of 7Zip. This is the final solution
zip = "c;/program files/7-zip/7z"; rbz = "c;/users/public/sketchup/plugins/dummy tool.rbz"; rb = "c;/users/public/sketchup/plugins/dummy tool.rb" cmd = "\"#{zip}\" a -tzip \"#{rbz}\" \"#{rb}\""; puts cmd #system( "start \"#{cmd}\"" ) #system( %[start "#{cmd}"] ) #system( "start", %["#{cmd}"] ) #system( %x[start "#{cmd}"] ) system('start "" ' + cmd)
-
some zip program require the .zip extension, so you could try using rename...
I'm on a mac, but maybe something like...
zip = "c;/program files/7-zip/7z" zipped = "c;/users/public/sketchup/plugins/dummy tool.zip" rbz = "c;/users/public/sketchup/plugins/dummy tool.rbz" rb = "c;/users/public/sketchup/plugins/dummy tool.rb" cmd = "\"#{zip}\" a -tzip \"#{zipped}\" \"#{rb}\""; puts cmd %x('start "" ' + cmd) File.rename(zipped, rbz)
john
-
@driven said:
some zip program require the .zip extension
john
Not a problem with 7-Zip.
The -tzip sets the output format.
Advertisement