;IdleClose.ahk
; Closes programs that's not been activated for x minutes
;Skrommel @2006
idletime=0001 ;HHMM
letlive=Explorer.exe,Progman.exe ;<programname>,<programname>
kill=0 ;0=close 1=kill
confirm=1 ;0=ask before closing 1=don't ask
#SingleInstance,Force
#NoEnv
SetWinDelay,0
applicationname=IdleClose
ids=
idletime=%idletime%00
WinGet,list,List
Loop,% list
{
id:=list%A_Index%
ids=%ids%%id%,
%id%:=A_Now
}
SetTimer,KILL,10000
SetTimer,CLEAN,60000
activeid:=WinExist("A")
Loop
{
WinWaitNotActive,ahk_id %activeid%
activeid:=WinExist("A")
%activeid%:=A_Now
IfNotInString,ids,%activeid%`,
ids=%ids%%activeid%,
}
KILL:
%activeid%:=A_Now
WinGet,list,List
Loop,% list
{
id:=list%A_Index%
time:=%id%
If (time+idletime<A_Now)
{
WinGet,pid,PID,ahk_id %id%
path:=GetModuleFileNameEx(pid)
SplitPath,path,name
If name In %letlive%
Continue
If confirm=1
{
MsgBox,4,%applicationname%,Really close %name%?
IfMsgBox,No
{
letlive=%letlive%,%name%
Continue
}
}
If kill=1
Process,Close,%pid%
Else
WinClose,ahk_id %id%
}
}
Return
CLEAN:
newids=
Loop,Parse,ids,`,
{
If A_LoopField=
Continue
IfWinExist,ahk_id %A_LoopField%
newids=%newids%%A_LoopField%,
Else
%A_LoopField%=
}
ids=%newids%
Return
GetModuleFileNameEx( p_pid ) ;by shimanov at
http://www.autohotke...?t=4182&start=15{
if A_OSVersion in WIN_95,WIN_98,WIN_ME
{
MsgBox, This Windows version (%A_OSVersion%) is not supported.
return
}
/*
#define PROCESS_VM_READ (0x0010)
#define PROCESS_QUERY_INFORMATION (0x0400)
*/
h_process := DllCall( "OpenProcess", "uint", 0x10|0x400, "int", false, "uint", p_pid )
if ( ErrorLevel or h_process = 0 )
{
MsgBox, [OpenProcess] failed
return
}
name_size = 255
VarSetCapacity( name, name_size )
result := DllCall( "psapi.dll\GetModuleFileNameExA", "uint", h_process, "uint", 0, "str", name, "uint", name_size )
if ( ErrorLevel or result = 0 )
MsgBox, [GetModuleFileNameExA] failed
DllCall( "CloseHandle", h_process )
return, name
}