MSDOS in Ruby Console
-
I've hacked up a little bit of DOS that let's you change, make, remove and list contents of directories and delete files in the Ruby Console. It's useful as is, but I'm not happy that you have to put all the params in quotes:
cd '/working/dir' dir '*.skp' del 'xf*.jpg'
I'll be happy with it when the quotes are gone:
cd /working/dir dir *.skp del xf*.jpg
Any way to lose the quotes, short of writing a shell?
-
not in the ruby console. you're locked to the Ruby syntax.
Alternative would be to make a Webdialog console window that translates your DOS syntax into Ruby method calls. -
You could dump the quotes if you use an inputbox. Sort of like Start > Run on Windows.
-
@thomthom said:
not in the ruby console. you're locked to the Ruby syntax
And locked out of the Ruby Console? The string entered exists inside it as a string.
Jim, can I get this done in your WebConsole?
-
@martinrinehart said:
Jim, can I get this done in your WebConsole?
Sure, this is how I would do it:
Seriously, WebConsole either evals the contents of the textarea, or saves it to disk and loads the contents - I don't remember.
-
@martinrinehart said:
Any way to lose the quotes, short of writing a shell?
**** Warning! ****
Kernel.exec('cmd.exe')
will cause Sketchup to 'hang' on Win32.
When the shell is exited, BugSplat! closes Sketchup.
(This is becauseKernel.exec
replaces the currentProcess
, which is the interpreter process started by Sketchup.)So to use a DOS shell, instead use 1 of two choices:
DOS shell in a Sub Process
Kernel.system('cmd.exe')
which runs in a subprocess, and also returns a boolean value of success(TrueClass
) or failure(FalseClass
).
As a bonus, it can set a detailed error code in the Ruby global$?
.
And ugly side-effect however, is that Sketchup stops and waits while the shell is open. The Windows TaskManager reports that sketchup.exe is 'not responding', which is true; but will return to normal operation once the DOS shell is closed.
If the user types 'exit' in the shell,Kernel.system
returnstrue
with exitcode 0; if the user clicks the shell window close button thenKernel.system
returnsfalse
with exitcode 58.DOS shell in a Separate Process
UI.openURL('cmd.exe')
Simple and works because of the$SAFE
level is set to 0 by default. -
@martinrinehart said:
I've hacked up a little bit of DOS that let's you change, make, remove and list contents of directories and delete files in the Ruby Console.
What's wrong with the standard included classesFile
andDir
??
You can do all that would be desired with their methods.@martinrinehart said:
It's useful as is, but I'm not happy that you have to put all the params in quotes: ...
Any way to lose the quotes, short of writing a shell?
In many cases, since we migrated to 32bit Windows, we've had to put quotes around most pathnames or filenames that have spaces in DOS command scripts anyway... so it's not a big deal.
_ -
Martin, Dan, That's very nifty stuff. Thanks.
-
Stumbled on this. Didn't know it would happen, but it of course makes sense, because of the way MS Windows works.
If you wish to open a File Explorer window, to some given folder, use UI.openURL method.
# use empty quote quirk to get SU folderpath supath = Sketchup.find_support_file("") # open a File Explorer UI.openURL(supath)
_
-
@dan rathbun said:
Stumbled on this. Didn't know it would happen, but it of course makes sense, because of the way MS Windows works.
If you wish to open a File Explorer window, to some given folder, use UI.openURL method.
> # use empty quote quirk to get SU folderpath > supath = Sketchup.find_support_file("") > # open a File Explorer > UI.openURL(supath) >
_
UI.openURL will open/execute any filepath passed to it - if that's a folder it opens it in Windows-Explorer, if it's a file it opens with its default application, if it's an application/batch-file it runs, if it's a URL it opens in a web-browser [default app].
-
@tig said:
UI.openURL will open/execute any filepath passed to it - if that's a folder it opens it in Windows-Explorer, if it's a file it opens with its default application, if it's an application/batch-file it runs, if it's a URL it opens in a web-browser [default app].
I just tested safe levels with UI.openURL and the setting of $SAFE has no effect on the function of executables started by UI.openURL.
Could this be a security issue?
-
FYI: MSDOS Shell Default Font
It's a pain in the butt to get the Font set in the DOS shell.
Try changing the 'DefaultFont' registry setting in:
'HKEY_CURRENT_USER\Software\Microsoft\Command Processor'You may need to Log off and back on, before the setting takes effect.
There is also a list of fonts at:
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont' where the list numbers should begin at 0.
But I haven't had any luck getting the list to appear in the Console Properties dialog.(EDIT)By the way, the settings for the Console Properties dialog are saved at:
'HKEY_CURRENT_USER\Console' -
@dan rathbun said:
What's wrong with the standard included classes
File
andDir
??
You can do all that would be desired with their methods.That's all I've done:
def cd( path ) Dir::chdir( path ) end
-
@martinrinehart said:
@dan rathbun said:
What's wrong with the standard included classes
File
andDir
??
You can do all that would be desired with their methods.That's all I've done:
def cd( path ) Dir::chdir( path ) end
Shell Commands are already allowed within Ruby. [Examples for PC win32 platform, but also applies to Mac OSX.]
use the %x delimiter, as in: %x{dir *.skp}
or backquoted strings, as in:dir *.skp
(from: Programming Ruby -
The Pragmatic Programmer's Guide)The Ruby Language > Expressions > Single Terms
Shell Command. A shell command is a string enclosed in backquotes, or in a general delimited string (page 200) starting with %x. The value of the string is the standard output of running the command represented by the string under the host operating system's standard shell. The execution also sets the $? variable with the command's exit status.
[Example - '.c' changed to '.skp';'ls' changed to 'dir'.]
%(#BF0000)[filter = "*.skp"
files =dir #{filter}
files = %x{dir #{filter}}]! Backquoted strings allow replacement like doublequoted strings !
Expressions > Miscellaneous Expressions**http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html**
Command Expansion
If you enclose a string in backquotes, or use the delimited form prefixed by %x, it will (by default) be executed as a command by your underlying operating system. The value of the expression is the standard output of that command.! The output is a String, so any String method can be applied to the expression: Ex:
dir
.include?('.jpg')
returns true if there are any jpeg files in the dir
_
Advertisement