How work system command in ruby to launch Command line


#1

Hello all !
I’m coding a batch exporter for my use. I’m trying to resize jpg in batch with

system(‘mogrify -resample 72x72 -resize 256x256! /Users/Adebeo/Desktop/toto/*.jpg’)
false

or

system(‘mogrify’, ‘-resample 72x72 -resize 256x256! /Users/Adebeo/Desktop/toto/*.jpg’)

But its doesn’t work. I enter it in the Sketchup Ruby Console.
The following Command line in terminal is working

mogrify -resample 72x72 -resize 256x256! /Users/Adebeo/Desktop/toto/*.jpg

My question is simple. how system command works in ruby api?
Any idea or help ?

Thanks !


#2

The biggest issue with using subshell commands in the SketchUp Ruby API is figuring out what went wrong when they won’t run! Usually it is a problem with how quotes are being parsed, or perhaps in your case, with how wildcards in the arguments are passed through. Often it helps to put the command into a string and puts that string in the Ruby Console to see what you got.

The system() command works almost the same in the SketchUp Ruby API as from the command line Ruby: it returns true if the command was found and executed ok, false if the command was found but generated an error exit code, and nil if the command couldn’t be run at all (some references say it will raise an exception, but I haven’t see that happen). The system’s process exit code will be in $?. You can examine the return value and exit code to try to figure out what is wrong with your syntax. Sometimes it is obscure.

The biggest issue with using system() is that SketchUp’s Ruby API does not provide anywhere for text output from the command to go, so if the command tries to write to stdout or stderr the output will be lost.

One way to avoid this shortcoming is to use the backtick Ruby command or its alias %x{}. In this form, the command’s output is returned instead of true/false/nil. You can also use the popen command to hold a two-way conversation with the subshell - sometimes that helps to clarify things.

By the way, please fill out the rest of your profile so we know what OS and version of SketchUp you are using.


#3

@denis_bolomier

%x(mogrify -resample 72x72 -resize 256x256! /Users/Adebeo/Desktop/toto/*.jpg)

should work from ‘Ruby Console’ or a script, but have you tried just using sips instead?
it’s much quicker than ImageMagick’s mogrify command, but I don’t follow why you need the resample, so…

%x(sips -Z 256 /Users/Adebeo/Desktop/toto/*.jpg)

in Terminal use
man sips
to see the options for sips…

john


#4

Thanks you both !

I take and

command = "sips -z 256 256 #{colladaTextureDirectory}/*.jpg"
system(command)

and it’s works !

ps: I have 7 machines pc and pc with all the release of Sketchup . I have completed my profile