Pulling Text from a GUI

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
qcgreywolf
Posts: 3
Joined: 16 Apr 2024, 15:37

Pulling Text from a GUI

Post by qcgreywolf » 16 Apr 2024, 15:49

I'm having fun learning v2 of AHK as a coding beginner! I have a script that creates a GUI, and I am using that GUI to launch some company websites and fill out forms quicker. The GUI is always on top, and I fill it out as I get data.

Is there a way to create a HotString that pulls from one of those text boxes? For example, lets say I have a Name, Phone # and Email text box in the GUI. Can I have a HotString that functions like the following?

Code: Select all

::CRM1::FetchDataFromGUI()

FetchDataFromGUI() {
    combinedData := Name.Value . " " . PhoneNumber.Value . " " . EmailAddress.Value
    old_clip := ClipboardAll()
	A_clipboard := combinedData
    send "^v"
    send "{Down}"
	sleep 333  ;or clipwait
	A_Clipboard := old_clip
}
I am trying to find a way to make filling out our companies CRM system faster without using a COM enabled browser or Chrome in debug mode (which I think would push my IT department into "WTF are you doing mode")

If it helps, here is the relevant bit of code within my script

Code: Select all

::CRM1::FetchDataFromGUI()

FetchDataFromGUI() {
    combinedData := MachineSN.Value
    old_clip := ClipboardAll()
	A_clipboard := combinedData
    send "^v"
    send "{Down}"
	sleep 333  ;or clipwait
	A_Clipboard := old_clip
}


; Open Hotline Helper GUI
#h::{
; GetCurrentMonitor: Gets the current monitor index based upon mouse position
GetCurrentMonitor() {
	CoordMode "Mouse", "Screen"
	MouseGetPos &MouseX, &MouseY
	NumMonitors := MonitorGetCount()
	Loop NumMonitors {
		MonitorGet A_Index, &MonitorLeft, &MonitorTop, &MonitorRight, &MonitorBottom
		If (MouseX >= MonitorLeft And MouseX < MonitorRight And MouseY >= MonitorTop And MouseY < MonitorBottom) 
			Return A_Index
	}
Return 1 ; should never really execute
}

; Create a new GUI object
MyGui := Gui("+AlwaysOnTop", "Hotline Helper")

; Set up margins
pad := 10
MyGui.MarginX := MyGui.MarginY := pad

; Add tabs
MyGui.Add("Tab3",, ["Main","Shortcuts","Other"])

; Add controls to the Main GUI
MyGui.Add("Button","Default w80 section", "&TheWorks").OnEvent("Click", TheWorks_Btn)
MyGui.Add("Button","Default w80 ys", "&AIS").OnEvent("Click", AIS_Btn)
MyGui.Add("Button","Default w80 ys", "&CRM").OnEvent("Click", CRM_Btn)


MyGui.Add("Text", "xm y+10", " Machine SN:")
MachineSN := MyGui.Add("Edit","w100 vMachineSN")

MyGui.Add("Text", "xm y+10", " Customer Name:")
CustomerName := MyGui.Add("Edit","w200 vCustomerName")

MyGui.Add("Text", "xm y+10", " Phone Number:")
PhoneNumber := MyGui.Add("Edit","w125 vPhoneNumber")

MyGui.Add("Text", "xm y+10", " Email Address:")
EmailAddress := MyGui.Add("Edit","w200 vEmailAddress")

MyGui.Add("Text","xm y+10", " Alarm:")
Alarm := MyGui.Add("Edit","w300 r3 vAlarm")

MyGui.Add("Text","", " T/S:")
TS := MyGui.Add("Edit","w300 r6 vTS")

MyGui.Add("Text","", " Solution:")
Solution := MyGui.Add("Edit","w300 r4 vSolution")

MyGui.Add("Button","Default w80 section", "&Exit").OnEvent("Click", Exit_Btn)

MyGui.Add("Button","Default w80 ys", "&Wipe").OnEvent("Click", Wipe_Btn)

; Add controls to the Shortcuts GUI


; Show the GUI
CurrentMonitor := GetCurrentMonitor()
MonitorGet CurrentMonitor, &MonitorLeft, &MonitorTop, &MonitorRight, &MonitorBottom
MyGui.GetClientPos(&x, &y, &w, &h)
x := Integer(((MonitorRight - MonitorLeft - w) / 2) + MonitorLeft)
y := Integer(((MonitorBottom - MonitorTop - h) / 2) + MonitorTop)
MyGui.Show("x" x " y" y)


; Define button click functions
Exit_Btn(*) {
    MyGui.Destroy()
}

Wipe_Btn(*) {
    MachineSN.Value := ""  ; Set the edit box's value to an empty string, clearing it
    CustomerName.Value := ""  ; Set the edit box's value to an empty string, clearing it
    PhoneNumber.Value := ""  ; Set the edit box's value to an empty string, clearing it
    EmailAddress.Value := ""  ; Set the edit box's value to an empty string, clearing it
    Alarm.Value := ""  ; Set the edit box's value to an empty string, clearing it
    TS.Value := ""  ; Set the edit box's value to an empty string, clearing it
    Solution.Value := ""  ; Set the edit box's value to an empty string, clearing it
}

AIS_Btn(*) {
    Run "secretwebsiteurl" MachineSN.Value
}

CRM_Btn(*) {
    MsgBox "This doesn't do anything. Yet."
}

TheWorks_Btn(*){
    Run "secretwebsiteurl" MachineSN.Value
    Run "secretwebsiteurl" MachineSN.Value
}

CloseGUI() {
    ExitApp()  ; Close the script
}
}

Return

Panaku
Posts: 22
Joined: 02 Apr 2022, 17:24

Re: Pulling Text from a GUI

Post by Panaku » 16 Apr 2024, 16:19

In AHK v2, GUIs are Objects and their controls belong to those objects. There simple/common ways to access those controls later, including values.

The first way is to give the controls names via the v option, then you can reference the control by using the variable name of the GUI and it's control.

Code: Select all

#Requires AutoHotkey v2+
#SingleInstance Force

MyGui := Gui()
MyGui.Add("Edit", "w300 vEdit1", "")
MyGui.Show()

F1:: {
    MsgBox(MyGui["Edit1"].value)
}
The second way would be to assign the controls to variables themselves, and then you can access that information through that variable.

Code: Select all

#Requires AutoHotkey v2+
#SingleInstance Force

MyGui := Gui()
Edit1 := MyGui.Add("Edit", "w300", "")
MyGui.Show()

F1:: {
    MsgBox(Edit1.value)
}
Just run the script as-is, type something into the edit field, and press F1[\b] and you should see the results.

qcgreywolf
Posts: 3
Joined: 16 Apr 2024, 15:37

Re: Pulling Text from a GUI

Post by qcgreywolf » 17 Apr 2024, 10:18

I think I may have asked my question incorrectly. Here is the GUI
image.png
(32.38 KiB) Downloaded 77 times
What I want to accomplish is have text in those fields, such as in the Alarm field "Some text here that needs to go elsewhere", able to be pasted anywhere with a hotstring. I don't want to click a button or anything on the GUI, I want to be able to actively pull from the text fields in a separate app with a HotString
For example, if I type "alarm1", I want the text from the Alarm field to replace the "Alarm1" text in a word document or a SAP web window.

In my example code above, I made a hotstring and it tells me the variable hasn't been initialized yet... but I have the gui elements all initializing as you show. (I did that originally so I can clear all the fiends whenever I want to).

For example, I have this code;

Code: Select all

::CRM1:: {
    combinedData := MachineSN.Value
    old_clip := ClipboardAll()
	A_clipboard := combinedData
    send "^v"
    send "{Down}"
	sleep 333  ;or clipwait
	A_Clipboard := old_clip
}
and this is earlier in the script;

Code: Select all

MyGui.Add("Text", "xm y+10", " Machine SN:")
MachineSN := MyGui.Add("Edit","w100 vMachineSN")
When I start the script, I get;
image.png
(33.42 KiB) Downloaded 77 times

Panaku
Posts: 22
Joined: 02 Apr 2022, 17:24

Re: Pulling Text from a GUI

Post by Panaku » 17 Apr 2024, 10:56

The error you're receiving, This variable appears to never be assigned a value., is likely happening because of the scope in which you define that variable. If you're creating your GUI, or GUI Controls inside of a function, those variables are only accessible inside of that function unless you define them as global. My recommendation would be to create your GUI in the Auto-Execute section of your script, and to give your variables names via the v option. Then you can access the GUI and it's controls from any function.

Code: Select all

#Requires AutoHotkey v2+
#SingleInstance Force

MyGui := Gui()
Edit1 := MyGui.Add("Edit", "w300", "")
MyGui.Show()

::Alarm1:: {
    Send(Edit1.Value)
}

qcgreywolf
Posts: 3
Joined: 16 Apr 2024, 15:37

Re: Pulling Text from a GUI

Post by qcgreywolf » 18 Apr 2024, 07:57

While this did not work for me (declaring the GUI elements outside of the hotkey function generated a whole pile of errors), it DID lead me down the rabbit hole of learning about how AHK v2 handles Global and Local variables.

Placing Global in front of my text field variables solved everything!

Thanks for guiding me in the correct direction.

Post Reply

Return to “Ask for Help (v2)”