Jump to content

Help with global variables


Recommended Posts

Hi All

I need some help. I have a main script file called shoots.au3 which has all the common variables and commands and then scriptlets which run sections of the shoots.au3.

The problem i have is that i seem to have to declare all the variables that i might use at the top of the script before using them.

It seems to have started once i started to add Functions to break up the script, so only bits of it ran, when i wanted them to run.

#include <Date.au3>
#include <File.au3>
#include "CustomMsgBox.au3"
#include <Misc.au3>
#include "Busy.au3"
#include <Array.au3>
#include <Outlook.au3>

Opt("SendKeyDelay", 50) ;50 milliseconds
Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("WinWaitDelay", 750) ;250 milliseconds

HotKeySet("{ESC}", "Exit1")
Func Exit1()
    Exit
EndFunc   ;==>Exit1

$yesterday1   = _DateTimeFormat(_DateAdd("D",-1,_NowCalcDate()),0)
$yesterday    = StringReplace($yesterday1, "/", "-")
;MsgBox(0, "This Should be SUNDAY's Date", $yesterday, 5)
$today = @MDAY & "-" & @MON & "-" & @YEAR

$num = 13
Global $sfile[$num], $sWindow[$num], $sSwitch[$num], $f[10], $d[10], $backup, $body, $eod, $s_f, $who, $who1, $who2, $crprog, $crwin, $p_c, $p_c_r, $ops, $month, $dir, $dir1, $dir2, $dir3, $dir4, $dir5, $dir6, $wash, $cob, $stan

$pcent1 = 100 / $num
$pcent = 100 / $num

uVar()

Func uVar()

    $body = "Here is today’s product changes." & @CRLF & @CRLF
    $month = _DateMonthOfYear(@MON, 0)
    $s_f = "S:\"
    $wash = "Washington"
    $stan = "Stanmore"
    $cob = "Cobham"
    $d_i = "\DUMPED ITEMS\"
    $w_r = "\Wastage Report\"
    $dir = $s_f & $wash & $d_i & @YEAR & "\"
    $dir1 = $s_f & $stan & $d_i & @YEAR & "\"
    $dir2 = $s_f & $cob & $d_i & @YEAR & "\"
    $dir3 = $s_f & $wash & $w_r & @YEAR & "\"
    $dir4 = $s_f & $stan & $w_r & @YEAR & "\"
    $dir5 = $s_f & $cob & $w_r & @YEAR & "\"
    $dir6 = $s_f & "Branch Reports\TOP 10 BY DEPT\" & @YEAR & "\"
    
    uVar2()
EndFunc

Func uVar2() ; variables for Crystal Reports and emails
    $crprog = "C:\Program Files\Business Objects\BusinessObjects Enterprise 12.0\win32_x86\crw32.exe"
    $crwin = "Crystal Reports"
    $p_c_r = "Product Changes Report"
    $ops = "O:\"
    $eod = $ops & "EOD REPORTS\"
    $p_c = "P:\" & @YEAR & "\" & $month & "\"
    $who = "xxxx"
    $who1 = "xx; xx; xxx"
    $who2 = "xx; xx; xx"
    $backup = "C:\Users\Public\Backup\"

    uVar3()
EndFunc

Func uVar3() ;variables for eod and monrun
    $f[1] = $dir & $wash & " Dumped WE " & $yesterday & ".pdf"
    $d[1] = $dir & $wash
    $f[2] = $dir2 & $cob & " Dumped WE " & $yesterday & ".pdf"
    $d[2] = $dir2 & $cob
    $f[3] = $dir1 & $stan & " Dumped WE " & $yesterday & ".pdf"
    $d[3] = $dir1 & $stan
    $f[4] = $dir3 & $wash & " Wastage Report WE " & $yesterday & ".pdf"
    $d[4] = $dir3 & $wash
    $f[5] = $dir5 & $cob & " Wastage Report WE " & $yesterday & ".pdf"
    $d[5] = $dir5 & $cob
    $f[6] = $dir4 & $stan & " Wastage Report WE " & $yesterday & ".pdf"
    $d[6] = $dir4 & $stan
    $f[7] = $dir6 & "top 10 by dept we " & $yesterday & ".pdf"
    $d[7] = $dir6
    $f[8] = $p_c & "Product Changes Report-" & $today & ".pdf"
    $d[8] = $p_c
    $f[9] = $p_c & "Price Changes Report-" & $today & ".pdf"
    $d[9] = $p_c
EndFunc   ;==>uVar

Func startup() ; list of files to load at startup, checks if they exist and adds switches
    $sfile[1] = @ProgramFilesDir & "\Office\Office12\OUTLOOK.EXE"
    $sWindow[1] = "Outlook"
    $sfile[2] = @ProgramFilesDir & "\OpenXpos Head Office\OpenXpos Head Office.exe"
    $sWindow[2] = "OpenXpos Head Office"
    $sfile[3] = @ProgramFilesDir & "\AutoIt3\AutoIt3Help.exe"
    $sWindow[3] = "AutoIt Help File"
    $sfile[4] = @ProgramFilesDir & "\TopDesk\topdesk.exe"
    $sWindow[4] = "TopDesk"
    $sfile[5] = "C:\Users\Public\Backup\Batch files\auto_priority.exe"
    $sWindow[5] = "auto_priority"
    $sfile[6] = @ProgramFilesDir & "\Java\jre6\bin\jusched.exe"
    $sWindow[6] = "Java"
    $sfile[7] = @ProgramFilesDir & "\Nitro PDF\Professional\NitroPDFPrinterMonitor.exe"
    $sWindow[7] = "Nitro PDF Printer Monitor"
    $sfile[8] = @ProgramFilesDir & "\BBC Alerts\BBC_Alerts.exe"
    $sWindow[8] = "BBC_Alerts"
    $sfile[9] = @ProgramFilesDir & "\AutoIt3\SciTE\SciTE.exe"
    $sWindow[9] = "SciTE Script Editor"
    $sfile[10] = @ProgramFilesDir & "\OpenXPos\OpenXPos.exe"
    $sWindow[10] = "OpenXpos"
    $sfile[11] = @ProgramFilesDir & "\AutoIt3\Au3Info.exe"
    $sWindow[11] = "Autoit Window Info"
    $sfile[12] = @SystemDir & "\taskmgr.exe"
    $sWindow[12] = "Task Manager"

    For $x = 1 To UBound($sfile) - 1
        If Not FileExists($sfile[$x]) Then MsgBox(4096, "ALERT,     ALERT,       ALERT", $sfile[$x] & " Does NOT exists", 10)
    Next

    $sSwitch[2] = @ProgramFilesDir & "\OpenXpos Head Office\"
    $sSwitch[3] = "c:\"
    $sSwitch[4] = "c:\"
    $sSwitch[5] = "c:\"
    $sSwitch[6] = "c:\"
    $sSwitch[7] = "c:\"
    $sSwitch[8] = "c:\"
    $sSwitch[9] = "c:\"
    $sSwitch[10] = @ProgramFilesDir & "\OpenXPos\"
    $sSwitch[11] = "c:\"
    $sSwitch[12] = "c:\"
    ;$sSwitch[13]  ="c:\"
    ;Exit
EndFunc   ;==>startup

Func monrun() ; checks if the files exists and displays if missing
    For $x = 1 To UBound($f) - 3
        If Not FileExists($f[$x]) Then ShellExecute("explorer", $d[$x])
    Next
    For $x = 1 To UBound($f) - 3
        If Not FileExists($f[$x]) Then MsgBox(4096, "ALERT,     ALERT,       ALERT      MONRUN", $f[$x] & " Does NOT exists", 10)
        If Not FileExists($f[$x]) Then Exit
    Next
EndFunc   ;==>monrun

Func eod() ; checks if the files exists and displays if missing, then closes Autoit Window
    For $x = 8 To UBound($f) - 1
        If Not FileExists($f[$x]) Then ShellExecute("explorer", $d[$x])
    Next
    For $x = 8 To UBound($f) - 1
        If Not FileExists($f[$x]) Then MsgBox(4096, "ALERT,     ALERT,       ALERT      EOD", $f[$x] & " Does NOT exists", 10)
        If Not FileExists($f[$x]) Then Exit
    Next
    If WinExists("AutoIt v3 Window Info") Then
        WinClose("AutoIt v3 Window Info")
        WinWaitClose("AutoIt v3 Window Info", "", 5)
    EndIf
EndFunc   ;==>eod

Func testing()
    $answer = xMsgBox(16 + 0x200, "Tony's Script", "Are you just testing the script?." & @CRLF & @CRLF, "YES", "NO", "")
    If $answer = 7 Then $who = "xxx; xx; xxx" ; no
    ;
    If $answer = 6 Then $who = "xxxx; xxx; xxx" ; yes
EndFunc   ;==>testing

Func folders() ; open explorers ready to use
    ShellExecute("explorer", "O:\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $backup & "Batch files\Shoots\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $eod, "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $s_f, "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $backup & "Downloads\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", "O:\SPREADSHEETS FOR IMPORT\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $backup, "", "", @SW_MINIMIZE)
    ShellExecute("explorer", "O:\Tony\NPR_PFA\", "", "", @SW_MINIMIZE)
EndFunc   ;==>folders

Func shutrestart() ; shutsdown any listed programs
    If WinExists("Mozilla") Then
        WinSetState("Mozilla", "", @SW_RESTORE)
        WinClose("Mozilla")
        ProcessWaitClose("firefox.exe", 30)
        If ProcessExists("firefox.exe") Then MsgBox(4096, "ALERT,     ALERT,       ALERT", "Problem closing Firefox", 10)
    EndIf
    If WinExists("Post-it") Then WinClose("Post-it")
    WinWaitClose("Post-it", "", 5)

    If WinExists("Microsoft Outlook") Then
        WinSetState("Microsoft Outlook", "", @SW_RESTORE)
        WinActivate("Microsoft Outlook")
        Send("!fx")
        ProcessWaitClose("outlook.exe", 30)
        If ProcessExists(".exe") Then MsgBox(4096, "ALERT,     ALERT,       ALERT", "Problem closing Outlook", 10)
    EndIf

    If WinExists("[CLASS:SciTEWindow]") Then
        WinSetState("[CLASS:SciTEWindow]", "", @SW_RESTORE)
        WinActivate("[CLASS:SciTEWindow]")
        Send("!fx")
        ProcessWaitClose("scite.exe", 30)
    EndIf

    If WinExists("OpenXpos Head Office") Then
        WinActivate("OpenXpos Head Office")
        Sleep(750)
        $size = WinGetPos("OpenXpos Head Office")
        Sleep(200)
        MouseClick("left", $size[0] + 15, $size[1] + 30) ; click yes
        Sleep(750)
        MouseClick("left", $size[0] + 45, $size[1] + 95) ; click yes
        WinWaitClose("OpenXpos Head Office", "", 5)
        If WinExists("", "You still have program Running in OpenXPos") Then
            MsgBox(0, "Found One!.", "Click OK." & @CRLF & "This will time out in 10 seconds", 10)
            WinActivate("", "You still have program Running in OpenXPos")
            MouseClick("left", 600, 565)
        EndIf
        WinWaitClose("OpenXpos Head Office", "", 25)
    EndIf

    If WinExists("OpenXPos Store") Then
        WinActivate("OpenXPos Store")
        Sleep(750)
        $size = WinGetPos("OpenXpos Head Office")
        Sleep(200)
        MouseClick("left", $size[0] + 15, $size[1] + 30) ; click yes
        Sleep(750)
        MouseClick("left", $size[0] + 45, $size[1] + 95) ; click yes
        WinWaitClose("OpenXPos Store", "", 5)
        If WinExists("", "You still have program Running in OpenXPos") Then
            MsgBox(0, "Found One!.", "Click OK." & @CRLF & "This will time out in 10 seconds", 10)
            WinActivate("", "You still have program Running in OpenXPos")
            MouseClick("left", 600, 565)
        EndIf
        WinWaitClose("OpenXPos Store", "", 25)
    EndIf

    If WinExists("AutoIt Help") Then
        WinClose("AutoIt Help")
        WinWaitClose("AutoIt Help", "", 5)
    EndIf


    If WinExists("AutoIt v3 Window Info") Then
        WinClose("AutoIt v3 Window Info")
        WinWaitClose("AutoIt v3 Window Info", "", 5)
    EndIf

    MsgBox(0, "Continue Restart?", "Click on OK to continue the RESTART sequence" & @CRLF & "This will time out in 30 seconds", 30)
EndFunc   ;==>shutrestart

Func myadlib()
    If WinExists("Security Alert") Then
        WinActivate("Security Alert")
        Send("!y")
    EndIf
    If WinExists("Business Objects") Then
        WinMove("Business Objects", "", 0, 0)
        Send("!r")
    EndIf
    If WinExists("File already exists") Then
        WinActivate("File already exists")
        Send("y")
    EndIf
    If WinExists("", "This report has been modified") Then
        WinActivate("", "This report has been modified")
        Send("!n")
    EndIf
    If WinExists("frmLogin") Then
        WinActivate("frmLogin")
        Send("81{ENTER}")
        Sleep(500)
        Send("blotto{ENTER}")
    EndIf
EndFunc   ;==>myadlib

Please can someone help explain this, or give me a better way of doing this?

also if someone knows of a tidier way of doing this, i would like to see it.

Link to comment
Share on other sites

If you really are overwhelmed with variables, I would suggest compressing your info into easier to manage [multi-dimension] Arrays. To keep with what you started it may prove useful to declare your variables on separate lines for tidiness/readability sake in their own little "declaration section":

;Global Variables ---------
Global var1
Global var2
Global var3
;End Global Variables -----

Also, you can always declare them for the first time in the functions where they are used first though skiTE does not like it and its probably not the best practice. If you declare them as "Global" they can be used anywhere whereas "Local" will limit it to a function (check the help file for a better description: "Dim / Global / Local / Const")

...
Func uVar()

    Global $body = "Here is today’s product changes." & @CRLF & @CRLF
    Global $month = _DateMonthOfYear(@MON, 0)
    Global $s_f = "S:\"
    Global $wash = "Washington"
    Global $stan = "Stanmore"
    Global $cob = "Cobham"
    Global $d_i = "\DUMPED ITEMS\"
    Global $w_r = "\Wastage Report\"
    Global $dir = $s_f & $wash & $d_i & @YEAR & "\"
    Global $dir1 = $s_f & $stan & $d_i & @YEAR & "\"
    Global $dir2 = $s_f & $cob & $d_i & @YEAR & "\"
    Global $dir3 = $s_f & $wash & $w_r & @YEAR & "\"
    Global $dir4 = $s_f & $stan & $w_r & @YEAR & "\"
    Global $dir5 = $s_f & $cob & $w_r & @YEAR & "\"
    Global $dir6 = $s_f & "Branch Reports\TOP 10 BY DEPT\" & @YEAR & "\"
    
    uVar2()
EndFunc
...
Link to comment
Share on other sites

  • Moderators

happy2help,

It appears you do not understand how the Global/Local keywords work.

- Any variable that is set (that is given a value: $variable = something) in the main part of the script is automatically a Global variable.

- Any variable that is set in a function is automatically a Local variable and is lost when the function ends.

- You can override this with the Global/Local keywords (although SciTE is likely to complain if you use them in the wrong sense!)

So, in your script the variable $month is first defined in function uVar. As it therefore becomes a Local variable by default, it is destroyed when you exit the function. So when you subsequently try and use it in function uVar2, there is nothing to use! :)

Best practise is to declare all variables using the Global and Local keywords so there is no ambiguity. It is also a good idea to use as few Global variables as you can get away with - although this is harder to achieve than you might think.

Remember that you can pass variables as parameters to other functions - and if you use the ByRef keyword you can alter the value within a function as well.

So in your case, look carefully at the variables and see which ones need to be Global - judging from the list you have had to declare the answer is "quite a lot". In that case, consider whether your code could be better structured. There are many ways to do things and although there is no "right" answer, having umpteen Global variables in 273 lines of code is perhaps less than optimal....... :)

I hope this helps. Ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Have I got the right end-of-the-stick with this code

#include <Date.au3>
#include <File.au3>
#include "CustomMsgBox.au3"
#include <Misc.au3>
#include "Busy.au3"
#include <Array.au3>
#include <Outlook.au3>

Opt("SendKeyDelay", 50) ;50 milliseconds
Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("WinWaitDelay", 750) ;250 milliseconds

HotKeySet("{ESC}", "Exit1")
Func Exit1()
    Exit
EndFunc   ;==>Exit1

$yesterday1   = _DateTimeFormat(_DateAdd("D",-1,_NowCalcDate()),0)
$yesterday    = StringReplace($yesterday1, "/", "-")
;MsgBox(0, "This Should be SUNDAY's Date", $yesterday, 5)
$today = @MDAY & "-" & @MON & "-" & @YEAR

$num = 13
Global $sfile[$num], $sWindow[$num], $sSwitch[$num], $f[10], $d[10]

$pcent1 = 100 / $num
$pcent = 100 / $num

uVar()

Func uVar()

    Global $body = "Here is today’s product changes." & @CRLF & @CRLF
    Global $month = _DateMonthOfYear(@MON, 0)
    Global $s_f = "S:\"
    Global $wash = "Washington"
    Global $stan = "Stanmore"
    Global $cob = "Cobham"
    Global $d_i = "\DUMPED ITEMS\"
    Global $w_r = "\Wastage Report\"
    Global $dir = $s_f & $wash & $d_i & @YEAR & "\"
    Global $dir1 = $s_f & $stan & $d_i & @YEAR & "\"
    Global $dir2 = $s_f & $cob & $d_i & @YEAR & "\"
    Global $dir3 = $s_f & $wash & $w_r & @YEAR & "\"
    Global $dir4 = $s_f & $stan & $w_r & @YEAR & "\"
    Global $dir5 = $s_f & $cob & $w_r & @YEAR & "\"
    Global $dir6 = $s_f & "Branch Reports\TOP 10 BY DEPT\" & @YEAR & "\"
    
    uVar2()
EndFunc

Func uVar2() ; variables for Crystal Reports and emails
    Global $crprog = "C:\Program Files\Business Objects\BusinessObjects Enterprise 12.0\win32_x86\crw32.exe"
    Global $crwin = "Crystal Reports"
    Global $p_c_r = "Product Changes Report"
    Global $ops = "O:\"
    Global $eod = $ops & "EOD REPORTS\"
    Global $p_c = "P:\" & @YEAR & "\" & $month & "\"
    Global $who = "xxx"
    Global $who1 = "xxx; xxx;xxx"
    Global $who2 = "xxx; xxx; xxx"
    Global $backup = "C:\Users\Public\Backup\"

    uVar3()
EndFunc

Func uVar3() ;variables for eod and monrun
    $f[1] = $dir & $wash & " Dumped WE " & $yesterday & ".pdf"
    $d[1] = $dir & $wash
    $f[2] = $dir2 & $cob & " Dumped WE " & $yesterday & ".pdf"
    $d[2] = $dir2 & $cob
    $f[3] = $dir1 & $stan & " Dumped WE " & $yesterday & ".pdf"
    $d[3] = $dir1 & $stan
    $f[4] = $dir3 & $wash & " Wastage Report WE " & $yesterday & ".pdf"
    $d[4] = $dir3 & $wash
    $f[5] = $dir5 & $cob & " Wastage Report WE " & $yesterday & ".pdf"
    $d[5] = $dir5 & $cob
    $f[6] = $dir4 & $stan & " Wastage Report WE " & $yesterday & ".pdf"
    $d[6] = $dir4 & $stan
    $f[7] = $dir6 & "top 10 by dept we " & $yesterday & ".pdf"
    $d[7] = $dir6
    $f[8] = $p_c & "Product Changes Report-" & $today & ".pdf"
    $d[8] = $p_c
    $f[9] = $p_c & "Price Changes Report-" & $today & ".pdf"
    $d[9] = $p_c
EndFunc   ;==>uVar

Func startup() ; list of files to load at startup, checks if they exist and adds switches
    $sfile[1] = @ProgramFilesDir & "\Office\Office12\OUTLOOK.EXE"
    $sWindow[1] = "Outlook"
    $sfile[2] = @ProgramFilesDir & "\OpenXpos Head Office\OpenXpos Head Office.exe"
    $sWindow[2] = "OpenXpos Head Office"
    $sfile[3] = @ProgramFilesDir & "\AutoIt3\AutoIt3Help.exe"
    $sWindow[3] = "AutoIt Help File"
    $sfile[4] = @ProgramFilesDir & "\TopDesk\topdesk.exe"
    $sWindow[4] = "TopDesk"
    $sfile[5] = "C:\Users\Public\Backup\Batch files\auto_priority.exe"
    $sWindow[5] = "auto_priority"
    $sfile[6] = @ProgramFilesDir & "\Java\jre6\bin\jusched.exe"
    $sWindow[6] = "Java"
    $sfile[7] = @ProgramFilesDir & "\Nitro PDF\Professional\NitroPDFPrinterMonitor.exe"
    $sWindow[7] = "Nitro PDF Printer Monitor"
    $sfile[8] = @ProgramFilesDir & "\BBC Alerts\BBC_Alerts.exe"
    $sWindow[8] = "BBC_Alerts"
    $sfile[9] = @ProgramFilesDir & "\AutoIt3\SciTE\SciTE.exe"
    $sWindow[9] = "SciTE Script Editor"
    $sfile[10] = @ProgramFilesDir & "\OpenXPos\OpenXPos.exe"
    $sWindow[10] = "OpenXpos"
    $sfile[11] = @ProgramFilesDir & "\AutoIt3\Au3Info.exe"
    $sWindow[11] = "Autoit Window Info"
    $sfile[12] = @SystemDir & "\taskmgr.exe"
    $sWindow[12] = "Task Manager"

    For $x = 1 To UBound($sfile) - 1
        If Not FileExists($sfile[$x]) Then MsgBox(4096, "ALERT,     ALERT,       ALERT", $sfile[$x] & " Does NOT exists", 10)
    Next

    $sSwitch[2] = @ProgramFilesDir & "\OpenXpos Head Office\"
    $sSwitch[3] = "c:\"
    $sSwitch[4] = "c:\"
    $sSwitch[5] = "c:\"
    $sSwitch[6] = "c:\"
    $sSwitch[7] = "c:\"
    $sSwitch[8] = "c:\"
    $sSwitch[9] = "c:\"
    $sSwitch[10] = @ProgramFilesDir & "\OpenXPos\"
    $sSwitch[11] = "c:\"
    $sSwitch[12] = "c:\"
    ;$sSwitch[13]  ="c:\"
    ;Exit
EndFunc   ;==>startup

Func monrun() ; checks if the files exists and displays if missing
    For $x = 1 To UBound($f) - 3
        If Not FileExists($f[$x]) Then ShellExecute("explorer", $d[$x])
    Next
    For $x = 1 To UBound($f) - 3
        If Not FileExists($f[$x]) Then MsgBox(4096, "ALERT,     ALERT,       ALERT      MONRUN", $f[$x] & " Does NOT exists", 10)
        If Not FileExists($f[$x]) Then Exit
    Next
EndFunc   ;==>monrun

Func eod() ; checks if the files exists and displays if missing, then closes Autoit Window
    For $x = 8 To UBound($f) - 1
        If Not FileExists($f[$x]) Then ShellExecute("explorer", $d[$x])
    Next
    For $x = 8 To UBound($f) - 1
        If Not FileExists($f[$x]) Then MsgBox(4096, "ALERT,     ALERT,       ALERT      EOD", $f[$x] & " Does NOT exists", 10)
        If Not FileExists($f[$x]) Then Exit
    Next
    If WinExists("AutoIt v3 Window Info") Then
        WinClose("AutoIt v3 Window Info")
        WinWaitClose("AutoIt v3 Window Info", "", 5)
    EndIf
EndFunc   ;==>eod

Func testing()
    $answer = xMsgBox(16 + 0x200, "Tony's Script", "Are you just testing the script?." & @CRLF & @CRLF, "YES", "NO", "")
    If $answer = 7 Then $who = "xxx; xxx; xxx" ; no
    ;
    If $answer = 6 Then $who = "xxx;xxx;xxx" ; yes
EndFunc   ;==>testing

Func folders() ; open explorers ready to use
    ShellExecute("explorer", "O:\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $backup & "Batch files\Shoots\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $eod, "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $s_f, "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $backup & "Downloads\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", "O:\SPREADSHEETS FOR IMPORT\", "", "", @SW_MINIMIZE)
    ShellExecute("explorer", $backup, "", "", @SW_MINIMIZE)
    ShellExecute("explorer", "O:\Tony\NPR_PFA\", "", "", @SW_MINIMIZE)
EndFunc   ;==>folders

Func shutrestart() ; shutsdown any listed programs
    If WinExists("Mozilla") Then
        WinSetState("Mozilla", "", @SW_RESTORE)
        WinClose("Mozilla")
        ProcessWaitClose("firefox.exe", 30)
        If ProcessExists("firefox.exe") Then MsgBox(4096, "ALERT,     ALERT,       ALERT", "Problem closing Firefox", 10)
    EndIf
    If WinExists("Post-it") Then WinClose("Post-it")
    WinWaitClose("Post-it", "", 5)
    If WinExists("Microsoft Outlook") Then
        WinSetState("Microsoft Outlook", "", @SW_RESTORE)
        WinActivate("Microsoft Outlook")
        Send("!fx")
        ProcessWaitClose("outlook.exe", 30)
        If ProcessExists(".exe") Then MsgBox(4096, "ALERT,     ALERT,       ALERT", "Problem closing Outlook", 10)
    EndIf
    If WinExists("[CLASS:SciTEWindow]") Then
        WinSetState("[CLASS:SciTEWindow]", "", @SW_RESTORE)
        WinActivate("[CLASS:SciTEWindow]")
        Send("!fx")
        ProcessWaitClose("scite.exe", 30)
    EndIf
    If WinExists("OpenXpos Head Office") Then
        WinActivate("OpenXpos Head Office")
        Sleep(750)
        $size = WinGetPos("OpenXpos Head Office")
        Sleep(200)
        MouseClick("left", $size[0] + 15, $size[1] + 30) ; click yes
        Sleep(750)
        MouseClick("left", $size[0] + 45, $size[1] + 95) ; click yes
        WinWaitClose("OpenXpos Head Office", "", 5)
        If WinExists("", "You still have program Running in OpenXPos") Then
            MsgBox(0, "Found One!.", "Click OK." & @CRLF & "This will time out in 10 seconds", 10)
            WinActivate("", "You still have program Running in OpenXPos")
            MouseClick("left", 600, 565)
        EndIf
        WinWaitClose("OpenXpos Head Office", "", 25)
    EndIf
    If WinExists("OpenXPos Store") Then
        WinActivate("OpenXPos Store")
        Sleep(750)
        $size = WinGetPos("OpenXpos Head Office")
        Sleep(200)
        MouseClick("left", $size[0] + 15, $size[1] + 30) ; click yes
        Sleep(750)
        MouseClick("left", $size[0] + 45, $size[1] + 95) ; click yes
        WinWaitClose("OpenXPos Store", "", 5)
        If WinExists("", "You still have program Running in OpenXPos") Then
            MsgBox(0, "Found One!.", "Click OK." & @CRLF & "This will time out in 10 seconds", 10)
            WinActivate("", "You still have program Running in OpenXPos")
            MouseClick("left", 600, 565)
        EndIf
        WinWaitClose("OpenXPos Store", "", 25)
    EndIf
    If WinExists("AutoIt Help") Then
        WinClose("AutoIt Help")
        WinWaitClose("AutoIt Help", "", 5)
    EndIf
    If WinExists("AutoIt v3 Window Info") Then
        WinClose("AutoIt v3 Window Info")
        WinWaitClose("AutoIt v3 Window Info", "", 5)
    EndIf

    MsgBox(0, "Continue Restart?", "Click on OK to continue the RESTART sequence" & @CRLF & "This will time out in 30 seconds", 30)
EndFunc   ;==>shutrestart

Func myadlib()
    If WinExists("Security Alert") Then
        WinActivate("Security Alert")
        Send("!y")
    EndIf
    If WinExists("Business Objects") Then
        WinMove("Business Objects", "", 0, 0)
        Send("!r")
    EndIf
    If WinExists("File already exists") Then
        WinActivate("File already exists")
        Send("y")
    EndIf
    If WinExists("", "This report has been modified") Then
        WinActivate("", "This report has been modified")
        Send("!n")
    EndIf
    If WinExists("frmLogin") Then
        WinActivate("frmLogin")
        Send("81{ENTER}")
        Sleep(500)
        Send("blotto{ENTER}")
    EndIf
EndFunc   ;==>myadlib
Link to comment
Share on other sites

  • Moderators

happy2help,

I am still somewhat confused by your code structure. You are using functions to set up your initial variables - which is one reason why you are having to declare so many of them as Global to begin with. Perhaps if you started your code as follows:

#include <Date.au3>
#include <File.au3>
#include "CustomMsgBox.au3"
#include <Misc.au3>
#include "Busy.au3"
#include <Array.au3>
#include <Outlook.au3>

Opt("SendKeyDelay", 50) ;50 milliseconds
Opt("TrayIconDebug", 1) ;0=no info, 1=debug line info
Opt("WinWaitDelay", 750) ;250 milliseconds

; Declare Global constants and variables
Const $num = 13
Global $sfile[$num], $sWindow[$num], $sSwitch[$num], $f[10], $d[10]

HotKeySet("{ESC}", "Exit1")
Func Exit1()
    Exit
EndFunc   ;==>Exit1

; None of the following have to be declared Global because they will be Global automatically - but it is good practice

Global $yesterday1   = _DateTimeFormat(_DateAdd("D",-1,_NowCalcDate()),0)
Global $yesterday    = StringReplace($yesterday1, "/", "-")
;MsgBox(0, "This Should be SUNDAY's Date", $yesterday, 5)
Global $today = @MDAY & "-" & @MON & "-" & @YEAR

Global $pcent1 = 100 / $num
Global $pcent = 100 / $num

Global $body = "Here is today’s product changes." & @CRLF & @CRLF
Global $month = _DateMonthOfYear(@MON, 0)
Global $s_f = "S:\"
Global $wash = "Washington"
Global $stan = "Stanmore"
Global $cob = "Cobham"
Global $d_i = "\DUMPED ITEMS\"
Global $w_r = "\Wastage Report\"
Global $dir = $s_f & $wash & $d_i & @YEAR & "\"
Global $dir1 = $s_f & $stan & $d_i & @YEAR & "\"
Global $dir2 = $s_f & $cob & $d_i & @YEAR & "\"
Global $dir3 = $s_f & $wash & $w_r & @YEAR & "\"
Global $dir4 = $s_f & $stan & $w_r & @YEAR & "\"
Global $dir5 = $s_f & $cob & $w_r & @YEAR & "\"
Global $dir6 = $s_f & "Branch Reports\TOP 10 BY DEPT\" & @YEAR & "\"

; variables for Crystal Reports and emails
Global $crprog = "C:\Program Files\Business Objects\BusinessObjects Enterprise 12.0\win32_x86\crw32.exe"
Global $crwin = "Crystal Reports"
Global $p_c_r = "Product Changes Report"
Global $ops = "O:\"
Global $eod = $ops & "EOD REPORTS\"
Global $p_c = "P:\" & @YEAR & "\" & $month & "\"
Global $who = "xxx"
Global $who1 = "xxx; xxx;xxx"
Global $who2 = "xxx; xxx; xxx"
Global $backup = "C:\Users\Public\Backup\"

;variables for eod and monrun - these have already been declared Global
$f[1] = $dir & $wash & " Dumped WE " & $yesterday & ".pdf"
$d[1] = $dir & $wash
$f[2] = $dir2 & $cob & " Dumped WE " & $yesterday & ".pdf"
$d[2] = $dir2 & $cob
$f[3] = $dir1 & $stan & " Dumped WE " & $yesterday & ".pdf"
$d[3] = $dir1 & $stan
$f[4] = $dir3 & $wash & " Wastage Report WE " & $yesterday & ".pdf"
$d[4] = $dir3 & $wash
$f[5] = $dir5 & $cob & " Wastage Report WE " & $yesterday & ".pdf"
$d[5] = $dir5 & $cob
$f[6] = $dir4 & $stan & " Wastage Report WE " & $yesterday & ".pdf"
$d[6] = $dir4 & $stan
$f[7] = $dir6 & "top 10 by dept we " & $yesterday & ".pdf"
$d[7] = $dir6
$f[8] = $p_c & "Product Changes Report-" & $today & ".pdf"
$d[8] = $p_c
$f[9] = $p_c & "Price Changes Report-" & $today & ".pdf"
$d[9] = $p_c

All this section is now part of your main script and so the variables are available to any of the functions in your script. You might be thinking - "this is big chunk of code to scroll over each time" - so look at the #Region/#EndRegion directives in SciTE which allow you to fold your code. :)

Your code then continues with a series of functions (startup, monrun, eod, testing, folders, shutrestart, myadlib) but there appears no way to get into them. :) Are you intending to create a GUI to choose what to run? Or do they all run sequentially without user intervention? Do they call each other? If I could have a little more information, I might be able to offer more help!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi All

I need some help. I have a main script file called shoots.au3 which has all the common variables and commands and then scriptlets which run sections of the shoots.au3.

The problem i have is that i seem to have to declare all the variables that i might use at the top of the script before using them.

As i explained, this script is just storing all the functions that small scripts call. That way i don't need to repeat the same code in several scripts (a bit like a UDF)

Also thanks for the #Region/#EndRegion help. i have seen it before, but never knew it's true use.

Link to comment
Share on other sites

  • Moderators

happy2help ,

I must have missed that bit, sorry. :)

However, it does make your life easier because you can pass parameters and use ByRef if you want to modify a variable. Let me explain....

Your main script (shoots.au3) calls a function in this include file - because that is what it is as far as shoots.au3 is concerned - and passes the parameters to be used. The all the variables in your include file can be Local - easy!

An example:

; Main script
;...code
#include <scriptlets.au3>  ; the file we have been discussing
;...code
uVar(variable_list)
;...code

; Scriptlets.au3
Func uVar(matching variable list)
    ;...code
EndFunc

The key here is the 2 variable lists. Any variable that you want the function to modify - which in this case is probably all of them - must be passed ByRef, which just means that if the function changes the value it also changes the value of the variable in the main script. A short example:

$one = 1
$two = 2
consolewrite($one & " - " & $two & @CRLF)
test_byref($one, $two)
consolewrite($one & " - " & $two & @CRLF)

Func test_byref(ByRef $var1, $var2) ; Note only the first is passed ByRef
    $var1 = 10
    $var2 = 20
EndFunc

I hope that makes ByRef clear. And as an added bonus, you do not have to declare variables in the parameter - they are all automatically Local unless set to Global by the use of ByRef.

Ah, you say, but I have many, many variables to pass. Well, there is an aswer to that as well - use an array.

Dim $aArray[3]
$aArray[1] = 1
$aArray[2] = 2
consolewrite($aArray[1] & " - " & $aArray[2] & @CRLF)
test_byref($aArray)
consolewrite($aArray[1] & " - " & $aArray[2] & @CRLF)

Func test_byref(ByRef $aFunc_Array)
    $aFunc_Array[1] = 10
    $aFunc_Array[2] = 20
EndFunc

Looking at your scriptlet.au3 again, I would not advise calling the Uvar functions from within each other - the number of parameters to pass would get too large. Just get the main script to call them in sequence.

I believe we are on the right track now. Ask if anything is unclear.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

happy2help,

It appears you do not understand how the Global/Local keywords work.

- Any variable that is set (that is given a value: $variable = something) in the main part of the script is automatically a Global variable.

- Any variable that is set in a function is automatically a Local variable and is lost when the function ends.

- You can override this with the Global/Local keywords (although SciTE is likely to complain if you use them in the wrong sense!)

So, in your script the variable $month is first defined in function uVar. As it therefore becomes a Local variable by default, it is destroyed when you exit the function. So when you subsequently try and use it in function uVar2, there is nothing to use! :)

Best practise is to declare all variables using the Global and Local keywords so there is no ambiguity. It is also a good idea to use as few Global variables as you can get away with - although this is harder to achieve than you might think.

Remember that you can pass variables as parameters to other functions - and if you use the ByRef keyword you can alter the value within a function as well.

So in your case, look carefully at the variables and see which ones need to be Global - judging from the list you have had to declare the answer is "quite a lot". In that case, consider whether your code could be better structured. There are many ways to do things and although there is no "right" answer, having umpteen Global variables in 273 lines of code is perhaps less than optimal....... :)

I hope this helps. Ask if anything is unclear.

M23

the best and simplest explanation ever!

help me to understand clearly.

thanks.

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