Feedback from writing a short app

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Franktic
Posts: 14
Joined: 08 Oct 2018, 22:35

Feedback from writing a short app

31 Oct 2018, 00:32

I'm not sure if this is the correct board to submit this to but here goes. I wrote a short app to test: 1. using functions to control guis; 2. swapping between multiple guis; 3. saving responses to a file; 4. reading and displaying responses from a file.

Code: Select all

#SingleInstance, Force

Global h11Text, h12Text, h21Text, h22Text := ""
Global v1, v2, v3, v4 := ""

Global whereAmI := 0

Global stringToSave := ""
Global StringArray := []
Global selectedFile := ""
Global loadedFile := False

main()

main(){
    showgui1() ; show the gui without the save function
}

ShowGui1(){
    global   

    WhereAmI := 1
    Gui 1:Destroy
    Gui 1:Default
    
    Gui Add, Edit, r5 vh11Text,
    Gui Add, Edit, r5 vh12Text,

    Gui Add, Button, gGoTo2, Next
    Gui Add, Button, gLoad, Load
    Gui Add, Button, gSaveToFile, Save
    Gui Add, Button, gQuit, Quit

    Gui, 1:Show, w400 h400, Gui 1
    loadfields()
    return
}

ShowGui2(){
    global
    
    WhereAmI := 2
    Gui 2:Destroy
    Gui 2:Default

    Gui Add, Edit, r2 vh21Text,
    Gui Add, Edit, r2 vh22Text,
    
    Gui Add, Button, gGoTo1, Next
    Gui Add, Button, gSaveToFile, Save
    Gui Add, Button, gQuit, Quit

    Gui, 2:Show, w400 h400 Center, Gui 2
    loadfields()
    return
}

Load(){
    Global
    
    FileSelectFile, selectedFile 
   
    FileRead, fileData, %selectedFile% 
    
    If (selectedFile != "") {   ; if a file has been selected 
        LoadedFile := True    ; we have loaded the file
    }
    
    StringArray:= StrSplit(fileData, "|") ;split the file using the | separator
    v1 := StringArray[1]
    v2 := StringArray[2]
    v3 := StringArray[3]
    v4 := StringArray[4]
    
        loadfields()
    return
}

Goto1(){
    SaveData()
    Gui 2:Destroy
    ShowGui1()
    return
}

Goto2(){
    SaveData()
    Gui 1:Destroy
    ShowGui2()
    return
}

LoadFields(){
    Global
    
        if (whereAmI = 1) {
            GuiControl, 1:, h11Text, % v1
            GuiControl, 1:, h12Text, % v2
        }
        Else  if (whereAmI = 2) {
            GuiControl, 2:, h21Text, % v3
            GuiControl, 2:, h22Text, % v4
        }

    return
}

Quit(){
    ExitApp
}

SaveData(){

    Global

    if (whereAmI = 1){
        GuiControlGet v1,,h11Text
        GuiControlGet v2,,h12Text
    }
    Else {
        GuiControlGet v3,,h21Text
        GuiControlGet v4,,h22Text
    }
    
    return
}

SavetoFile(){
    StringToSave := ""
    StringToSave := v1 "|" v2 "|" v3 "|" v4
            
    FileDelete %A_ScriptDir%\test.txt
    FileAppend, %StringToSave%, %A_ScriptDir%\test.txt
    
    return
}


#z::ExitApp
From this short exercise I found AutoHotKey to be very powerful and somewhat fun to write in. I'm using the SmartGui IDE to do my coding, although I didn't use the WYSIWYG gui builder for this simple exercise. Aspects of AutoHotKey I had problems with though were: 1. a gui object cannot pass parameters to functions. Functions are treated as labels. 2. Arrays are treated differently to variables. I couldn't get the array elements to work in many cases and had to resort to loading the elements into separate variables (v1 to v4). 3. The requirement for commas after commands is vague and often leads to confusion. 4. In a similar vein, the requirement to use the % sign to force expression mode is also vague and appears to work inconsistently. 5. Although I didn't use this in my app, the naming of gui controls can be confusing. For example buttonOK, then 2buttonOK, 3buttonOK etc. is not intuitive.

Invariably I had to have the documentation page open for almost everything I did. I am a newbie to ahk, however I felt that the inconsistencies I found would have me working this way for some time. As I use ahk for hobby programming and a little for 'real' work, I would like to know if my issues are genuine or borne out of ignorance of the language. If they are real issues, does V2 fix all or any of those? Thanks for reading.
User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: Feedback from writing a short app

31 Oct 2018, 16:36

try this

#SingleInstance, Force

ShowGui1()
return

ShowGui1()
{
global
whereAmI = 1
Gui 1:Default
Gui 1:Add, Edit, r5 vh11Text,
Gui 1:Add, Edit, r5 vh12Text,
Gui 1:Add, Button, gGoTo2, Next
Gui 1:Add, Button, gLoadFields, Load
Gui 1:Add, Button, gSaveData, Save
Gui 1:Add, Button, gQuit, Quit
Gui 1:Show, w400 h400, Gui 1
loadfields()
return
}

ShowGui2()
{
global
whereAmI = 2
Gui 2:Default
Gui 2:Add, Edit, r2 vh21Text,
Gui 2:Add, Edit, r2 vh22Text,
Gui 2:Add, Button, gGoTo1, Next
Gui 2:Add, Button, gSaveData, Save
Gui 2:Add, Button, gQuit, Quit
Gui 2:Show, w400 h400 Center, Gui 2
loadfields()
return
}

LoadFields()
{
Global
Gui %whereAmI%: Submit, NoHide ; save the text in vh12Text in variable called h12Text, and for all other controls
Loop, 2
GuiControl, %whereAmI%:, % "h" whereAmI A_Index "Text", % _IniRead("h" whereAmI A_Index "Text")
return
}

SaveData(){
Global
Gui %whereAmI%: Submit, NoHide ; save the text in vh12Text in variable called h12Text, and for all other controls
Loop, 2
_IniWrite("h" whereAmI A_Index "Text")
return
}

_IniWrite(var)
{
IniWrite, % %var%, Setting.ini, Settings, % var
return
}

_IniRead(var)
{
IniRead, var, Setting.ini, Settings, % var
return % var
}


Goto1(){
SaveData()
Gui 2:Destroy
ShowGui1()
return
}

Goto2(){
SaveData()
Gui 1:Destroy
ShowGui2()
return
}


Quit()
{
GuiClose:
2GuiClose:
ExitApp
}


#z::ExitApp
:wave: There is always more than one way to solve a problem. ;)
Franktic
Posts: 14
Joined: 08 Oct 2018, 22:35

Re: Feedback from writing a short app

01 Nov 2018, 03:00

Hi YoucefHam - Thanks for your response. My code certainly works but yours is more elegant using an ini file instead of a standard text file. The point I was making was that ahk is a great language but has some issues. I wanted to know if it V2 cleared up the issues I raised or if I should look elsewhere.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ccqcl, Google [Bot], Rohwedder and 183 guests