Heesob Park
5/22/2008 8:47:00 AM
Hi,
2008/5/22 Pranjal Jain <pranjal.jain123@gmail.com>:
> Hi All
> I am trying to write the data in the notepad. But I the data going in it
> is wrong. The output which I am getting in the notepad is
>
>
>
> !FA
>
> The code I used is as follows:
>
> "
> # Require the win32ole library:
> require 'win32ole'
> # Create an instance of the Wscript Shell:
> wsh = WIN32OLE.new('Wscript.Shell')
> # Try to activate the Notepad window:
> if wsh.AppActivate('Notepad')
> sleep(1)
> # Enter text into Notepad:
> wsh.SendKeys('Ruby{TAB}on{TAB}Windows{ENTER}')
> # ALT-F to pull down File menu, then A to select Save As...:
> wsh.SendKeys('!F')
> wsh.SendKeys('A')
> sleep(1)
> if wsh.AppActivate('Save As')
> wsh.SendKeys('c:\temp\filename.txt{ENTER}')
> sleep(1)
> # If prompted to overwrite existing file:
> if wsh.AppActivate('Save As')
> # Enter 'Y':
> wsh.SendKeys('Y')
> end
> end
> # Quit Notepad with ALT-F4:
> wsh.SendKeys('%{F4}')
> end
>
> "
>
> One more interesting thing is that here I am able to write the value in
> the notepad which is already opened. if the notepad is not opened, then
> ruby just intepret the script & does not write the output.
>
> Please suggest the solution for this.
> Thankx in advance :)
Here is a working code:
# Require the win32ole library:
require 'win32ole'
# Create an instance of the Wscript Shell:
wsh = WIN32OLE.new('Wscript.Shell')
if not wsh.AppActivate('Notepad')
wsh.Run('Notepad')
sleep(1)
end
# Try to activate the Notepad window:
if wsh.AppActivate(Notepad)
sleep(1)
# Enter text into Notepad:
wsh.SendKeys('Ruby{TAB}on{TAB}Windows{ENTER}')
# ALT-F to pull down File menu, then A to select Save As...:
wsh.SendKeys('%FA')
sleep(1)
wsh.SendKeys('c:\temp\filename.txt{ENTER}')
sleep(1)
# If prompted to overwrite existing file:
if wsh.AppActivate('Save As')
# Enter 'Y':
wsh.SendKeys('Y')
end
# Quit Notepad with ALT-F4:
wsh.SendKeys('%{F4}')
end
Regards,
Park Heesob