Error running Windows Command Line commands within Ruby plugin

I am having trouble calling command line commands within a saved SketchUp model, and from certain computers.

I’m trying to send simple functions to the Windows Command Line from the Ruby API (as simple as ‘echo hello’). On some computers in unsaved SketchUp models, echo hello returns ‘hello’. On other computers, and in saved SketchUp models, I get no response at all.

Does anyone know how I need to alter my calls to the Windows Command Line so that it works in all models/on all computers?

You mean sending shell commands from SketchUp’s Ruby Console window ?

EDIT: It can also be done in a script, of course.

Normally this is done with a “shell string”, aka %x string, which leverages the Kernel#` method (aka Kernel “backquote” method.)

listing = `dir`


win_ver = %x{ver}.strip.split(/\s|\[|\]/).last

BUT, there was a bug in SketchUp 2014 (I think,) that was not fixed until v2015. The bugged Kernel backquote (and therefore %x shell strings,) did NOT return the results. The workaround was to redirect the command to a local temporary text file. Then read the textfile using Ruby’s File class.

Dir::chdir(some_path) {
  %x[dir > listing.txt]
  lines = File.readlines('listing.txt')

* Now that is just an example, it is far easier to just glob an array of files using the Ruby Dir class’ [] class method:

Dir::chdir(some_path) { listing = Dir["*.*"] }

Dan’s final point is apt: since SketchUp started including the Ruby library in v2014, it is always better to use a Ruby library function when available instead of invoking a system command. You are far less likely to run into strange debugging issues.

Actually, it must have been fixed in v14.1 (MR1) because it works without my “backquote_patch” file. (And the bug only manifested on MS Windows.)

I’ve re-written the old patch script (I wrote at the time v2014 was released,) as a mixin module that can be mixed conditionally into an author’s plugin sub-module(s).

See topic: [code] Backquote method patch as Mixin module

But it seems that in SU2018 we are still having this issue… nil returns from system commands.

Answered in other thread …