One of the most powerful features of AutoHotkey is the ability to use Object-Oriented Programming (OOP) to create objects from classes.
In this tutorial we will use AutoHotkey input boxes to create objects with the input we enter into the boxes.
Using objects will make it easier for us to include logic that can analyze the data and make decisions based on the object’s properties. It organizes the data and makes it more easily transfer to a form or spreadsheet.
Imagine our job is to enter the results of sports games into a database. Our company requires us to record the name of the sport (baseball, football, etc.), the two team names and who won, and to create a summary of the game including game statistics (the total number of home runs for baseball, the total number of touch downs for football, etc.)
Let’s take a look at a few ways we can do this with input boxes.
Passing Data from Multiple Input Boxes into an Object
Let’s first start by creating a Game class. It looks like this.
class Game {
sport := ""
teamNameA := ""
teamNameB := ""
teamAPoints := 0
teamBPoints := 0
totalHomeRuns := 0
totalTouchdowns := 0
getWinner() {}
getLoser() {}
getSummary() {}
}
Let’s fill out the methods getWinner and getLoser. These methods return the team names of the winner and loser, respectively.
getWinner() {
if (this.teamAPoints > this.teamBPoints) {
return this.teamNameA
}
return this.teamNameB
}
getLoser() {
if (this.teamAPoints > this.teamBPoints) {
return this.teamNameB
}
return this.teamNameA
}
Let’s also add standard setters and getters for the properties.
; Setters
setSport(sportName) { this.sport := sportName }
setTeamNameA(teamName) { this.teamNameA := teamName }
setTeamNameB(teamName) { this.teamNameB := teamName }
setTeamAPoints(points) { this.teamAPoints := points }
setTeamBPoints(points) { this.teamBPoints := points }
setTotalHomeRuns(homeRuns) { this.totalHomeRuns := homeRuns }
setTotalTouchdowns(touchdowns) { this.totalTouchdowns := touchdowns}
; Getters
getSport() { return this.sport }
And finally, let’s fill out the getSummary method.
getSummary() {
winner := this.getWinner()
loser := this.getLoser()
summary := "The " . winner . " won against the " . loser . ". "
summary += "The score was " . this.teamAPoints . " to " . this.teamBPoints . ". "
stats := ""
Switch this.sport {
Case: "baseball":
stats := "There were " . this.homeRuns . " home runs in the game."
Case: "football":
stats := "There were " . this.touchdowns . " touchdowns in the game."
}
summary += stats
return summary
}
The getSummary method returns a slightly different message depending on the sport type. It will always begin, as for example, “The Cleveland Guardians won against the New York Yankees. The score was 3 to 6.” The third sentence will change depending on the sports type. If it’s a baseball game, it will say something like, “There were 4 home runs in the game”, and if it’s a football game, it will say something like “There were 5 touchdowns in the game.”
The Game class now looks like this.
class Game {
sport := ""
teamNameA := ""
teamNameB := ""
teamAPoints := 0
teamBPoints := 0
totalHomeRuns := 0
totalTouchdowns := 0
getWinner() {
if (this.teamAPoints > this.teamBPoints) {
return this.teamNameA
}
return this.teamNameB
}
getLoser() {
if (this.teamAPoints > this.teamBPoints) {
return this.teamNameB
}
return this.teamNameA
}
getSummary() {
winner := this.getWinner()
loser := this.getLoser()
summary := "The " . winner . " won against the " . loser . ". "
summary += "The score was " . this.teamAPoints . " to " . this.teamBPoints . ". "
stats := ""
Switch this.sport {
Case: "baseball":
stats := "There were " . this.homeRuns . " home runs in the game."
Case: "football":
stats := "There were " . this.touchdowns . " touchdowns in the game."
}
summary += stats
return summary
}
; Setters
setSport(sportName) { this.sport := sportName }
setTeamNameA(teamName) { this.teamNameA := teamName }
setTeamNameB(teamName) { this.teamNameB := teamName }
setTeamAPoints(points) { this.teamAPoints := points }
setTeamBPoints(points) { this.teamBPoints := points }
setTotalHomeRuns(homeRuns) { this.totalHomeRuns := homeRuns }
setTotalTouchdowns(touchdowns) { this.totalTouchdowns := touchdowns}
}
We now have a class from which we can create an object to store all of the game’s data. But how do we get the data? From the user. We will pass it to our script with input boxes.
The simplest way to create an input box in AutohotKey is in the format:
InputBox, variableName, text shown in the input box
Let’s add a bunch of input boxes that will store the data we need.
; Ask the user for the sport name, e.g. baseball
InputBox, sportName, What is the sport?
; Ask the user for one team's name, e.g. Cleveland Guardians
InputBox, teamNameA, What was one team's name?
; Ask the user for the team's total points, e.g. 8
InputBox, teamAPoints, How many points did they score?
; Ask the user for the other team's name, e.g. New York Yankees
InputBox, teamNameB, What was the other team's name?
; Ask the user for the team's total points, e.g. 6
InputBox, teamBPoints, How many points did they score?
; Ask the user the number of home runs, e.g. 3
InputBox, totalHomeRuns, What was the total number of home runs in the game between both teams?
; Ask the user the number of touchdowns, e.g. 4
InputBox, totalTouchdowns, What was the total number of touchdowns in the game between both teams?
Excellent! Thanks to the input boxes, we now have data to put into our object. Let’s create the object and put the data inside using its setter methods.
; Create a game object
game := New Game()
; Set the sport
game.setSport(sportName)
; Set the first team
game.setTeamNameA(teamNameA)
; Set their score
game.setTeamAPoints(teamAPoints)
; Set the other team
game.setTeamNameB(teamNameB)
; Set their score
game.setTeamBPoints(teamBPoints)
; Set the total home runs
game.setTotalHomeRuns(totalHomeRuns)
; Set the total touchdowns
game.setTotalTouchdowns(totalTouchdowns)
Now that all of the data is in the object, we can call the getSummary method to process it and return the game summary that our job requires.
; Get the game summary
gameSummary := game.getSummary()
To continue with our baseball example, getSummary would return a message like this: “The Cleveland Guardians won against the New York Yankees. The score was 8 to 6. There were 3 home runs in the game.”
The game object now has all of the data required for you to complete your job. Use the object’s getters methods to enter the data into your company’s online form or spreadsheet.
The final script looks like this:
; Ask the user for the sport name, e.g. baseball
InputBox, sportName, What is the sport?
; Ask the user for one team's name, e.g. Cleveland Guardians
InputBox, teamNameA, What was one team's name?
; Ask the user for the team's total points, e.g. 8
InputBox, teamAPoints, How many points did they score?
; Ask the user for the other team's name, e.g. New York Yankees
InputBox, teamNameB, What was the other team's name?
; Ask the user for the team's total points, e.g. 6
InputBox, teamBPoints, How many points did they score?
; Ask the user the number of home runs, e.g. 3
InputBox, totalHomeRuns, What was the total number of home runs in the game between both teams?
; Ask the user the number of touchdowns, e.g. 4
InputBox, totalTouchdowns, What was the total number of touchdowns in the game between both teams?
; Create a game object
game := New Game()
; Set the sport
game.setSport(sportName)
; Set the first team
game.setTeamNameA(teamNameA)
; Set their score
game.setTeamAPoints(teamAPoints)
; Set the other team
game.setTeamNameB(teamNameB)
; Set their score
game.setTeamBPoints(teamBPoints)
; Set the total home runs
game.setTotalHomeRuns(totalHomeRuns)
; Set the total touchdowns
game.setTotalTouchdowns(totalTouchdowns)
; Get the game summary
gameSummary := game.getSummary()
; Save the game summary to a form, spreedsheet row, etc.
class Game {
sport := ""
teamNameA := ""
teamNameB := ""
teamAPoints := 0
teamBPoints := 0
totalHomeRuns := 0
totalTouchdowns := 0
getWinner() {
if (this.teamAPoints > this.teamBPoints) {
return this.teamNameA
}
return this.teamNameB
}
getLoser() {
if (this.teamAPoints > this.teamBPoints) {
return this.teamNameB
}
return this.teamNameA
}
getSummary() {
winner := this.getWinner()
loser := this.getLoser()
summary := "The " . winner . " won against the " . loser . ". "
summary += "The score was " . this.teamAPoints . " to " . this.teamBPoints . ". "
stats := ""
Switch this.sport {
Case: "baseball":
stats := "There were " . this.homeRuns . " home runs in the game."
Case: "football":
stats := "There were " . this.touchdowns . " touchdowns in the game."
}
summary += stats
return summary
}
; Setters
setSport(sportName) { this.sport := sportName }
setTeamNameA(teamName) { this.teamNameA := teamName }
setTeamNameB(teamName) { this.teamNameB := teamName }
setTeamAPoints(points) { this.teamAPoints := points }
setTeamBPoints(points) { this.teamBPoints := points }
setTotalHomeRuns(homeRuns) { this.totalHomeRuns := homeRuns }
setTotalTouchdowns(touchdowns) { this.totalTouchdowns := touchdowns}
; Getters
getSport { return this.sport }
}