Jump to content

Script to open notepad not working properly different OS


Go to solution Solved by jdelaney,

Recommended Posts

Hi,

I have written a single script(to work on different OS/different language) to open run Notepad ,send some keys and close and save to some location. 

Below is the code:-

Run("notepad.exe")
$handle = WinWaitActive("[CLASS:Notepad]","")
WinSetState("[CLASS:Notepad]","",@SW_MAXIMIZE)
Send("Hello there")
WinClose($handle)
$handle = WinGetHandle("[ACTIVE]")
ControlClick($handle,"","[CLASS:Button; INSTANCE:1]")
$handle = WinWaitActive("[CLASS:#32770]","")
$hw = WinGetHandle("[ACTIVE]")
ControlSetText($hw, "", "ComboBox1", "c:\actual")
ControlSend($hw,"","ComboBox3","UTF-8")
ControlClick($hw,"","[CLASS:Button; INSTANCE:1]")

When i run the above script  2 out of 5 times it fails. I don't know whats wrong with this code.Most of the time it stops at SaveAs window where i am sending path to save the notepad.

Can you please review the code and help me .

Thanks!! 

Link to comment
Share on other sites

  • Solution

This should work most the times...I don't know what combobox 1 is...I don't have that on my app

#include <WinAPIex.au3>
#include <WinAPI.au3>
Global $iPID = Run ( 'notepad.exe' ) ; run program to create PID
Local $a = "" ; initalize array as empty
Local $iTimer = TimerInit() ; start timer
While Not IsArray($a) And TimerDiff($iTimer)<5000 ; If no array exists and timer is less than 5 secs
    $a = _WinAPI_EnumProcessWindows ($iPID,True) ; remake array with current windows
WEnd

If IsArray($a) Then
    Local $h = $a[1][0]
    WinSetState($h,"",@SW_MAXIMIZE)
    WinActivate($h)
    ControlSend($h, "", "Edit1", "Hello there")
    WinClose($h)

    $hPopup = ""
    Local $iTimer = TimerInit() ; start timer
    While Not IsHWnd($hPopup) And TimerDiff($iTimer)<5000 ; If no array exists and timer is less than 5 secs
        $hPopup = _WinAPI_GetWindow($h,6)
    WEnd

    If IsHWnd($hPopup) Then
        WinActivate($hPopup)
        ControlFocus($hPopup, "", "Button1")
        ControlClick($hPopup, "", "Button1")
        WinWaitClose($hPopup)


        $hPopup2 = ""
        Local $iTimer = TimerInit() ; start timer
        While Not IsHWnd($hPopup2) And TimerDiff($iTimer)<5000 ; If no array exists and timer is less than 5 secs
            $hPopup2 = _WinAPI_GetWindow($h,6)
        WEnd

        If IsHWnd($hPopup2) Then
            WinActivate($hPopup2)
            ControlFocus($hPopup2, "", "ComboBox1")
            ControlCommand($hPopup2, "", "ComboBox1", "SelectString", 'c:\actual')
            ControlFocus($hPopup2, "", "ComboBox3")
            ControlCommand($hPopup2, "", "ComboBox3", "SelectString", 'UTF-8')
            ControlFocus($hPopup2, "", 1)
            ControlClick($hPopup2, "", 1)
        Else
            MsgBox(1,1,"couldn't find second popup")
        EndIf

    Else
        MsgBox(1,1,"couldn't find popup")
    EndIf
Else
    MsgBox(1,1,"couldn't launch notepad")
EndIf

Notice, that I'm not just assuming the window becomes active, to get the handle.  I'm activly searching out the window, based on the pid, and the parent window's handle.

_WinAPI_GetWindow($h,6)...this function, with a '6' returns the 'enabled popup's handle of the parent window's handle...enabled popups are classes:#32770

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

  • Moderators

If you're writing only a couple of lines to a text file I would posit this would be easier:

FileWrite("C:\Temp\MyText.txt", "This is much faster")

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

 

If you're writing only a couple of lines to a text file I would posit this would be easier:

FileWrite("C:\Temp\MyText.txt", "This is much faster")

haha, I way overthought it.  This would def be the best way.

Although, I stick by my script, as far as manipulating windows based on logic, and not assumptions :).

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

This should work most the times...I don't know what combobox 1 is...I don't have that on my app

#include <WinAPIex.au3>
#include <WinAPI.au3>
Global $iPID = Run ( 'notepad.exe' ) ; run program to create PID
Local $a = "" ; initalize array as empty
Local $iTimer = TimerInit() ; start timer
While Not IsArray($a) And TimerDiff($iTimer)<5000 ; If no array exists and timer is less than 5 secs
    $a = _WinAPI_EnumProcessWindows ($iPID,True) ; remake array with current windows
WEnd

If IsArray($a) Then
    Local $h = $a[1][0]
    WinSetState($h,"",@SW_MAXIMIZE)
    WinActivate($h)
    ControlSend($h, "", "Edit1", "Hello there")
    WinClose($h)

    $hPopup = ""
    Local $iTimer = TimerInit() ; start timer
    While Not IsHWnd($hPopup) And TimerDiff($iTimer)<5000 ; If no array exists and timer is less than 5 secs
        $hPopup = _WinAPI_GetWindow($h,6)
    WEnd

    If IsHWnd($hPopup) Then
        WinActivate($hPopup)
        ControlFocus($hPopup, "", "Button1")
        ControlClick($hPopup, "", "Button1")
        WinWaitClose($hPopup)


        $hPopup2 = ""
        Local $iTimer = TimerInit() ; start timer
        While Not IsHWnd($hPopup2) And TimerDiff($iTimer)<5000 ; If no array exists and timer is less than 5 secs
            $hPopup2 = _WinAPI_GetWindow($h,6)
        WEnd

        If IsHWnd($hPopup2) Then
            WinActivate($hPopup2)
            ControlFocus($hPopup2, "", "ComboBox1")
            ControlCommand($hPopup2, "", "ComboBox1", "SelectString", 'c:\actual')
            ControlFocus($hPopup2, "", "ComboBox3")
            ControlCommand($hPopup2, "", "ComboBox3", "SelectString", 'UTF-8')
            ControlFocus($hPopup2, "", 1)
            ControlClick($hPopup2, "", 1)
        Else
            MsgBox(1,1,"couldn't find second popup")
        EndIf

    Else
        MsgBox(1,1,"couldn't find popup")
    EndIf
Else
    MsgBox(1,1,"couldn't launch notepad")
EndIf

Notice, that I'm not just assuming the window becomes active, to get the handle.  I'm activly searching out the window, based on the pid, and the parent window's handle.

_WinAPI_GetWindow($h,6)...this function, with a '6' returns the 'enabled popup's handle of the parent window's handle...enabled popups are classes:#32770

 

Thanks for your reply.It solved my problem.

BTW ComboBox1 is Edit1 (please consider as typo error)

Edited by shaktiku1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...