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

AHK - Tray Recycler

This is a script that creates a recycle bin on your tray. It is a good example of how to monitor and manipulate the recycle bin on your computer, and contains some functions that you can use. Currently the only thing that doesn't work is the "Create Shortcut" option.
; Flags for RecyleEmpty()
SHERB_NOCONFIRMATION := 1
SHERB_NOPROGRESSUI := 2
SHERB_NOSOUND := 4

#Persistent
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
WinGetPos, trayX, trayY, trayWidth, trayHeight, ahk_class Shell_TrayWnd
sysget, IconX, 49
sysget, IconY, 50
Gui, +ToolWindow -Caption +AlwaysOnTop
Gui, Margin, 0, 0
Gui, Add, Pic, Icon32 x0 y0 vRBPic h25 w-1, Shell32.dll
Gui, Show, Hide
LastRBPic := 0

Menu, Tray, NoStandard
Menu, Tray, Add, Open, RunRB
Menu, Tray, Add, Explore, ExpRB
Menu, Tray, Add, Empty Recycle Bin, EmptyRB
Menu, Tray, Add
Menu, Tray, Add, Create Shortcut, ShctRB
Menu, Tray, Add
Menu, Tray, Add, Properties, PropRB
Menu, Tray, Add, Exit, Exit
Menu, Tray, Default, Open
SetTimer, UpdateIcon, 100

UpdateIcon:
RecycleQueryAll(Size, NumItems)
T := NumItems ? 1 : 0
If (T <> PrevItems) {
Menu, Tray, Icon, Shell32.dll, % NumItems ? 33 : 32
If NumItems
Menu, Tray, Enable, Empty Recycle Bin
Else
Menu, Tray, Disable, Empty Recycle Bin
PrevItems := NumItems ? 1 : 0
}
Menu, Tray, Tip, Files: %NumItems%`nSize: %Size% Bytes

; Set GUI Window Position
ImageSearch, imageX, imageY, trayX, trayY, trayX+trayWidth, trayY+trayHeight, % "*120 *Icon" . (NumItems ? 33 : 32) . " Shell32.dll"
If (!ErrorLevel) {
MouseGetPos, MouseX, MouseY
If (mouseX >= imageX && mouseX <= imageX+IconX
&& mouseY >= imageY-26 && mouseY <= imageY+IconY-26
&& GetKeyState("LButton","P")) {
If (LastRBPic <> T)
GuiControl,, ShellIcon, % "*icon" . (NumItems ? 33 : 32) . " *w0 *h0 Shell32.dll"
Gui, Show, % "NA X" imageX " Y" imageY-26
}
Else
Gui, Hide
}
Return

GuiDropFiles:
Loop, Parse, A_GuiEvent, `n
FileRecycle, %A_LoopField%
Gui, Hide
Return

EmptyRB:
RecycleEmpty()
Return

ExpRB:
PropRB:
RunRB:
Run % (A_ThisLabel = "PropRB" ? "Properties "
: A_ThisLabel = "ExpRB" ? "Explore "
: "") . "::{645ff040-5081-101b-9f08-00aa002f954e}"
Return

ShctRB:
Return

Exit:
ExitApp

RecycleEmpty(Flag=0) {
DllCall( "Shell32\SHEmptyRecycleBinA", UInt,0, UInt,0, UInt,Flag )
}
RecycleQuery(Drive, ByRef i64Size, ByRef i64NumItems) {
pszRootPath := Drive . (StrLen(Drive) = 1 ? ":\" : "") . "..."
VarSetCapacity(pSHQueryRBInfo, 20, 0)
NumPut(20, pSHQueryRBInfo, 0, "UInt")

Result := DllCall( "Shell32\SHQueryRecycleBinA", Str, pszRootPath, UInt, &pSHQueryRBInfo )
/*
If (ErrorLevel || (A_LastError <> 0 && A_LastError <> 18)) {
MsgBox ErrLvl: %ErrorLevel%`nLstErr: %A_LastError%`nResult: %Result%
Return
}
*/
; typedef struct _SHQUERYRBINFO{
cbSize := NumGet(pSHQueryRBInfo, 0, "UInt")
i64Size := NumGet(pSHQueryRBInfo, 4, "UInt64")
i64NumItems := NumGet(pSHQueryRBInfo, 12, "UInt64")
; }
Return Result
}
RecycleQueryAll(ByRef Size, ByRef NumItems) {
Size := (NumItems := 0)
DriveGet, Drives, List, Fixed
Loop, Parse, Drives
{
RecycleQuery(A_LoopField, TSize, TNumItems)
Size += TSize
NumItems += TNumItems
}
}

Saturday, December 27, 2008

AHK - Random Mouse Path (MouseMove)

This functions chooses random points along the path from the current mouse position to the new one, and moves through those points on the way to the new position. This function is supposed to help prevent people from detecting the use of a bot based on movement patterns. The first two parameters are the X and Y coordinates respectively, and the third (optional) parameter is the speed at which the mouse will move. For more information on this parameter see here. The default is 25.
MoveMouse(X, Y, Speed=25) {
T := A_MouseDelay
SetMouseDelay, -1
MouseGetPos, CX, CY
Pts := Round(Sqrt((X - CX)**2 + (Y - CY)**2) / 30,0)
Loop %Pts% {
Random, NX, % CX - ((CX - X) / Pts) * (A_Index - 1)
, % CX - ((CX - X) / Pts) * A_Index
Random, NY, % CY - ((CY - Y) / Pts) * (A_Index - 1)
, % CY - ((CY - Y) / Pts) * A_Index
MouseMove, % NX, % NY, % Speed
}
MouseMove, % X, % Y, % Speed
SetMouseDelay, % T
}


Forum Post

Tuesday, December 23, 2008

AHK - GenFile() - Generate files with exact size in bytes

This simple function will append exactly %Bytes% bytes to %Name%. This is useful for testing things that need to be able to move files, you can test for files of average size and get an accurate speed test.
GenFile(Name, Bytes) {
VarSetCapacity(FBytes, Bytes, 1)
FileAppend, % FBytes, % Name
}

GenFile("File.txt", 1024) ; Generates a 1kb file named "File.txt"

Sunday, December 14, 2008

AHK - AlwaysOnTop Window Toggle

This script is great for use with script editors. For example, you need to look up detailed information about some commands, so you just hold down RAlt, look it up, then can switch back and forth simply by holding down RAlt again.

WinTitle = ahk_class Notepad

WinSet, AlwaysOnTop, On, % WinTitle
RAlt::
A := WinExist("A")
WinSet, AlwaysOnTop, Off, % WinTitle
WinActivate, % WinTitle
WinActivate, ahk_id %A%
Hotkey, RAlt, Off
Return

RAlt Up::
WinSet, AlwaysOnTop, On, % WinTitle
Hotkey, RAlt, On
Return


Forum Post

Sunday, December 7, 2008

AHK - RegEx - Return last X lines of text/text file

RegEx For returning last X lines of a piece of text (text lines should be delimited by just LF, files by CRLF). Replace red text with # of lines. First one is for plain text, second is for text files.
RegExReplace("^.*?((`n[^`n]*){#})$","$1")
RegExReplace(data,"^(.|`r)*?((`r`n[^`r`n]*){#}})$","$2")


Forum Post

AHK - UrlDownloadToFile Interruption

This first script is an example of how to pause UrlDownloadToFile, or simply to execute other functions while still downloading. The second is an example of how to pause the download, then exit the script and delete the download file without finishing it. Differences in the second code are highlighted in red.

Size = 300 ; approximate size in bytes of final file

SetTimer, CheckStatus, 10
;Download File
URLDownloadToFile, http://www.autohotkey.com/download/AutoHotkey104706_Install.exe, _tmp_AutoHotkey104706_Install.exe
Return

;Check Status
CheckStatus:
FileGetSize, FSize, _tmp_AutoHotkey104706_Install.exe
If (FSize >= Size) {
Critical ; Make so this thread can't interrupted
SetTimer, CheckStatus, off ; Stop checking status
FileRead, FileData, _tmp_AutoHotkey104706_Install.exe ; Read file
MsgBox % "Done!`n" (ErrorLevel ? "Error: " ErrorLevel : "`n" FileData) ; Done!
; Demonstrates interruption of UrlDownloadToFile
; Size of file stays the same
Loop, 1000 {
FileGetSize, FSize, _tmp_AutoHotkey104706_Install.exe
ToolTip, File Size (Paused): %FSize%
Sleep, 10
}
; Resume Download
SetTimer, CheckStatus2, 10
}
Return

CheckStatus2:
FileGetSize, FSize, _tmp_AutoHotkey104706_Install.exe
ToolTip, File Size (Resumed): %FSize%
Return

~Esc::
SetTimer, ExitApp, 10
Return

ExitApp:
ExitApp

Size = 300 ; approximate size in bytes of final file
OnExit, OnExit ; Execute "OnExit" label when exiting

SetTimer, CheckStatus, 10
;Download File
URLDownloadToFile, http://www.autohotkey.com/download/AutoHotkey104706_Install.exe, _tmp_AutoHotkey104706_Install.exe
Return

;Check Status
CheckStatus:
FileGetSize, FSize, _tmp_AutoHotkey104706_Install.exe
If (FSize >= Size) {
Critical
SetTimer, CheckStatus, off
FileRead, FileData, _tmp_AutoHotkey104706_Install.exe
ToolTip, File Size: %FSize%
MsgBox % "Done!`n" (ErrorLevel ? "Error: " ErrorLevel : "`n" FileData)
FileDelete, _tmp_AutoHotkey104706_Install.exe
; Demonstrates interruption of UrlDownloadToFile
; Size of file stays the same
Loop, 250 {
FileGetSize, FSize, _tmp_AutoHotkey104706_Install.exe
ToolTip, File Size (Paused): %FSize%
Sleep, 10
}
; Exit
ExitApp

}
Return

~Esc::
SetTimer, OnExit, 10
Return

OnExit:
; Create a batch file that will delete the download file and itself
FileAppend, ping http://www.google.com`nDel "%A_ScriptDir%\_tmp_AutoHotkey104706_Install.exe"`n Del "del.bat", del.bat
; Wait for file to exist
Loop {
If (FileExist("del.bat"))
break
}
; Run file
Run, del.bat, %A_ScriptDir%, HIDE
ExitApp


Forum Post