Sign in to follow this  
Followers 0
AndyS01

WinMenuSelectItem() fails

6 posts in this topic

#1 ·  Posted

I have a script that opens an editor (Notepad) and inserts text into its window, but the text insertion fails.  The WinActivate() does not always give focus to the newly loaded Notepad and the WinMenuSelectItem() fails.

Here is my test file:
 

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=THTracker.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_UseX64=N
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

_Main()

Func _Main()
    add2editor("Line1" & @CRLF & "Line2" & @CRLF & "Line3" & @CRLF)
EndFunc   ;==>_Main

Func add2editor($sOutput)
    Local $cmd, $hWnd, $ret, $title, $loopcnt, $maxcnt = 10

    $cmd = "C:\windows\system32\notepad.exe"

    ConsoleWrite("Start the editor" & @CRLF)
    Run($cmd)

    $title = "Notepad"

    ConsoleWrite("Wait til the editor gui exists" & @CRLF)
    $loopcnt = 0
    While (1)
        $ret = WinWait($title, "", 3)
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] $ret = " & $ret & @CRLF)
        $hWnd = $ret
        If ($ret <> 0) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

    MsgBox(0, "A-" & @ScriptLineNumber, Hex($hWnd))

    ConsoleWrite("Give focus to the editor" & @CRLF)
    $loopcnt = 0
    While (1)
        $ret = WinActivate($hWnd)
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] @error = " & @error & ", $ret = " & $ret & @CRLF)
        $hWnd = $ret
        If ($ret <> 0) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

    MsgBox(0, "B-" & @ScriptLineNumber, Hex($hWnd))

    ConsoleWrite("Do a File -> New" & @CRLF)
    $loopcnt = 0
    While (1)
        ;
        $ret = WinMenuSelectItem($hWnd, "", "&File", "New")
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] @error = " & @error & ", $ret = " & $ret & @CRLF)
        If ($ret = 1) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

    MsgBox(0, "C-" & @ScriptLineNumber, Hex($hWnd))

;Local $str = ClipGet() ; save whatever is in the clipboard

    Send("^a") ; select all existing text in the editor's window

    ConsoleWrite("put the new text into the clipboard" & @CRLF)
    ClipPut($sOutput)

    Send("^v") ; paste it into the editor's window

    ;;;ClipPut($str) ; restore the previous contents of the clipboard

EndFunc   ;==>add2editor

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try with

WinMenuSelectItem($hWnd, "", "&File", "&New")

There is also n o need for WinActivate().

Edited by ahmet

Share this post


Link to post
Share on other sites

#3 ·  Posted

That made it work once in a while, but not every time.  Change the code to run the SciTE editor (I assume that's what you're running the test with) and exit before doing the ClipPut().  This should open a new tab (File -> New), but it doesn't.  It fails every time.  What I've given you is a stripped down version of me actual script.  In the full script, I'm opening an editor called LopeEdit which has a tabbed interface like the SciTE editor.

Here is the test code (with WinActivate and the MsgBox's removed) that opens the SciTE editor instead of Notepad:

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=THTracker.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_UseX64=N
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

_Main()

Func _Main()
    add2editor("Line1" & @CRLF & "Line2" & @CRLF & "Line3" & @CRLF)
EndFunc   ;==>_Main

Func add2editor($sOutput)
    Local $cmd, $hWnd, $ret, $title, $loopcnt, $maxcnt = 10

$cmd = "C:\Program Files\AutoIt3\SciTE\SciTE.exe"

    ConsoleWrite("Start the editor" & @CRLF)
    Run($cmd)

    $title = "Notepad"

    ConsoleWrite("Wait til the editor gui exists" & @CRLF)
    $loopcnt = 0
    While (1)
        $ret = WinWait($title, "", 3)
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] $ret = " & $ret & @CRLF)
        $hWnd = $ret
        If ($ret <> 0) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

    ConsoleWrite("Do a File -> New" & @CRLF)
    $loopcnt = 0
    While (1)
        ;
        $ret = WinMenuSelectItem($hWnd, "", "&File", "&New")
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] @error = " & @error & ", $ret = " & $ret & @CRLF)
        If ($ret = 1) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

Exit ;<======================== !!!

    Send("^a") ; select all existing text in the editor's window

    ConsoleWrite("put the new text into the clipboard" & @CRLF)
    ClipPut($sOutput)

    Send("^v") ; paste it into the editor's window

EndFunc   ;==>add2editor

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

Run some tests with this

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=THTracker.ico
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_UseX64=N
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GuiMenu.au3>



;Opt('MustDeclareVars', 1)
Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

_Main()

Func _Main()
    add2editor("Line1" & @CRLF & "Line2" & @CRLF & "Line3" & @CRLF)
EndFunc   ;==>_Main

Func add2editor($sOutput)
    Local $cmd, $hWnd, $ret, $title, $loopcnt, $maxcnt = 10
    Local $hMenu, $iCnt, $sMsg
    $cmd = "notepad.exe"

    ConsoleWrite("Start the editor" & @CRLF)
    ;Run($cmd)

    $title = "- SciTE"

    ConsoleWrite("Wait til the editor gui exists" & @CRLF)
    $loopcnt = 0
    While (1)
        $ret = WinWait($title, "", 3)
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] $ret = " & $ret & @CRLF)
        $hWnd = $ret
        If ($ret <> 0) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

    ;MsgBox(0, "A-" & @ScriptLineNumber, Hex($hWnd))

    ConsoleWrite("Give focus to the editor" & @CRLF)
    $loopcnt = 0
    While (1)
        ExitLoop
        $ret = WinActivate($hWnd)
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] @error = " & @error & ", $ret = " & $ret & @CRLF)
        $hWnd = $ret
        If ($ret <> 0) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd
    MsgBox(0, "Window title", WinGetTitle($hWnd))
    ;MsgBox(0, "B-" & @ScriptLineNumber, Hex($hWnd))
    $hMenu = _GUICtrlMenu_GetMenu($hWnd)
    If $hMenu = 0 Then Exit MsgBox(0, "Error", "No menu")
    $hMenu = _GUICtrlMenu_GetItemSubMenu($hMenu, 0)
    $iCnt = _GUICtrlMenu_GetItemCount($hMenu)
    $sMsg = "There are " & $iCnt & " items in the menu:" & @CRLF
    For $n = 0 To $iCnt - 1
        $sMsg &= @TAB & $n & ": " & _GUICtrlMenu_GetItemText($hMenu, $n) & @CRLF
    Next
    MsgBox(64, "Menu: FIle", $sMsg)
    ConsoleWrite($sMsg)
    ConsoleWrite("Do a File -> New" & @CRLF)
    $loopcnt = 0
    While (1)
        ;
        ;WinWaitActive($hWnd)
        $ret = WinMenuSelectItem($hWnd, "", "&File", "&New")
        ConsoleWrite("+++:" & @ScriptLineNumber & ":[" & $loopcnt & "] @error = " & @error & ", $ret = " & $ret & @CRLF)
        If ($ret = 1) Then ExitLoop
        $loopcnt += 1
        If ($loopcnt > $maxcnt) Then Exit
        Sleep(125)
    WEnd

    ;MsgBox(0, "C-" & @ScriptLineNumber, Hex($hWnd))

    ;Local $str = ClipGet() ; save whatever is in the clipboard
    WinActivate($hWnd)
    WinWaitActive($hWnd)
    Send("^a") ; select all existing text in the editor's window

    ConsoleWrite("put the new text into the clipboard" & @CRLF)
    ClipPut($sOutput)

    Send("^v") ; paste it into the editor's window

    ;;;ClipPut($str) ; restore the previous contents of the clipboard

EndFunc   ;==>add2editor

 

Share this post


Link to post
Share on other sites

That worked for me.  Thanks

Share this post


Link to post
Share on other sites

Glad I could help.

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • Rhazz
      I can't activate the "File Open" window on Chrome and Edge
      By Rhazz
      Hi, I'm trying to upload a file with a web browser but I can't select the Open File window to select the file that I want to upload.
      TL;DR I can't handle the Open File window of Chrome and MS Edge. AutoIt just doesn't find it by its title (in Firefox yes, but not on Chrome and MS Edge).
      With another software (Selenium Webdriver in Python) I access to a web page with a common HTML5 file uploader and I click on it, but when the Open File window comes up, I want to select a file and press Enter (write the filepath that I give and press Enter key or Open button).
      I have the idea, and I have the script on AutoIt too... but it doesn't work on Windows Edge and Chrome, only works on Firefox and Pale Moon (who are basically the same).
      Here is my code:
      #include <MsgBoxConstants.au3> $title = "Abrir" ; My Windows is in spanish, but I guess it's "Open" in English and it's the same code WinActivate($title) If WinActive($title) Then send("C:\Users\myuser\images\my_image.jpg") Send("{ENTER}") Else MsgBox($MB_SYSTEMMODAL, "WinActive", "Window not found.") EndIf In Firefox (and Pale Moon) the Open File window comes up, the file path is written and the Enter key is pressed. It works perfect.
      In Chrome and Edge, I never see the Open File window on top... and I neither see "Window not found" message (seems like AutoIt finds it but doesn't send keys).
      Edit: before to send this post, I started thinking again and... after several tests, I discovered one thing: when you select the window, in Firefox (and Pale Moon) the cursor goes to the combobox (where I have to put the text) and it's possible to write just after clicking (or opening, what in AutoIt it is "Activate" I guess) the window but in Chrome and Edge it isn't like that, when you click the window, the cursor doesn't go to the combobox.
      With that "discovery", I tried to select the combobox but... I failed again. And now I have no idea how to go ahead.
      #include <MsgBoxConstants.au3> $title = "Abrir" WinActivate($title) If WinActive($title) Then ControlClick($title, "", 1148) ; Still it does not work send("C:\Users\myuser\images\my_image.jpg") Send("{ENTER}") Else MsgBox($MB_SYSTEMMODAL, "WinActive", "Window not found.") EndIf  
    • afallenhope
      How to enable a window?
      By afallenhope
      Hello all I am trying to use this function to detect if a specific window is running. Then for it to activate it and send an ALT+F command to close the window. But I cannot seem to get it to detect the window and subsequently the rest of the code does not work. 
      #include <MsgBoxConstants.au3> Handleit() Func Handleit() If WinExists("[CLASS:MsiDialogCloseClass]","InstallShield") Then WinActivate("[CLASS:MsiDialogCloseClass]","Installshield") Send("!f") Else MsgBox($MB_SYSTEMMODAL, "", "Uhhh... something didn't work. Fix it!") EndIf EndFunc Is there anything that I can substitute for the CLASS? 
      Thank you for taking the time to read my question! 
      -Richard
    • Ellone
      Several window at a time and focus on a window.
      By Ellone
      Hello,
      I was wondering if it is possible to run more than one script at a time on different windows in order to emulate click and keyboard stroke.
      As far as I know, it's not possible to have several mouses, so I don't see how this could be achieved.
       
      For example, let's say I have a client in one window, and my internet browser in another window. I'd like to run one script on the client window, and another script on the navigator browser simultaneously.
      Is this possible ?
       
      Also how can I get the focus on my browser at the beginning of my script ? I tried winActivate("Mozilla"), without success.
       
       
      Regards.
    • remin
      Focus on Chrome if GUI is active.
      By remin
      I'm still trying to convert my ahk scripts to au3.
      I can't find out how to give focus to my Chrome window (or whatever window) if my GUI (created in autoit) is active.
      Did I miss something?
      This is my code:
      WinActivate("[CLASS:Chrome_WidgetWin_1]") WinWaitActive("[CLASS:Chrome_WidgetWin_1]") It activates Chrome but does not give it focus when my GUI is still visible or minimized at the taskbar.
      I see the Chrome window in front of me but I have to click in it to give it focus, 
      (same if I only use the first line (WinActivate without WinWaitActive))
      1) How can I give focus to the chrome window in above case?
      2) When chrome is minimized on the taskbar above code does not activate chrome.
          Is there a way to activate chrome when it is on the taskbar?
          
          I tried this code but it didn't work:  WinSetState("[CLASS:Chrome_WidgetWin_1]", "", @SW_SHOW)
    • thepaulguy
      Simple Window Closing Madness
      By thepaulguy
      I am having some issues with what should be the simplest of scripts. I have a program that records internet tv. If the network drops for some reason, an error window pops up and it will not record anything that is scheduled from that point on until the error is closed. So if this occurs at night when I am sleeping, I could miss recording several tv shows. So here is what I have done.

      The error windows do not have a title so I am using the window text and WinGetHandle to close.

      Like this...


      while 1 sleep(100) if WinExists("","ネットワークに接続できません。") then $handle = WinGetHandle("","ネットワークに接続できません。") WinClose($handle) ;MsgBox(0, "WinGetHandle Result",$handle) EndIf Wend
      I have tested this out by manually creating the error window using this and the above script will close the window fine.


      MsgBox(0, "","ネットワークに接続できません。")
      When I force the program to throw the error by disconnecting my network cable, the error window comes up but never closes. I added the WinGetHandle Result msgbox to see if it was even detecting the window. It does display the proper handle, I confirmed it by using the AutoIt Window Info tool.

      I even tried this but it doesn't work either.....


      while 1 sleep(100) if WinExists("","ネットワークに接続できません。") then $handle = WinGetHandle("","ネットワークに接続できません。") WinActivate($handle) Send("{ESC}") ;MsgBox(0, "WinGetHandle Result",$handle) EndIf Wend
      Any suggestions?