Skip to content

Instantly share code, notes, and snippets.

@manciuszz
Last active April 4, 2024 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manciuszz/6220269d700b68e86d7b061d06751e8b to your computer and use it in GitHub Desktop.
Save manciuszz/6220269d700b68e86d7b061d06751e8b to your computer and use it in GitHub Desktop.
Fast 'shell:startup' and Task Scheduler Auto Application Startup replacement using AutoHotkey

Auto Executor

The compiled 'AutoExecutor.exe' file SHORTCUT (.lnk) is meant to be placed inside 'shell:startup' directory.
This would launch AutoExecutor.exe upon windows startup, which in turn would start launching applications inside 'Apps' folder.

How it works:

The script searches for .exe, .lnk and .ahk files inside 'Apps' folder. Once found, it checks if the filename doesn't contain specific rules. The currently available rules are:

  • #milliseconds_FILENAME.exe - which would delay the launch of this specific FILENAME.exe by an X amount of time in milliseconds.
    • For example: "#3000_ThrottleStop.exe" would launch 'ThrottleStop.exe' after 3 seconds.
  • !FILENAME.exe - try hiding the application window of FILENAME.exe

Note, that the 'Apps' folder has to be relative to the 'AutoExecutor.exe' script file.

Features:

  • Launch applications in administrator privileges
  • Auto Start applications inside 'Apps' folder
  • Customize application launch time
    • To use this feature, add a prefix to file names inside 'Apps' folder.
#NoEnv
#SingleInstance Force
#NoTrayIcon
SetBatchLines, -1
SetKeyDelay, -1, 1
SetMouseDelay, -1
SetWinDelay, -1
SetControlDelay, -1
class Utility {
RequireAdmin() {
FULL_COMMAND_LINE := DllCall("GetCommandLine", "str")
if not (A_IsAdmin or RegExMatch(FULL_COMMAND_LINE, " /restart(?!\S)")) {
try {
FilePath := ""
if A_IsCompiled
FilePath := A_ScriptFullPath
else
FilePath := Format("{} /restart \\\""{}\\\""", A_AhkPath, A_ScriptFullPath)
psScript =
(
param($param1)
Start-Process powershell -WindowStyle Hidden -Verb RunAs -ArgumentList \"-Command Start-Process '$param1'\"
)
psScript := Format("powershell -Command &{{1}} '{2}'", psScript, FilePath)
RunWait, % psScript,, Hide
}
ExitApp
}
}
SetTimer(fn, period := -1) {
SetTimer % fn, % period
}
}
class AutoExecutor {
static FolderName := "Apps"
static ExtensionPattern := "exe|lnk|ahk|bat|cmd"
static _ := AutoExecutor := new AutoExecutor()
__New() {
Utility.RequireAdmin()
this.Initialize()
}
Config() {
this.ExitManager := new this.ExitManager()
this.Pattern := "i).*\.(" . this.ExtensionPattern . ")$"
this.SearchPath := A_ScriptDir . "\" . this.FolderName . "\*"
}
Initialize() {
this.Config()
this.Search(this.SearchPath)
this.Finalize()
}
Search(SearchPath) {
Loop, Files, % SearchPath
{
FileFullPath := A_LoopFileFullPath
if !RegExMatch(FileFullPath, this.Pattern)
continue
FileName := A_LoopFileName
SleepMatch := RegExMatch(FileName, "#(\d+)_", SleepMatch)
HideFlagMatch := InStr(FileName, "!") ? 0 : 1
SleepTime := SleepMatch ? SleepMatch1 : ""
if (SleepTime > 0) {
this.LaunchAppDelayed(FileFullPath, SleepTime, HideFlagMatch)
} else {
this.LaunchApp(FileFullPath, HideFlagMatch)
}
}
}
LaunchAppDelayed(FileFullPath, Delay := -1, nShowCmd := 1) {
this.ExitManager.QueueExecution()
Utility.SetTimer(ObjBindMethod(this, "_ExitManagedLaunchApp", FileFullPath, nShowCmd), -1 * Delay)
}
_ExitManagedLaunchApp(FileFullPath, nShowCmd) {
this.LaunchApp(FileFullPath, nShowCmd)
this.ExitManager.FinishExecution()
}
LaunchApp(FileFullPath, nShowCmd := 1) {
DllCall("Shell32\ShellExecute", "UInt", 0, "Str", "open", "Str", FileFullPath, "Str", "", "Str", "", "Int", nShowCmd)
}
Finalize() {
this.ExitManager.AttemptExit()
}
class ExitManager {
__New() {
this.TOTAL_EXECUTIONS := 0
this.ExitFn := ObjBindMethod(this, "AttemptExit")
}
QueueExecution() {
this.TOTAL_EXECUTIONS++
}
FinishExecution() {
this.TOTAL_EXECUTIONS--
}
AttemptExit() {
if (this.TOTAL_EXECUTIONS > 0) {
Utility.SetTimer(this.ExitFn, -1000)
return
}
this.Exit()
}
Exit() {
ExitApp
}
}
}
#If (WinActive("ahk_exe notepad++.exe") || WinActive("ahk_exe code.exe"))
^R::Reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment