; smart-icons-for-mounted-media.ahk: An AutoHotkey script to add and remove desktop and quick-launch icons for discs and removable drives as they're mounted and unmounted.
; Copyright 2008, Marc Stewart
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see .
; Version: 1.01
; Changes: Drives are checked on startup, and icons are removed on exit.
; ==== START OF USER-SET PARAMETERS =============
; Add or remove drive letters to monitor.
MonitorDrives = ABDEFGHIJKLMNOPQRSTUVWXYZ
; Should icons be added to Desktop?
; 1 = Yes
; 0 = No
AddSmartIconsToDesktop = 1
; Should icons be added to QuickLaunch panel?
; 1 = Yes
; 0 = No
AddSmartIconsToQuickLaunch = 0
; ==== END OF USER-SET PARAMETERS ===============
DriveLabelA := A
DriveLabelB := B
DriveLabelC := C
DriveLabelD := D
DriveLabelE := E
DriveLabelF := F
DriveLabelG := G
DriveLabelH := H
DriveLabelI := I
DriveLabelJ := J
DriveLabelK := K
DriveLabelL := L
DriveLabelM := M
DriveLabelN := N
DriveLabelO := O
DriveLabelP := P
DriveLabelQ := Q
DriveLabelR := R
DriveLabelS := S
DriveLabelT := T
DriveLabelU := U
DriveLabelV := V
DriveLabelW := W
DriveLabelX := X
DriveLabelY := Y
DriveLabelZ := Z
WM_DEVICECHANGE()
OnMessage(0x219, "WM_DEVICECHANGE")
OnExit, ExitSub
WM_DEVICECHANGE()
{
global ; Drive labels must persist across function calls
local DriveLetter ; but these don't need to.
local DriveStatus
local DriveType
local DriveLabel
Sleep 500 ; Gives new drives a chance to Ready themselves
Loop, parse, MonitorDrives ; Check each drive listed above
{
DriveLetter = %A_LoopField%
DriveGet, DriveStatus, Status, %DriveLetter%:
If DriveStatus = Ready
{
DriveGet, DriveType, Type, %DriveLetter%:
If DriveType = Network
{
DriveLabel%DriveLetter% := DriveLetter . " (Network)"
}
Else
{
DriveGet, DriveLabel%DriveLetter%, Label, %DriveLetter%:
If !DriveLabel%DriveLetter%
{
If DriveType = CDROM
{
DriveLabel%DriveLetter% := DriveLetter . " (Unlabeled Disc)"
}
Else If DriveType = Removable
{
DriveLabel%DriveLetter% := DriveLetter . " (Removable Drive)"
}
Else If DriveType = RAMDisk
{
DriveLabel%DriveLetter% := DriveLetter . " (RAM Disk)"
}
Else If DriveType = Fixed
{
DriveLabel%DriveLetter% := DriveLetter . " (Fixed Drive)"
}
Else
{
DriveLabel%DriveLetter% := DriveLetter . " (Unlabeled Drive)"
}
}
}
; CREATE THE SHORTCUT
DriveLabel = % DriveLabel%DriveLetter%
If AddSmartIconsToDesktop
{
FileCreateShortcut, %DriveLetter%:\, %A_Desktop%\%DriveLabel%.lnk
}
If AddSmartIconsToQuickLaunch
{
FileCreateShortcut, %DriveLetter%:\, %A_AppData%\Microsoft\Internet Explorer\Quick Launch\%DriveLabel%.lnk
}
}
Else
{
; DELETE THE SHORTCUT
; To prevent problems deleting a shortcut to a non-existent drive,
; rewrite the shortcut to a known location: the user's desktop.
DriveLabel = % DriveLabel%DriveLetter%
If AddSmartIconsToDesktop
{
FileCreateShortcut, %A_Desktop%, %A_Desktop%\%DriveLabel%.lnk
FileDelete, %A_Desktop%\%DriveLabel%.lnk
}
If AddSmartIconsToQuickLaunch
{
FileCreateShortcut, %A_Desktop%, %A_AppData%\Microsoft\Internet Explorer\Quick Launch\%DriveLabel%.lnk
FileDelete, %A_AppData%\Microsoft\Internet Explorer\Quick Launch\%DriveLabel%.lnk
}
}
}
}
DeleteAllSmartIcons()
{
global ; Drive labels must persist across function calls
local DriveLetter ; but these don't need to.
local DriveLabel
Loop, parse, MonitorDrives
{
DriveLetter = %A_LoopField%
DriveLabel = % DriveLabel%DriveLetter%
If AddSmartIconsToDesktop
{
FileCreateShortcut, %A_Desktop%, %A_Desktop%\%DriveLabel%.lnk
FileDelete, %A_Desktop%\%DriveLabel%.lnk
}
If AddSmartIconsToQuickLaunch
{
FileCreateShortcut, %A_Desktop%, %A_AppData%\Microsoft\Internet Explorer\Quick Launch\%DriveLabel%.lnk
FileDelete, %A_AppData%\Microsoft\Internet Explorer\Quick Launch\%DriveLabel%.lnk
}
}
}
return
ExitSub:
DeleteAllSmartIcons()
ExitApp