Sunday, January 25, 2009

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
}
}

No comments: