I'm not sure a whole lot of people use it, but at least on a personal level, here's a solution.
Code: Select all
Menu, Tray, UseErrorLevel
If FileExist(startUpDir:=("C:\Users\" A_UserName "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" A_ScriptName " - Shortcut.lnk"))
Menu, Tray, Add, Remove from StartUp, StartUpToggle
else
Menu, Tray, Add, Add to StartUp, StartUpToggle
GoSub, SkipLabel_StartUp
return
StartUpToggle:
; I could really use a better way to know the name of the menu item that was selected
; Using now errorlevel to determine if the menu item name exists
Menu, Tray, Rename, Remove from StartUp, Add to StartUp
If ErrorLevel ; Remove from StartUp doesn't exist. So Add to StatUp does. So we're adding this script to startup
{
FileCreateShortcut, %A_ScriptFullPath%, %startUpDir%
Menu, Tray, Rename, Add to StartUp, Remove from StartUp
}
else ; we successfully renamed the Remove from StartUp, which means that was selected, so we need to remove the script from startup
FileDelete, %startUpDir%
return
SkipLabel_StartUp:
Use this code in any script after saving the above to a separate ahk file, of course changing the name to whatever you chose, and possibly using an Absolute path if your scripts are in different folders.
Code: Select all
#Include NameOfTheAboveScript.ahk ; EDIT: HardCode this directory, perhaps. It did not work with just the name of the script, if the shortcut is launched, as it appears the A_ScriptDir defaults to the StartUp folder. You could alternatively include NameOfTheAboveScript.ahk in your StartUp folder.
return ; unless you have more code you want to run in your auto-execute section after the #Include, like other #Include files
If this kind of feature might be added in the future to default AHK installs -- or possibly instead adding to the context menu when right clicking the script in Windows Explorer -- a couple issues would have to be worked out by someone more familiar with menu manipulation than me. I had to think of the workaround of using an ErrorLevel to figure out if the user wanted to Remove or Add the script to their StartUp folder. Additionally, the location of the StartUp folder may change depending on the OS version, so that can be problematic and may need several A_
OSVersion checks which runs the risk of creating a little bit of bulk.