AutoHotkey

From HalfgeekKB
(Redirected from Autohotkey)
Jump to navigation Jump to search

Something's horribly amiss! STOP!

Put this in your script to make it cut out early when you press F8:

F8::ExitApp

Note that this will make the instance of AHK exit, so your script won't run again until you reopen it.

This is an alternative that I'm using on a remote server; it reloads but pauses as it waits (and stays paused if the reload fails). The pause is more or less immediate since the reload is asynchronous.

; This hotkey pauses and reloads the script. If the reload failed, at least the
; script is paused. This is my panic button.
F8::
Reload
; Reload continues running after this pause. When it finishes loading, the
; script is started anew, unpaused.
Pause
Return

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

Packing the names and years together (using splits and sub-splits):

SayThePhrase(someName, years)
{
	WinActivate, Notepad
	Send, %someName% is a model citizen with %years% years of experience.{Enter}
}

F3::
statListList := "Larry,9;Moe,12;Curly,4"
StringSplit, statList, statListList, `;
Loop, %statList0%
{
	StringSplit, stat, statList%a_index%, `,
	SayThePhrase(stat1, stat2)
}
Return

See also