How do I automatically open the file I created? - ruby

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).

Related

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.

CMD doesn't wait to finish process

When i use command
<path exc.xlsx>
it starts excel file working and wait until i close the file than i can type next command. My issue is that when other excel file is running cmd doesn't wait to close the file and goes to next line. It is necessary to type the command that will force cmd to wait until i close excel file even when onother excel file is running. I tried a lot of commands from the internet but no one seems to work properly. These were for instance:
start /wait exc.xlsx && exit
exc.xlsx cmd /k
exc.xlsx|rem
I'm using Windows7.
I try this method but it sometimes doesn't work properly. The line <os.rename> try to change name of the file. If it can it means that the file is closed and break the loop but using that the script sometimes can't open the excel file. I'm not as advanced as i would be and cant find another way to check if the file is closed.
import os
def is_open(file_name):
if os.path.exists(file_name):
while True:
try:
os.rename(file_name, file_name)
break
except:
continue
time.sleep(0.001)
is_open('exc.xlsx')
That's not very dignified and uses a lot of processor. If there is a way using programing language (preferably python) I can implement that in my script.

How to simulate opening of .BAT file without one?

I want to launch several Perl scripts one after another (although, technically in parallel here) automatically.
It is easy to make a routine where the script makes a trivial .BAT file consisting only with the next Perl script, for example:
write_file("Z.bat", "$myfile_nextnumber.pl\n");
system "start Z.bat";
and you get nice output from each script in a separate window.
But how to achieve this without creating any file?
The closest what I got:
system "start cmd /k $myfile_nextnumber.pl";
but in this case, it does not show the path and file that was executed.
Why not just put a print statement before each system call?
foreach my $file (#filelist) {
print "Calling $file\n";
system "start cmd /k $file";
}

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

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.

How to automate Windows Run application using Ruby

I need to open Run from my Ruby script and type the location of a file and click OK. I have seen some examples to open notepad and entering text using WIN32OLE but I am not sure how to open the Run command.
If you are using Windows, I think you can do:
`start location_of_my_file`
You can do that with any of the following commands in ruby
1) exec
2) using backtick or %x
3) system
Along with the name of the file you should also give the name of the program that should execute it.
Ex: If you want to open calculator then you can just do
exec 'calc' # or `calc` or %x(calc) or system 'calc'
Ex: If you want to open a text file in notepad then :
exec 'notepad file_name.txt'
or
`notepad file_name.txt`
or
%x(notepad file_name.txt)
or
system 'notepad file_name.txt'
Here is one way that you can do it:
require 'win32ole'
def power
wsh = WIN32OLE.new('Wscript.Shell')
if not wsh.AppActivate('powershell')
wsh.Run('powershell')
sleep(3)
wsh.SendKeys('gwmi win32_bios{ENTER}')
wsh.SendKeys('gwmi win32_processor{ENTER}')
wsh.SendKeys('gwmi win32_volume{ENTER}')
wsh.SendKeys('ls{ENTER}')
wsh.SendKeys('ping 192.168.0.14{ENTER}')
wsh.SendKeys('exit')
end
end
power

Resources