Difference between revisions of "AutoHotkey"

From HalfgeekKB
Jump to navigation Jump to search
Line 1: Line 1:
  
 +
== Fill in the blank ==
 +
 +
Say I've made a macro that should have some sent keys set by a variable, perhaps in a loop. An example:
 +
 +
<syntaxhighlight lang=autohotkey>
 +
F3::
 +
WinActivate, Notepad
 +
Send, SOMENAME is a model citizen with NUMBER years of experience.{Enter}
 +
Return
 +
</syntaxhighlight>
 +
 +
Simple example of using variables:
 +
 +
<syntaxhighlight lang=autohotkey>
 +
F3::
 +
WinActivate, Notepad
 +
someName = Larry
 +
years = 9
 +
Send, %someName% is a model citizen with %years% years of experience.{Enter}
 +
Return
 +
</syntaxhighlight>
 +
 +
Factored out into a function (note that the name must now have quotes):
 +
 +
<syntaxhighlight lang=autohotkey>
 +
SayThePhrase(someName, years)
 +
{
 +
WinActivate, Notepad
 +
Send, %someName% is a model citizen with %years% years of experience.{Enter}
 +
}
 +
 +
F3::
 +
SayThePhrase("Larry", 9)
 +
Return
 +
</syntaxhighlight>
 +
 +
Looping without the function:
 +
 +
<syntaxhighlight lang=autohotkey>
 +
F3::
 +
nameList = Larry,Moe,Curly
 +
yearsList = 9,12,4
 +
StringSplit, names, nameList, `,
 +
StringSplit, yearses, yearsList, `,
 +
; A note on "arrays": AHK does not have a built-in array type; arrays are
 +
; simulated by concatenating a base variable name with an index. As implemented
 +
; by StringSplit, the array is 1-based and the array length is stored at the
 +
; zero index. In this example, names0 would be set to 3 and names1, names2, and
 +
; names3 would be set to each successive name.
 +
Loop, %names0%
 +
{
 +
; a_index is the loop counter.
 +
someName := names%a_index%
 +
years := yearses%a_index%
 +
WinActivate, Notepad
 +
Send, %someName% is a model citizen with %years% years of experience.{Enter}
 +
}
 +
Return
 +
</syntaxhighlight>
 +
 +
Looping with the function:
 +
 +
<syntaxhighlight lang=autohotkey>
 +
SayThePhrase(someName, years)
 +
{
 +
WinActivate, Notepad
 +
Send, %someName% is a model citizen with %years% years of experience.{Enter}
 +
}
 +
 +
F3::
 +
nameList = Larry,Moe,Curly
 +
yearsList = 9,12,4
 +
StringSplit, names, nameList, `,
 +
StringSplit, yearses, yearsList, `,
 +
Loop, %names0%
 +
{
 +
SayThePhrase(names%a_index%, yearses%a_index%)
 +
}
 +
Return
 +
</syntaxhighlight>
  
 
== See also ==
 
== See also ==

Revision as of 07:11, 20 June 2014

Fill in the blank

Say I've made a macro that should have some sent keys set by a variable, perhaps in a loop. An example:

F3::
WinActivate, Notepad
Send, SOMENAME is a model citizen with NUMBER years of experience.{Enter}
Return

Simple example of using variables:

F3::
WinActivate, Notepad
someName = Larry
years = 9
Send, %someName% is a model citizen with %years% years of experience.{Enter}
Return

Factored out into a function (note that the name must now have quotes):

SayThePhrase(someName, years)
{
	WinActivate, Notepad
	Send, %someName% is a model citizen with %years% years of experience.{Enter}
}
	
F3::
SayThePhrase("Larry", 9)
Return

Looping without the function:

F3::
nameList = Larry,Moe,Curly
yearsList = 9,12,4
StringSplit, names, nameList, `,
StringSplit, yearses, yearsList, `,
; A note on "arrays": AHK does not have a built-in array type; arrays are
; simulated by concatenating a base variable name with an index. As implemented
; by StringSplit, the array is 1-based and the array length is stored at the
; zero index. In this example, names0 would be set to 3 and names1, names2, and
; names3 would be set to each successive name.
Loop, %names0%
{
	; a_index is the loop counter.
	someName := names%a_index%
	years := yearses%a_index%
	WinActivate, Notepad
	Send, %someName% is a model citizen with %years% years of experience.{Enter}
}
Return

Looping with the function:

SayThePhrase(someName, years)
{
	WinActivate, Notepad
	Send, %someName% is a model citizen with %years% years of experience.{Enter}
}
	
F3::
nameList = Larry,Moe,Curly
yearsList = 9,12,4
StringSplit, names, nameList, `,
StringSplit, yearses, yearsList, `,
Loop, %names0%
{
	SayThePhrase(names%a_index%, yearses%a_index%)
}
Return

See also