Ruby -- Get User Input outside of the Command Line using Ocra - ruby

So I have a very simple single file script:
puts "Enter the file name"
file = gets.chomp
puts "What do you want to replace it with?"
replace = gets.chomp
which then changes some files with the user-inputs. I packaged it up with Ocra, but I was hoping it would open up the command line when it was run and ask for the user inputs or something, or a pop-up window maybe. I need this to be very simple since my users won't know to go to the command line and run the .exe from there with arguments, so is there a way to to get a window to pop-up that takes in user input every time the .exe file is run? I've tried it in both .rb and .rbw formats.

cmd (or maybe it's cmd.exe) should work on Windows. It shells out to the cmd command which should launch a cmd window.

Related

Is there a command in Shell scripting for executing .exe file and running commands automatically inside of it? replacing the user interaction

I have a .sh script file that I'm modifying which runs an .EXE file that opens the Windows command line prompt automatically.
This .exe asks the user for an input (name of the file in the folder workspace that it will read)
I want to automate this step in my shell script so my user doesn't have to interact with this, and run the commands automatically
I read a bit about the expect command but I think that is for Linux only.
Can someone help me, I'm pretty new to Shell scripting and I couldn't find any useful information elsewhere.
I'm assuming that your executable accepts command-line arguments. So, here we go.
You can use the "start" command in Windows Shell. For example:
start C:\path\to\program.exe -argument
If you want to make the script wait until the .exe file finishes running before continuing, you can use the "/wait" command:
start /wait C:\path\to\program.exe -argument
IF all of that doesn't work, please try:
start myprogram.exe /command1 /command2 /command3
Hope it helps,

DOORS make console the interactive window

Is there any way to tell DOORS to use the current command prompt window as the interactive window when executing in batch mode?
For example, if I have hello.dxl which looks like
print("Hello world")
and Run.bat which looks like
"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl -W
It currently opens a new window, prints "Hello World" and then closes the window (it closes it because of the -W). Is there any way to redirect this output to the command prompt window that was opened to run the batch file?
There is no console variant of doors.exe and as far as I know there is no possibility to give a sort of handle to a specific prompt window and use e.g. OLE Automation to print to this window, so, basically, no, it's not possible.
A workaround that we use for this requirement is to have a batch file which
generates the name to a temporary file ,
passes this file to DOORS as a parameter (using environment variables)
make DOORS/DXL cout to this file
after the DXL has finished, type the content of the temporary file in the calling batch and optionally delete it.
PS: according to https://www.ibm.com/mysupport/s/question/0D50z00006HIM4oCAH/doors-print-redirect-tutorial-for-print-cout-and-logfiles it apparently used to be possible to redirect STDOUT/STDERR to a specific file, but not in recent DOORS versions.

Running keystrokes from program

I'm writing a program to automate an e-mail process and would like to know if there's a way to run keystrokes from within the program?
For example say I have this string:
str = "test"
And it gets copied to a file:
File.open('str.txt', 'w') { |s| s.puts(str }
And after that I want to use CNTRL-A; CNTRL-C on the file and copy the information, is this possible in a Ruby program, without the use of external gems?
Operating system: Windows 7
If sending arbitrary keystrokes to other applications is what you're after you can use the gem https://github.com/erinata/auto_click for it. However, if you can't use gems, what you can do instead is run NirCmd (or one of its alternatives) with the appropriate command line arguments to achieve the same result.
For example:
# Tell the OS to bring up the Notepad window and give it the time
# to do so.
`nircmd win activate ititle notepad`
sleep 0.5
# Select all text in the Notepad window and copy it to the
# clipboard.
`nircmd sendkeypress ctrl+a`
`nircmd sendkeypress ctrl+c`
If you can't install gems but can copy ClipText.exe to your current directory, do so then run this code in Ruby:
File.open('str.txt', 'w') { |s| s.puts(str }
`cliptext.exe from str.txt`
For a more rigorous way of executing commands on Windows see "How to execute Windows CLI commands in Ruby?".

How do I automatically open the file I created?

I create a file when I run my Ruby script. How would I set the script to open the file with a default editor or a text editor once the script is finished?
For instance, I create a file called "FooBar.txt" that gets a bunch of information loaded into it. How can I open that up afterwards?
I am sure this is really simple, but every time I search for it all it comes up with is opening a file to add text or edit in the script. I'm not actually opening the file with a program.
You can make a system call.
This is an example with Windows:
filename = "the_list.txt"
File.open(filename, "w"){|file|
file << "Some data\n"
}
`call notepad #{filename}`
This calls Notepad with the given filename.
Some variants to call an external program are:
`notepad #{filename}`
system( "notepad #{filename}")
system( "call notepad #{filename}")
%x{call notepad #{filename}}
You even don't need to add notepad:
%x{call #{filename}}
This depend on the main application, which is assigned to the extension of the file you create.
When you tell which system and which editor you need, more details are possible.
Another possibility:
require 'open3'
Open3.popen3("call #{filename}")
#or:
#Open3.popen3("call notepad #{filename}")
The advantage is the main program does not wait until the subprocess ends.
Variant as script: Store the following code as "file_build.rb".
filename = ARGV.first
File.open(filename, "w"){|file|
file << "Some data\n"
}
require 'open3'
puts "Call Editor"
Open3.popen3("call notepad #{filename}")
puts "End of script"
Now you can call file_build.rb test.txt. test.txt is created, an editor is called and the script closes. The editor keeps running, at least it did in my test (WinXP).

How to open a command prompt window in ruby script and have an interactive session

I have a requirement where i need to open command prompt and run a bat file, this bat file will ask for the user to enter a choice 2 to 3 times, how to automate this in ruby in windows.
Am able to open command prompt using
system("start cmd.exe")
After this I need to change directory and then i need to run the file present in c://temp//dat.bat, through ruby script.
Please let me know how to automate all these operations.
cmd_line = "cmd.exe /c \"cd #{directory}&&#{bat_file}\""
system(cmd_line) # if you're not interested in the output
Should do it.

Resources