Jump to content

probably a noob question


Go to solution Solved by kylomas,

Recommended Posts

So I'm writing a script that will perform some repetitive tasks that I have to do on PC's before they're sent out to the field. You know, the kind of thing that AutoIt was bread for ;)

Needless to say I've hit what is probably a simple wall, but my simple brain isn't grasping it.

The script starts and performs the first two items, but when it gets to the third (installing TeamViewer) it just ends. Actually it ends just prior to that when it closes a GUI window.

; Intro
Send("#d")
Sleep(500)
MsgBox(0, "DR Staging Script", "This script will perform numerous automated tasks, however some tasks will require input. Please monitor the scripts progress and provide input when necessary.", 20)

; Set PC Name
Send("#r")
WinWaitActive("Run")
Send("control sysdm.cpl{Enter}")
WinWaitActive("System Properties")
ControlFocus("[TITLE:System Properties; CLASS:#32770]", "", "Button2")
Send("{Space}")
ControlFocus("[TITLE:Computer Name/Domain Changes; CLASS:#32770]", "", "Edit1")
Send("^a")
Send("{Del}")
Local $pcname = InputBox("Input Required", "Please enter the PC's name.", "")
Send($pcname)
ControlFocus("[TITLE:Computer Name/Domain Changes; CLASS:#32770]", "", "Button6")
Send("{Space}")
WinWaitActive("[Class:DirectUIHWND", "OK", 15 )
Send("{Space}")
WinWaitActive("System Properties")
ControlFocus("[TITLE:System Properties; CLASS:#32770]", "", "Button3")
Send("{Space}")
WinWaitActive("Microsoft Windows")
ControlFocus("[TITLE:Microsoft Windows; CLASS:#32770]", "", "Button2")
Send("{Space}")

Sleep(3000)

; Set Time Zone for location where unit is being shipped
Send("#r")
WinWaitActive("Run")
Send("control timedate.cpl{Enter}")
WinWaitActive("Date and Time")
ControlFocus("[TITLE:Date and Time; CLASS:#32770]", "", "Button2")
Send("{Space}")

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 
$mainwindow = GUICreate("Please select a Time Zone", 275, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Available Time Zones", 85, 10)
  $Button_1 = GUICtrlCreateButton("Pacific Time", 77, 30, 120)
  $Button_2 = GUICtrlCreateButton("Mountain Time", 77, 60, 120)
  $Button_3 = GUICtrlCreateButton("Central Time", 77, 90, 120)
  $Button_4 = GUICtrlCreateButton("Eastern Time", 77, 120, 120)
  $Button_5 = GUICtrlCreateButton("Hawaii", 77, 150, 120)
  $Button_6 = GUICtrlCreateButton("Alaska", 77, 180, 120)
GUICtrlSetOnEvent($Button_1, "Pacific")
GUICtrlSetOnEvent($Button_2, "Mountain")
GUICtrlSetOnEvent($Button_3, "Central")
GUICtrlSetOnEvent($Button_4, "Eastern")
GUICtrlSetOnEvent($Button_5, "Hawaii")
GUICtrlSetOnEvent($Button_6, "Alaska")
GUISetState(@SW_SHOW)

While 1
  Sleep(1000)  ; Idle around
WEnd

Func Pacific()
  ;Note: at this point @GUI_CTRLID would equal $Button_1,
  ;and @GUI_WINHANDLE would equal $mainwindow
  WinActivate("Time Zone Settings")
  Send("{Home}")
  Send("{Down 5}")
  Send("{Enter}")
  WinClose("Date and Time")
  WinClose("Please select a Time Zone")
EndFunc

Func Mountain()
  ;Note: at this point @GUI_CTRLID would equal $Button_1,
  ;and @GUI_WINHANDLE would equal $mainwindow
  WinActivate("Time Zone Settings")
  Send("{Home}")
  Send("{Down 8}")
  Send("{Enter}")
  WinClose("Date and Time")
  WinClose("Please select a Time Zone")
EndFunc

Func Central()
  ;Note: at this point @GUI_CTRLID would equal $Button_1,
  ;and @GUI_WINHANDLE would equal $mainwindow
  WinActivate("Time Zone Settings")
  Send("{Home}")
  Send("{Down 10}")
  Send("{Enter}")
  WinClose("Date and Time")
  WinClose("Please select a Time Zone")
EndFunc

Func Eastern()
  ;Note: at this point @GUI_CTRLID would equal $Button_1,
  ;and @GUI_WINHANDLE would equal $mainwindow
  WinActivate("Time Zone Settings")
  Send("{Home}")
  Send("{Down 14}")
  Send("{Enter}")
  WinClose("Date and Time")
  WinClose("Please select a Time Zone")
EndFunc

Func Hawaii()
  ;Note: at this point @GUI_CTRLID would equal $Button_1,
  ;and @GUI_WINHANDLE would equal $mainwindow
  WinActivate("Time Zone Settings")
  Send("{Home}")
  Send("{Down 2}")
  Send("{Enter}")
  WinClose("Date and Time")
  WinClose("Please select a Time Zone")
EndFunc

Func Alaska()
  ;Note: at this point @GUI_CTRLID would equal $Button_1,
  ;and @GUI_WINHANDLE would equal $mainwindow
  WinActivate("Time Zone Settings")
  Send("{Home}")
  Send("{Down 3}")
  Send("{Enter}")
  WinClose("Date and Time")
  WinClose("Please select a Time Zone")
EndFunc

Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
  ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
  If @GUI_WINHANDLE = $mainwindow Then 
    WinClose("Please select a Time Zone")
    Exit
  EndIf 
EndFunc

Sleep(3000)

;Install TeamViewer
Run("C:\Service\TeamViewer\TeamViewer_Setup_en.exe")
WinWaitActive("TeamViewer 7 Setup")
Send("{Enter}")
Send("{Space}")
Send("{Enter}")
Send("{Tab}")
Send("{Space}")
Send("{Tab}")
Send("{Space}")
Send("{Tab 2}")
Send("{Enter}")
Send("{Tab}")
Send("{Space}")
Send("{Tab 2}")
Send("{Enter}")
WinWaitActive("Unattended access")
WinSetState("[TITLE:TeamViewer; CLASS:#32770]", "", @SW_MINIMIZE)
WinActivate("Unattended access")
ControlFocus("[TITLE:Unattended access; CLASS:#32770]", "", "12324")
Send("{Enter}")
WinActivate("[TITLE:Unattended access - Step 1 of 2; CLASS:#32770]", "")
Local $tvpassword = InputBox("Input Required", "Please enter the first word of the clinics name.", "")
ControlSend("[TITLE:Unattended access - Step 1 of 2; CLASS:#32770]", "", "Edit2", "scil" & $tvpassword)
ControlSend("[TITLE:Unattended access - Step 1 of 2; CLASS:#32770]", "", "Edit3", "scil" & $tvpassword)
ControlFocus("[TITLE:Unattended access; CLASS:#32770]", "", "Button3")
Send("{Enter}")
WinWaitActive("Unattended access - Step 2 of 2")
Send("{Down 2}")
Send("{Space}")
Send("{Enter}")
MsgBox(0, "ATTENTION", "Please record the TeamViewer ID for your records")
Send("{Enter}")
Sleep(2000)
WinClose("[TITLE:TeamViewer; CLASS:#32770]", "")

Sleep(3000)

;Install Scil IPS Software

Now I'm already aware that some of the code is, rudimentary at best, but I'm slowing going through as I learn and figure things out and replacing bits.

Link to comment
Share on other sites

  • Moderators

Few suggestions

·         Use ControlClick, ControlSend, and ControlCommand whenever possible. Send can fail for numerous reasons.

·         Add some error/progress checking. Set timeouts on your WinWait* commands and handle failures appropriately.

·         Your time zone functions can be combined into one function then check the @GUI_CtrlId in switch statement.

·         The idle delay should be shorter. Try 50 milliseconds as a starting point. If this causes high CPU usage you can increase it slightly.

·         The code would be easier to read if the procedures for each program were their own function. Example SetPCName($sName), SetTimeZone($sZone), etc…

·         Add some debug lines that write to the console so you can better troubleshoot the code.

Link to comment
Share on other sites

How exactly would I turn the SetTimeZone section into one function? As it is it makes sense to me, I know you can't have functions within functions (or it may be possible, but beyond me). Originally I found this code as an example and it was exactly what I was looking for, I just made some modifications to it to cater my needs exactly.

Send("#r")
   WinWaitActive("Run")
   Send("control timedate.cpl{Enter}")
   WinWaitActive("Date and Time")
   ControlFocus("[TITLE:Date and Time; CLASS:#32770]", "", "Button2")
   Send("{Space}")
   
   #include <GUIConstantsEx.au3>
   
   Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 
   $mainwindow = GUICreate("Please select a Time Zone", 275, 240)
   GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
   GUICtrlCreateLabel("Available Time Zones", 85, 10)
      $Button_1 = GUICtrlCreateButton("Pacific Time", 77, 30, 120)
      $Button_2 = GUICtrlCreateButton("Mountain Time", 77, 60, 120)
      $Button_3 = GUICtrlCreateButton("Central Time", 77, 90, 120)
      $Button_4 = GUICtrlCreateButton("Eastern Time", 77, 120, 120)
      $Button_5 = GUICtrlCreateButton("Hawaii", 77, 150, 120)
      $Button_6 = GUICtrlCreateButton("Alaska", 77, 180, 120)
   GUICtrlSetOnEvent($Button_1, "Pacific")
   GUICtrlSetOnEvent($Button_2, "Mountain")
   GUICtrlSetOnEvent($Button_3, "Central")
   GUICtrlSetOnEvent($Button_4, "Eastern")
   GUICtrlSetOnEvent($Button_5, "Hawaii")
   GUICtrlSetOnEvent($Button_6, "Alaska")
   GUISetState(@SW_SHOW)
   
   While 1
      Sleep(1000)  ; Idle around
   WEnd

   Func Pacific()
      ;Note: at this point @GUI_CTRLID would equal $Button_1,
      ;and @GUI_WINHANDLE would equal $mainwindow
   WinActivate("Time Zone Settings")
      Send("{Home}")
      Send("{Down 5}")
      Send("{Enter}")
   WinClose("Date and Time")
   WinClose("Please select a Time Zone")
   EndFunc

   Func Mountain()
      ;Note: at this point @GUI_CTRLID would equal $Button_1,
      ;and @GUI_WINHANDLE would equal $mainwindow
   WinActivate("Time Zone Settings")
      Send("{Home}")
      Send("{Down 8}")
      Send("{Enter}")
   WinClose("Date and Time")
   WinClose("Please select a Time Zone")
   EndFunc

   Func Central()
      ;Note: at this point @GUI_CTRLID would equal $Button_1,
      ;and @GUI_WINHANDLE would equal $mainwindow
   WinActivate("Time Zone Settings")
      Send("{Home}")
      Send("{Down 10}")
      Send("{Enter}")
   WinClose("Date and Time")
   WinClose("Please select a Time Zone")
   EndFunc

   Func Eastern()
      ;Note: at this point @GUI_CTRLID would equal $Button_1,
      ;and @GUI_WINHANDLE would equal $mainwindow
   WinActivate("Time Zone Settings")
      Send("{Home}")
      Send("{Down 14}")
      Send("{Enter}")
   WinClose("Date and Time")
   WinClose("Please select a Time Zone")
   EndFunc

   Func Hawaii()
      ;Note: at this point @GUI_CTRLID would equal $Button_1,
      ;and @GUI_WINHANDLE would equal $mainwindow
   WinActivate("Time Zone Settings")
      Send("{Home}")
      Send("{Down 2}")
      Send("{Enter}")
   WinClose("Date and Time")
   WinClose("Please select a Time Zone")
   EndFunc

   Func Alaska()
      ;Note: at this point @GUI_CTRLID would equal $Button_1,
      ;and @GUI_WINHANDLE would equal $mainwindow
   WinActivate("Time Zone Settings")
      Send("{Home}")
      Send("{Down 3}")
      Send("{Enter}")
   WinClose("Date and Time")
   WinClose("Please select a Time Zone")
   EndFunc

   Func CLOSEClicked()
      ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
      ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
   If @GUI_WINHANDLE = $mainwindow Then 
      WinClose("Please select a Time Zone")
      Exit
   EndIf 
   EndFunc
Edited by Flanders42
Link to comment
Share on other sites

  • Solution

Flanders42,

You are exiting your script when you close your GUI.  See comments in code below...

Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
  ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
  If @GUI_WINHANDLE = $mainwindow Then 
    WinClose("Please select a Time Zone")
    ;Exit <--------------------------  change this 
    guidelete($mainwindow) ;  <------  to this 
  EndIf 
EndFunc

Also, I don't understand the reference to $dummywindow in the comment.

kylomas

edit:  Your time zone routine could be organized like this

func Set_Time()
    switch @GUI_CtrlId
        case $button_1
            ; do pacific routine
        case $Button_2
            ; do mountain routine
        ;
        ; etc.
        ;
    endswitch
endfunc
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

  • Moderators

This is how I'd do the time zone piece.

; This just makes the code easier to understand
; $Eastern = 0, $Central = 1, etc...
Global Enum $Eastern, $Central, $Mountain, $Pacific, $Alaska, $Hawaii

; Store each required time zone in the $aTimeZone array
; The Enum above will be used to reference the array elements
Global $aTimeZone[6] = ["(UTC-05:00) Eastern Time (US & Canada)", "(UTC-06:00) Central Time (US & Canada)", _
        "(UTC-07:00) Mountain Time (US & Canada)", "(UTC-08:00) Pacific Time (US & Canada)", _
        "(UTC-09:00) Alaska", "(UTC-10:00) Hawaii"]

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
$mainwindow = GUICreate("Please select a Time Zone", 275, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Available Time Zones", 85, 10)
$Button_1 = GUICtrlCreateButton("Pacific Time", 77, 30, 120)
$Button_2 = GUICtrlCreateButton("Mountain Time", 77, 60, 120)
$Button_3 = GUICtrlCreateButton("Central Time", 77, 90, 120)
$Button_4 = GUICtrlCreateButton("Eastern Time", 77, 120, 120)
$Button_5 = GUICtrlCreateButton("Hawaii", 77, 150, 120)
$Button_6 = GUICtrlCreateButton("Alaska", 77, 180, 120)
; Just because I do not like redundant code
For $i = 1 To 6
    GUICtrlSetOnEvent(Eval("Button_" & $i), "SetTimeZone")
Next
GUISetState(@SW_SHOW)

While 1
    Sleep(50)
WEnd

Func SetTimeZone()
    Local $Zone
    Switch @GUI_CtrlId
        Case $Button_1
            $Zone = $Pacific
        Case $Button_2
            $Zone = $Mountain
        Case $Button_3
            $Zone = $Central
        Case $Button_4
            $Zone = $Eastern
        Case $Button_5
            $Zone = $Hawaii
        Case $Button_6
            $Zone = $Alaska
    EndSwitch
    ; Shows that we get the correct zone label when clicking each button
    ConsoleWrite($aTimeZone[$Zone] & @CRLF)
    ; Uncomment the following line to set the time zone programmatically
    ;Run("control timedate.cpl,,/Z " & $aTimeZone[$Zone])
EndFunc   ;==>SetTimeZone

Func CLOSEClicked()
    ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
    ;@GUI_WINHANDLE will be either $mainwindow or $dummywindow
    If @GUI_WinHandle = $mainwindow Then
        WinClose("Please select a Time Zone")
        Exit
    EndIf
EndFunc   ;==>CLOSEClicked

If the display names "(UTC-09:00) Alaska" do not work then try the key names "Alaskan Standard Time" found here HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionTime Zones

Edited by big_daddy
Link to comment
Share on other sites

You can have function calls within a function, which is a similar thing.

In other words you are accessing an exterior function, by calling it from within another.

Func One()

     ........ (code here)

     Two()

     ........ (code here)

EndFunc

Func Two()

     ........ (code here)

EndFunc

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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