Jump to content

AutoITX Test with WordPad and Notepad


wizard2277
 Share

Recommended Posts

To AutoITX People:

I'm a professional programmer that is a newbie to AutoIT and

AutoITX. I am attempting to use AutoITX from VB .Net

within Visual Studio 2008. I would like to be able to start

a couple of applications from one of my programs and switch

windows. As I switch from Window 1 to Window 2, I would

like to hide Window 1 so that it's not visible and vice-versa.

One of the programs is a third party application for which

I do not have source code.

I am attempting to test AutoITX to see if it will do what I

need, but I am having some problems in my testing.

Hiding and showing seems to work OK when I am using

Notepad as an application by itself. However, when I

switch to WordPad, it doesn't hide the window or kill

it when it's done (all of which occurs with Notepad).

I don't believe that it's a problem just with WordPad as

I see some of the same behavior wotj ,u 3rd party app

and Notepad. Is there a problem with the following

code (I put it on a single form with a command button

and a label):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objAu3 As New AutoItX3Lib.AutoItX3

objAu3.AutoItSetOption("WinTitleMatchMode", 2)

objAu3.Run("C:\Program Files\Windows Nt\Accessories\wordpad.exe")

objAu3.WinWaitActive("WordPad")

objAu3.Sleep(2000)

objAu3.Send("This is Wordpad Line 1{ENTER}{CAPSLOCK off}")

objAu3.Sleep(100)

objAu3.Send("This is Wordpad Line 2{ENTER}{CAPSLOCK off}")

objAu3.Sleep(200)

objAu3.Send("This is Wordpad Line 3{ENTER}{CAPSLOCK off}")

objAu3.Sleep(200)

objAu3.Send("This is Wordpad Line 4{ENTER}{CAPSLOCK off}")

objAu3.Sleep(200)

objAu3.Send("This is Wordpad Line 5{ENTER}{CAPSLOCK off}")

objAu3.Sleep(2000)

objAu3.WinSetState("Wordpad", "", objAu3.SW_HIDE)

objAu3.Sleep(2000)

objAu3.WinKill("Wordpad")

objAu3.WinWaitClose("Wordpad", "")

objAu3.Sleep(2000)

Label1.Text = "Wordpad and Notepad Opened and Closed"

End Sub

Any help or insight that you could give would be greatly appreciated.

Thanks,

C. Coulter

Link to comment
Share on other sites

Hello C. Coulter,

this solution works with vb script:

'-Begin-----------------------------------------------------------------

  Option Explicit
  
  Dim objAu3
  
  Dim WinTitle : WinTitle = "Dokument - WordPad"
  Dim WinClass : WinClass = "WordPadClass"
  
  Dim Title : Title = "[TITLE:" & WinTitle & "; CLASS:" & WinClass & "]" 
  
  Set objAu3 = CreateObject("AutoItX3.Control")

  objAu3.Run "wordpad.exe"
  objAu3.WinWaitActive(Title)
  objAu3.Sleep(2000)
  objAu3.Send("This is Wordpad Line 1{ENTER}{CAPSLOCK off}")
  objAu3.Sleep(100)
  objAu3.Send("This is Wordpad Line 2{ENTER}{CAPSLOCK off}")
  objAu3.Sleep(200)
  objAu3.Send("This is Wordpad Line 3{ENTER}{CAPSLOCK off}")
  objAu3.Sleep(200)
  objAu3.Send("This is Wordpad Line 4{ENTER}{CAPSLOCK off}")
  objAu3.Sleep(200)
  objAu3.Send("This is Wordpad Line 5{ENTER}{CAPSLOCK off}")
  objAu3.Sleep(2000)
  objAu3.WinSetState Title, "", objAu3.SW_HIDE
  objAu3.Sleep(2000)
  objAu3.WinSetState Title, "", objAu3.SW_SHOW
  objAu3.Sleep(2000)
  objAu3.WinKill Title
  objAu3.WinWaitClose Title
  objAu3.Sleep(2000)

  MsgBox "Wordpad and Notepad Opened and Closed"

'-End-------------------------------------------------------------------

I change only the title to title and class of the WordPad window and it works. If it is only the title, it does not work on my computer too.

Hope it helps.

Cheers

Stefan

Link to comment
Share on other sites

Stefan, et al:

Thanks so much for your help. After much trial and effort,

I finally got things to work the way that I wanted (with

one exception). The only thing that I could not figure

out was why the last "WinWaitClose" (currently commented

out below) hangs the program. If I comment that command

out, everything works:

Here is my final working code. I hope that it might help

someone else in the future:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objAu3 As New AutoItX3Lib.AutoItX3

Shell("notepad.exe", vbMaximizedFocus)

objAu3.WinWaitActive("Untitled - Notepad")

objAu3.Send("Hello, this is Notepad line 1{ENTER}{CAPSLOCK off}")

objAu3.Send("Hello, this is Notepad line 2{ENTER}{CAPSLOCK off}")

objAu3.WinSetState("Untitled - Notepad", "", objAu3.SW_HIDE)

objAu3.Sleep(2000)

Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe", vbMaximizedFocus)

objAu3.WinWaitActive("Document - WordPad")

objAu3.Send("Hello, this is WordPad line 1{ENTER}{CAPSLOCK off}")

objAu3.Send("Hello, this is WordPad line 2{ENTER}{CAPSLOCK off}")

objAu3.WinSetState("Document - WordPad", "", objAu3.SW_HIDE)

objAu3.Sleep(2000)

objAu3.WinSetState("Untitled - Notepad", "", objAu3.SW_SHOW)

objAu3.WinActivate("Untitled - Notepad")

objAu3.Sleep(1000)

objAu3.Send("Hello, this is Notepad line 3{ENTER}{CAPSLOCK off}")

objAu3.Send("Hello, this is Notepad line 4{ENTER}{CAPSLOCK off}")

objAu3.WinClose("Untitled - Notepad")

objAu3.WinWaitClose("Untitled - Notepad", "")

objAu3.WinSetOnTop("Document - WordPad", "", 1)

objAu3.WinSetState("Document - WordPad", "", objAu3.SW_SHOW)

objAu3.WinActivate("Document - WordPad")

objAu3.WinSetOnTop("Document - WordPad", "", 0)

objAu3.Send("Hello, this is WordPad line 3{ENTER}{CAPSLOCK off}")

objAu3.Send("Hello, this is WordPad line 4{ENTER}{CAPSLOCK off}")

objAu3.WinClose("Document - WordPad")

' Hangs program: objAu3.WinWaitClose("Document - WordPad", "")

objAu3.Sleep(4000)

Label1.Text = "WordPad and Notepad Opened and Closed"

End Sub

Link to comment
Share on other sites

  • Developers

This issue with the initial posted script had to do with the fact that you didn't have the right caps in the title.

The last few lines all had Wordpad in stead of WordPad.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hello C.Coulter,

your code works perfect on my system. I put it in a VBA editor and checked it out. Here is the code:

Sub Test()

  Dim objAu3 As New AutoItX3Lib.AutoItX3

  Set objAu3 = CreateObject("AutoItX3.Control")

  'Shell("notepad.exe", vbMaximizedFocus)
  objAu3.Run "notepad.exe", "", objAu3.SW_MAXIMIZE
  objAu3.WinWaitActive "Unbenannt - Editor"
  
  objAu3.Send "Hello, this is Notepad line 1{ENTER}{CAPSLOCK off}"
  objAu3.Send "Hello, this is Notepad line 2{ENTER}{CAPSLOCK off}"

  objAu3.WinSetState "Unbenannt - Editor", "", objAu3.SW_HIDE
  objAu3.Sleep 2000
  
  'Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe", vbMaximizedFocus)
  objAu3.Run "C:\\Programme\\Windows NT\\Zubehör\\wordpad.exe", _
    "C:\\Programme\\Windows NT\\Zubehör", objAu3.SW_MAXIMIZE
  objAu3.WinWaitActive "Dokument - WordPad"
  objAu3.Send "Hello, this is WordPad line 1{ENTER}{CAPSLOCK off}"
  objAu3.Send "Hello, this is WordPad line 2{ENTER}{CAPSLOCK off}"
  objAu3.WinSetState "Dokument - WordPad", "", objAu3.SW_HIDE
  objAu3.Sleep (2000)
  objAu3.WinSetState "Unbenannt - Editor", "", objAu3.SW_SHOW
  objAu3.WinActivate "Unbenannt - Editor"
  objAu3.Sleep 1000
  objAu3.Send "Hello, this is Notepad line 3{ENTER}{CAPSLOCK off}"
  objAu3.Send "Hello, this is Notepad line 4{ENTER}{CAPSLOCK off}"
  objAu3.WinClose "Unbenannt - Editor"
  objAu3.WinWaitClose "Unbenannt - Editor", ""
  objAu3.WinSetState "Dokument - WordPad", "", objAu3.SW_SHOW
  objAu3.WinSetOnTop "Dokument - WordPad", "", 1
  objAu3.WinActivate "Dokument - WordPad"
  objAu3.WinSetOnTop "Dokument - WordPad", "", 0
  objAu3.Send "Hello, this is WordPad line 3{ENTER}{CAPSLOCK off}"
  objAu3.Send "Hello, this is WordPad line 4{ENTER}{CAPSLOCK off}"
  objAu3.WinClose "Dokument - WordPad"
  objAu3.WinWaitClose "Dokument - WordPad"
  objAu3.Sleep 4000
  MsgBox "WordPad and Notepad Opened and Closed"

End Sub

I do not know the special problem of WinClose or WinWaitClose. Maybe check another solution for WinClose, Send "!fx", this means Alt+F for the file menu and x for exit. And remember, Notepad and WordPad open a dialog at the end to ask about saving the content. You must answer this dialog, otherwise the AutoIt script waits until eternity. Hope it helps.

Cheers

Stefan

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