Unable to write in notepad

Discussion in 'Ruby' started by Pranjal Jain, May 22, 2008.

  1. Pranjal Jain

    Pranjal Jain Guest

    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 :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Pranjal Jain, May 22, 2008
    #1
    1. Advertising

  2. Pranjal Jain

    Heesob Park Guest

    Hi,

    2008/5/22 Pranjal Jain <>:
    > 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
     
    Heesob Park, May 22, 2008
    #2
    1. Advertising

  3. Pranjal Jain

    Pranjal Jain Guest

    Hi
    I am sorry
    it worked for me sooner after I posted the question.I had used the same
    code which u mentioned.

    But there is another problem I am facing.
    For running the the script the notepad should be opened before I execute
    the script.

    I dnt want to keep any notepad open before I start running the script.
    Can u suggest how I can overcome this problem


    Heesob Park wrote:
    > Hi,
    >
    > 2008/5/22 Pranjal Jain <>:
    >> "
    >> wsh.SendKeys('!F')
    >> end
    >> 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Pranjal Jain, May 22, 2008
    #3
  4. Pranjal Jain

    Heesob Park Guest

    Hi,

    2008/5/22 Pranjal Jain <>:
    > Hi
    > I am sorry
    > it worked for me sooner after I posted the question.I had used the same
    > code which u mentioned.
    >
    > But there is another problem I am facing.
    > For running the the script the notepad should be opened before I execute
    > the script.
    >
    > I dnt want to keep any notepad open before I start running the script.
    > Can u suggest how I can overcome this problem
    >

    I guess you did not try my code.
    Following code is included for the opening notepad:

    if not wsh.AppActivate('Notepad')
    wsh.Run('Notepad')
    sleep(1)
    end

    Regards,

    Park Heesob
     
    Heesob Park, May 22, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JDS
    Replies:
    4
    Views:
    8,379
    ForestHonerkamp
    Nov 11, 2008
  2. Tim S.

    Call Notepad from aspx

    Tim S., Jul 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    904
    Troy Murphy
    Jul 14, 2003
  3. Steve C. Orr, MCSD

    Re: Call Notepad from aspx

    Steve C. Orr, MCSD, Jul 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    1,794
    Yan-Hong Huang[MSFT]
    Jul 16, 2003
  4. timmso
    Replies:
    1
    Views:
    573
    Alex Papadimoulis
    Dec 12, 2003
  5. Anukul Singhal
    Replies:
    3
    Views:
    176
Loading...

Share This Page