If you want one Admin Script to start and stop all of your ahk scripts at certain condition this is for you.
How it works:
Simply add the full path of your script in ArrayOfScripts then add your starting and ending condition in Script Tasks section using RunScript() and CloseScript() following the relevant index of the ArrayOfScripts like RunScript(ArrayOfScripts,2) and done.
Also this script only terminates the scripts that was opend by itself.
So if You open a script even if the closing condition apply it wont get closed unless it get forced by for example CloseScript(ArrayOfScripts,2,"Force").
You can make a shortcut of the script and put it in startup folder C:\Users\%UserName%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup in order to start it by windows.
Credits to @SKAN for his Silent Script Elevator function.(UAC prompts only in first time)
Feel free to give any suggestion that improves the code.
Have fun automating!
Code: Select all
/*
*Admin Script Manager
*Version 4
*3/19/2024
*By @Skyeee
*https://www.autohotkey.com/boards/viewtopic.php?f=83&t=127527
*/
#Requires AutoHotkey v2.0
#SingleInstance Force
DetectHiddenWindows True
RunAsTask(True) ;Silent Script Elevator
;--------------------------------------------------------------------------------------------------;
;----------------------------------------Available Scripts-----------------------------------------;
;--------------------------------------------------------------------------------------------------;
ArrayOfScripts:=
[
["C:\Documents\ahk\Admin\NoiseMaker v6.ahk" ], ;1
["C:\Documents\ahk\Admin\Batch Downloader v5.ahk" ], ;2
["C:\Documents\ahk\Admin\Web Manager v3.ahk" ], ;3
]
;--------------------------------------------------------------------------------------------------;
i:=1
While(i<=ArrayOfScripts.Length){
ArrayOfScripts[i].Push("0")
i:=i+1
}
Loop
{
;--------------------------------------------------------------------------------------------------;
;----------------------------------------Script Tasks----------------------------------------------;
;--------------------------------------------------------------------------------------------------;
if WinExist
RunScript(ArrayOfScripts,1)
else
CloseScript(ArrayOfScripts,1)
;--------------------------------------------------------------------------------------------------;
if WinExist("Downloader")
RunScript(ArrayOfScripts,2)
else
CloseScript(ArrayOfScripts,2)
;--------------------------------------------------------------------------------------------------;
if(WinExist("ahk_exe firefox.exe") || WinExist("ahk_class MozillaWindowClass"))
RunScript(ArrayOfScripts,3)
else
CloseScript(ArrayOfScripts,3)
;--------------------------------------------------------------------------------------------------;
WinWaitNotActive WinExist("A") ;waits until windows focus is changed
}
;--------------------------------------------------------------------------------------------------;
;--------------------------------------------Functions---------------------------------------------;
;--------------------------------------------------------------------------------------------------;
RunScript(ArrayOfVariables,Number){
tempScriptFullPath:=ArrayOfVariables[Number][1]
tempScriptName:=GetNameFromFullPath(tempScriptFullPath)
tempWindowsTitle:=Format("{1} - AutoHotkey",tempScriptName)
tempID:=WinExist(tempWindowsTitle)
if (tempID == 0)
{
if FileExist(tempScriptFullPath)
{
Run tempScriptFullPath
WinWait(tempWindowsTitle, ,5)
ArrayOfVariables[Number][2]:=WinExist(tempWindowsTitle)
}
else
MsgBox tempScriptName " file does not exist."
}
/*
else if(tempID == 0 && ArrayOfVariables[Number][2] != "0")
{
ArrayOfVariables[Number][2]:="0"
}
*/
}
;--------------------------------------------------------------------------------------------------;
CloseScript(ArrayOfVariables,Number,Force:=0){
if(ArrayOfVariables[Number][2] != "0")
{
tempFullId:=Format("ahk_id {1}",ArrayOfVariables[Number][2])
if WinExist(tempFullId)
WinClose tempFullId
ArrayOfVariables[Number][2]:="0"
}
else if(Force != 0)
{
tempFullId:=Format("ahk_id {1}",ArrayOfVariables[Number][2])
if WinExist(tempFullId)
WinClose tempFullId
}
}
;--------------------------------------------------------------------------------------------------;
GetNameFromFullPath(FullPath){
FoundPos := InStr(FullPath, "\", , -1)
FoundPos := FoundPos + 1
NewStr := SubStr(FullPath, FoundPos)
return NewStr
}
;--------------------------------------------------------------------------------------------------;
RunAsTask(SingleInstance := 0, Shortcut := "") ; RunAsTask() v0.23 - Auto-elevates script without UAC prompt
{ ; By SKAN for ah2 on D67M/D683 @ autohotkey.com/r?t=119710
Global A_Args
Local TaskSchd, TaskRoot, TaskName, RunAsTask, TaskExists
, CmdLine, PathCrc, Args, XML, AhkPath, Description
, STMM := A_TitleMatchMode
, DHW := A_DetectHiddenWindows
, SWD := A_WinDelay
, QUO := '"'
A_TitleMatchMode := 1
A_DetectHiddenWindows := 1
A_WinDelay := 0
Try TaskSchd := ComObject("Schedule.Service")
, TaskSchd.Connect()
, TaskRoot := TaskSchd.GetFolder("\")
Catch
Return
Loop Files A_AhkPath
AhkPath := A_LoopFileFullPath
CmdLine := A_IsCompiled ? QUO A_ScriptFullpath QUO : QUO AhkPath QUO A_Space QUO A_ScriptFullpath QUO
PathCrc := DllCall("ntdll\RtlComputeCrc32", "int",0, "wstr",CmdLine, "uint",StrLen(CmdLine)*2, "uint")
TaskName := Format("RunAsTask\{1:}_{2:}@{3:08X}", A_ScriptName, A_PtrSize=8 ? "64" : "32", PathCrc)
Try RunAsTask := TaskRoot.GetTask(TaskName)
, TaskExists := 1
Catch
TaskExists := 0
If ( A_IsAdmin = False )
{
If ( A_Args.Length > 0 )
Args := Format(StrReplace(Format("{:" A_Args.Length "}",""), "`s", "`n{}"), A_Args*) ; Join()
, WinSetTitle(TaskName Args , A_ScriptHwnd)
If ( TaskExists = True )
Try RunAsTask.Run(0)
Catch
MsgBox("Task launch failed (disabled?):`n" TaskName, "RunAsTask", " 0x40000 Iconx")
, ExitApp()
If ( TaskExists = False )
Try Run("*RunAs " CmdLine, A_ScriptDir)
Catch
MsgBox("Task not created..`nChoose 'Yes' in UAC", "RunAsTask", " 0x40000 Iconx T4")
, ExitApp()
If ( A_Args.Length > 0 )
WinWait("Exit_" TaskName " ahk_class AutoHotkey",, 5)
ExitApp()
}
If ( A_IsAdmin = True )
{
If ( TaskExists = False )
XML := Format('
( LTrim
<?xml version="1.0" ?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Principals>
<Principal>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
</Settings>
<Actions>
<Exec>
<Command>{1:}</Command>
<Arguments>{2:}</Arguments>
<WorkingDirectory>{3:}</WorkingDirectory>
</Exec>
</Actions>
</Task>
)'
, A_IsCompiled = 1 ? QUO A_ScriptFullpath QUO : QUO AhkPath QUO
, A_IsCompiled = 0 ? QUO A_ScriptFullpath QUO : ""
, A_ScriptDir
)
, TaskRoot.RegisterTask( TaskName
, XML
, 0x2 ; TASK_CREATE
, ""
, ""
, 3 ; TASK_LOGON_INTERACTIVE_TOKEN
)
If ( StrLen(Shortcut) )
Try FileGetShortcut(Shortcut,,,, &Description)
Catch
Description := ""
Finally
If ( Description != Taskname )
FileCreateShortcut("schtasks.exe", Shortcut, A_WorkingDir
, "/run /tn " QUO TaskName QUO, TaskName,,,,7)
If ( SingleInstance )
DllCall( "User32\ChangeWindowMessageFilterEx"
, "ptr", A_ScriptHwnd
, "uint", 0x44 ; WM_COMMNOTIFY
, "uint", 1 ; MSGFLT_ALLOW
, "ptr", 0
)
If ( WinExist(TaskName " ahk_class AutoHotkey") )
Args := WinGetTitle()
, WinSetTitle("Exit_" TaskName)
, Args := SubStr(Args, InStr(Args, "`n") + 1)
, A_Args := StrSplit(Args, "`n")
}
A_WinDelay := SWD
A_DetectHiddenWindows := DHW
A_TitleMatchMode := STMM
Return TaskName
}
;--------------------------------------------------------------------------------------------------;