Running keystrokes from program - ruby

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?".

Related

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

How to get files and directories set up for Ruby?

I am doing prep work for App Academy but I am having a hard time setting up files/directories to be able to run everything correctly. I have a couple questions and haven't been able to find concrete answers:
How do you make a directory in the windows terminal?
How do you run files previously written in Notepad in IRB? I wrote some simple scripts in Notepad because Sublime was causing me severe migraines. Or how do you change Notepad files to Ruby files?
How do you create new files in IRB like test_code.rb?
1) When at the Windows command prompt and having navigated to your working directory, type md directory_name
2) Unless you have specifically told notepad otherwise, notepad will have saved your file as a .txt file. You will simply have to rename the file extension from *.txt to *.rb.
3) When you installed Ruby on your Windows machine, the Ruby interpreter would have undoubtably been added to your path, so you should just be able to run your *.rb file direct from the windows cmd prompt and it will execute.
To load it while in IRB: Make sure you run IRB from the same folder as your *.rb file is in. Once you have cranked up an IRB session, type load 'my_file.rb'.
IRB is a great environment for testing code, but not for writing full scripts. Use Notepad or Notepad++ or Vim for Windows or your editor of choice, as long as it's capable of generating a text (non word processing document).
You can make a directory in the terminal or in the Explorer, it doesn't matter. Just note where you created it so you don't lose it.
If you want to run a script in Ruby, simply type ruby /path/to/the/file/script_to_run.rb and the Ruby interpreter should load and run the file.
You can load a script into IRB and watch it run, but that's rarely something we need to do. More often you'll want to run scripts using Ruby, and try things in IRB, since it's like a scratchpad.

how to make my console in python not to close?

I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.
I don't know why this is happening. I need your help.
Run the program from an already-open terminal. Open a command prompt and type:
python myscript.py
For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...
raw_input()
... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.
Run your program from a Windows command prompt. That will not automatically close when the program finishes.
If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).
Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:
python main.exe
pause
Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.

Running cmd scripts from a ruby file?

Is there anyway to write commands to the command prompt in windows and execute directly from a ruby program?
I would use this as a one click installer for all the gems I wanted to install on the computer after installing ruby. I hope that it would save time when transferring my ruby files to a new computer. Or would be an easier way to get a non-ruby person set up very quickly with all the gems I thought them might need.
I am imagining something like Watir but for the Cmd rather than a browser.
EDIT
Thanks to
How can I then close the cmd window without closing the program for instance:
'notepad'
starts a cmd window and it also starts notepad but the cmd windows stays until the notepad is closed.
Ruby will execute anything you put in backticks ` in your associated shell.
so if you type
test = `ipconfig`
puts test
test should now have stored in it the data from the cmd call ipconfig
EDIT
You can also use the System(..) call in Ruby to execute commands

Resources