Jump to content

Ways to manage big code/functions


badapple89
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

So I've made an "IT helper" that we can use to fix little issues around our work that basically have no real fix and just saves us clicks.

Its a basic GUI with buttons and each button runs a little script (function) and sometimes its own little gui for input. But its getting too long and unmanagable.

As each little script can be taken out and run/saved as its own autoit au3 file can I make it so all scripts are stored separately to the main GUI and it just launches them when clicked? I know I could just map each button to a file path of the separate files. But thats not very safe/fool proof.

Should I be looking at something like <#include> to include each seperate script? or do something with #AutoIt3Wrapper

For reference my a qucik persudo code of script is. As you can see with proper functions it gets quite long.

Button1 (func1)
Button2 (func2)
...
ButtonN (funcN)

Func1
Do this
and this
endfunc

Func2
Do that
or that
endfunc

FuncN
...
EndFuc
Link to comment
Share on other sites

Why do you want to separate them? Because the script is getting long? You can fold the functions in SciTE so you don't have to scroll through them. 

Or, if you really need to have them out of the main script, put them into their own files and use #include to add them to the main one. That way you can maintain each function separately in case things change. Look at the format of the standard includes as to how to use them that way. 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

badapple89,

Its a basic GUI with buttons and each button runs a little script (function)

 

Are these scripts or functions? 

As you can see with proper functions it gets quite long.

 

100 lines?  1000 lines?  What is long to you?

Its a basic GUI with buttons and each button runs a little script (function)

 

When you want to add/delete a function you have to change the gui.  And at some point you will run out of screen space.

The following script shows how to use an include for your "functions".  It also uses a listview driven by an array instead of buttons for the "functions" to perform.  This is just how I did it for the sake of illustration.  Note that the form of the #include is significant and that it cannot be a variable.  See the Help file for details.

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <ListviewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include "MyFunctions.au3"      ; <-----  This contains your functions and is in the same directory that the script is running from

; the following array holds your function name, a description for the listview item and a control id.

local $aTaskTBL[100][3] = [ _
                            ['PRT_RST','Printer Reset',0], _
                            ['Slo_Joe',"Print Joe's Report",0], _
                            ['TERM_RST','Reset Terminal',0], _
                            ['freecell','Play Freecell',0], _
                            ['AM_Report','Morning Report',0], _
                            ['Add_CHG','Create Change Record',0], _
                            ['Add_Prob','Create Problem Record',0], _
                            ['PWD_RST1','Reset Password (ACF2)',0], _
                            ['PWD_RST2','Reset Password (Workstation)',0], _
                            ['Arc_dal','Archive Daily Bitch Emails',0], _
                            ['Status_report','Produce Status Report',0]]

local $gui010   =   guicreate('My Little IT Helper',300,500)
local $lv010    =   guictrlcreatelistview('Task To Run',0,30,300,400,-1,bitor( $LVS_EX_CHECKBOXES, $WS_EX_CLIENTEDGE, $lvs_ex_gridlines ))
                    guictrlsetbkcolor(-1,$GUI_BKCOLOR_LV_ALTERNATE)
                    guictrlsetfont(-1,10,600)
local $btn010   =   guictrlcreatebutton('Run Selected Functions',20,460,260,20)

; populate the listview based on the above array

_GUICtrlListView_BeginUpdate($lv010)
for $1 = 0 to ubound($aTaskTBL) - 1
    if $aTaskTBL[$1][0] = '' then exitloop
    $aTaskTBL[$1][2] = guictrlcreatelistviewitem($aTaskTBL[$1][1],$lv010)
    guictrlsetbkcolor(-1,0xc0dcc0)
next
_GUICtrlListView_SetColumnWidth($lv010,0,$LVSCW_AUTOSIZE_USEHEADER)
_GUICtrlListView_EndUpdate($lv010)

guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
        case $btn010

            ; read each listview item and "call" the procedure from the array if checked

            for $1 = 0 to _GUICtrlListView_GetItemCount($lv010)
                if _GUICtrlListView_GetItemChecked($lv010,$1) then
                    call($aTaskTBL[$1][0])
                    _GUICtrlListView_SetItemChecked($lv010,$1,false)
                endif
            Next
    EndSwitch
wend

And the inlude file (MyFunctions.au3)

func prt_rst()
    ConsoleWrite('prt_rst' & @LF)
endfunc
func Slo_Joe()
    ConsoleWrite('slo_joe' & @LF)
endfunc
func TERM_RST()
    ConsoleWrite('term_rst' & @LF)
endfunc
func freecell()
    ConsoleWrite('freecell' & @LF)
endfunc
func AM_Report()
    ConsoleWrite('am_report' & @LF)
endfunc
func Add_CHG()
    ConsoleWrite('add_chg' & @LF)
endfunc
func Add_Prob()
    ConsoleWrite('add_prob' & @LF)
endfunc
func PWD_RST1()
    ConsoleWrite('pwd_rst1' & @LF)
endfunc
func PWD_RST2()
    ConsoleWrite('pwd_rst2' & @LF)
endfunc
func Arc_dal()
    ConsoleWrite('arc_dal' & @LF)
endfunc
func Status_report()
    ConsoleWrite('status_report' & @LF)
endfunc

Copy each piece of code to the same dir and run it to see how it works.

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

Maybe an IDE (Integrated Development Environment) like >ISN helps.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

My browser wigged out when I posted this. Didn't think I created it.

@BrewManNH

I want to be able to run each little thing separately if I want and at times not use the main GUI.

@kylomas

Are these scripts or functions? 

They are functions that called when the button is pressed. Wrong terminology. But most of them are simple and can be pulled out and saved as there own autoit "scripts"

100 lines?  1000 lines?  What is long to you?

800 at the moment. But many things I haven't added coz then it would be 1500+

I did find #include after I posted this.

What I'm thinking though is can I use something like

Button1 (func1)
Button2 (func2)
...
ButtonN (funcN)

Func1
#include "Reset Printer.au3"
endfunc

Func2
#include "Reset Printer.au3"
endfunc

FuncN
#include "Other Seperate Script.au3"
EndFuc

But I have an issue wehen one of these #include scripts contains there OWN functions (mostly these functions only interact with the #include script not main GUI).

Do I need to store the functions in an array in the main GUI and call them when needed as per your script?

 

And @water

I already feel sorry for whoever takes over from me and has to learn AutoIT ISN seems ANOTHER thing that they would have to deal with. But I'll look into it.

 

Sorry if im confusing things and getting it all backwards, Im new to AutoIT and coding in general!

Link to comment
Share on other sites

badapple89,

The #includes happen at compile time, not run time so it makes no sense to put them anywhere but at the top of your script.

But I have an issue wehen one of these #include scripts contains there OWN functions (mostly these functions only interact with the #include script not main GUI).

 

Why is this an issue?

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

You could use an organization like this

Button1 (func1)
...
ButtonN (funcN)

Func1
shellexecute("fullyqualifiedpathtoReset Printer.au3")
endfunc

FuncN
shellexecute("fullyqualifiedpathtoOther Seperate Script.au3")
EndFuc

 

So now you have a library of disparate scripts as opposed to a collection of functions...

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

 

shellexecute("fullyqualifiedpathtoOther Seperate Script.au3")

Means anyone that wants to run the main GUI needs access to that path. And if someone renames/moves one of the .au3 files it would  break.

 

The below code runs how I want it, try! Even though im using it wrong....

;MAINGUI

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Global $mainwindow

;Run the main window GUI
Main_window()

Func Main_window()
    ;Set GUI
    Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
    $mainwindow = GUICreate("Tech Helper", 220, 300)
    ; Set to popup bottom right
    $pos = WinGetPos($mainwindow)
    WinMove($mainwindow, "", @DesktopWidth - $pos[2], @DesktopHeight - $pos[3] - 30)
    WinSetOnTop($mainwindow, "", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close")

    ;Label - text centerd and middle
    GUICtrlCreateLabel("IT STAFF USE ONLY", 0, 20, 220, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    ;Set font
    GUICtrlSetFont(-1, 8.5, 5000)
    ;Set background colour
    GUICtrlSetBkColor(-1, 0xff0000)
    ;Set font colour
    GUICtrlSetColor(-1, 0xFFFFFF)
    ;Show GUI as hidden by default
    GUISetState(@SW_SHOW)

    $butWord = GUICtrlCreateButton("Fix something", 10, 80, 100)
    GUICtrlSetOnEvent($butWord, "Fix")

    ;Idle around only needed for main GUI
    While 1
        Sleep(1000)
    WEnd

EndFunc   ;==>Main_window

Func Fix()
    MsgBox(0, "Some", "Were about to run a fix")
    #include "fix.au3"
EndFunc   ;==>SomeThings

Func On_Close()
    Switch @GUI_WinHandle ; See which GUI sent the CLOSE message
        Case $mainwindow
            Exit ; If it was this GUI - we exit
    EndSwitch
EndFunc   ;==>On_Close
;fix.au3 located in folder

Msgbox (0, "test", "BAM FIXED")

But if I add a function in.

;fix.au3 located in folder

fixit()

Func fixit()
Msgbox (0, "test", "BAM FIXED")
endfunc

I get

fixit() undefined function

and for the Func line

ERROR: syntax error

Link to comment
Share on other sites

function fixit() is located in fix.au3

you cannot call a function in another file unless you have allowed your main script to access those function.

but including the fix.au3 in your main script, it will look for the functions in the fix.au3 file.

you can run fix.au3 on its own but if you dont call any of the functions that fix.au3 has then yes, you are correct. it will not "run" but it will compile.

compiling a function is not calling the function. placing fixit() anywhere in the fix.au3 file will call the function fixit() in that file.

Link to comment
Share on other sites

You have to put the #include lines at the top of your script, the way you're doing it will cause its own set of syntax errors.

When you use "#include" to add a file to your script, the code in the include file is added wherever you put the line in your script. So what you are doing is adding in a function, inside another function, which is a syntax error.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hmmm ok im begging to get my head around this a bit more.

And I think I may have explained a little wrong, the individual "scripts" would also be compiled into there individual exes so they can be run separately.

So if I have a bunch of all these exes I think when the main GUI button is pressed the Fix function could be this.

Func Fix()
    MsgBox(0, "Some", "Were about to run a fix")
    FileInstall ("C:\PATH_ON_MY_LOCAL_C\Fix.exe", "C:\Auto_IT_Temp\", 1)
    shellexecute("C:\Auto_IT_Temp\Fix.exe")
EndFunc   ;==>SomeThings

Little messy I guess, but I think It will do what I want. I can run the exes singularly or as part of MainGUI.

And am I right in thinking that "Fix.exe" will be included when I compile the MainGUI and that other users of MainGUI will NOT need access to my Local C path?

I don't do the FileInstall at the start as there is no need to copy ALL files most times we open it up and run one thing.

Or do you think the time taken to do the FileInstall() in the function is better spent doing fileintall() at the start?

Link to comment
Share on other sites

How many pages of functions do you have if you fold them all up?  800 lines still sounds like something that can be reasonably maintained in a single script, unless of course there are instances in which they would be executed manually outside of your GUI.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Solution

I thought the point was to be able to use these files WITHOUT having to run the GUI front end? Using FileInstall with compiled scripts you now HAVE to run the GUI before those exes will be extracted, you'd have to run it from the GUI at least once.

Sounds like you're changing the goal in the middle of the game to me.

Maybe you should slow down and figure out exactly what it is you want to do first, then explain it as well as you can, because right now you're all over the place with what you're final goal is going to be.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

How about something like this?  You'll want to look at some of the hard-coded values and assumptions I make, but it should do the trick for you...

#include <GuiListBox.au3>
#include <WindowsConstants.au3>

Local Const $WIN_WIDTH = 200
Local Const $WIN_HEIGHT = 200

Local $style = BitOr( _
    $WS_MAXIMIZEBOX, _
    $WS_CAPTION, _
    $WS_SYSMENU, _
    $WS_MINIMIZEBOX, _
    $WS_THICKFRAME _
)
Local $window = GUICreate("Script Runner", $WIN_WIDTH, $WIN_HEIGHT, Default, Default, $style)
Local $listbox = GUICtrlCreateList("", 2, 2, $WIN_WIDTH - 4, $WIN_HEIGHT - 4)
GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
FindScripts()
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState(@SW_SHOW)
Do
    Sleep(1)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func FindScripts()
    Local $search = FileFindFirstFile(@ScriptDir & "\scripts\*")
    If $search = -1 Then
        Return
    EndIf
    Local $data = ""
    While True
        Local $file = FileFindNextFile($search)
        If @error Then ExitLoop

        $data &= @ScriptDir & "\scripts\" & $file & "|"
    WEnd
    GUICtrlSetData($listbox, $data)
    FileClose($search)
EndFunc

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Local $iIDFrom = 0, $iCode = 0

    $iIDFrom = BitAND($iwParam, 0xFFFF)
    $iCode = BitShift($iwParam, 16)

    Switch $iIDFrom
        Case $listbox
            Switch $iCode
                Case $LBN_DBLCLK
                    Local $script = GUICtrlRead($listbox)
                    ShellExecute($script)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc
Edited by mrider

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

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