Sunday, January 25, 2009

AHK - Sender() and Reciever()

These two functions can be used to send short messages between AHK scripts without the use of temporary files. Here are the two functions:
Sender(Message) {
DetectHiddenWindows, On
WinGet, List, List, ahk_class AutoHotkey
Message := Message
Loop, %List%
SendMessage, % 1+4096,,,, % "ahk_id " List%A_Index%
Loop, Parse, Message
Loop, %List%
SendMessage, % asc(A_LoopField)+4099,,,, % "ahk_id " List%A_Index%
Loop, %List%
SendMessage, % 2+4096,,,, % "ahk_id " List%A_Index%
}


Loop, 258
OnMessage(A_Index+4096,"Reciever")
Return

Reciever(wParam, lParam, msg, hwnd)
{
Global Message
If (msg = 4097)
Message=
Else If (msg = 4098)
SetTimer, Handler, -1
Else
Message .= chr(msg-4099)
}

Handler:
MsgBox % Message
Return

And here is an example script. Run Recv.ahk before BCast.ahk.
; BCast.ahk
SetBatchLines, -1
Sender("Hello World!")
Sender(Message) {
DetectHiddenWindows, On
WinGet, List, List, ahk_class AutoHotkey
Message := Message
Loop, %List%
SendMessage, % 1+4096,,,, % "ahk_id " List%A_Index%
Loop, Parse, Message
Loop, %List%
SendMessage, % asc(A_LoopField)+4099,,,, % "ahk_id " List%A_Index%
Loop, %List%
SendMessage, % 2+4096,,,, % "ahk_id " List%A_Index%
}


; Recv.ahk
#Persistent
SetBatchLines, -1
Loop, 258
OnMessage(A_Index+4096,"Reciever")
Return

Reciever(wParam, lParam, msg, hwnd)
{
Global Message
If (msg = 4097)
Message=
Else If (msg = 4098)
SetTimer, Handler, -1
Else
Message .= chr(msg-4099)
}

Handler:
MsgBox % Message
Return

No comments: