It is common to want to add your AutoHotkey script to the startup, so that it automatically is started when you start your PC.
I share here a standard approach to provide this to the user.
And the menu callback looks like:
I share here a standard approach to provide this to the user.
ToStartup Function
You can download the file from GitHub https://github.com/tdalon/ahk/blob/master/Lib/ToStartup.ahk
; ToStartup(sFile,True) : add sFile to Startup
; ToStartup(sFile,False) : remove sFile from Startup
; ToStartup(sFile) returns True if File Shortcut exists in startup and False else
ToStartup(sFile,Toggle := ""){
sLnk := RegExReplace(sFile,"\..*",".lnk")
sLnk := RegExReplace(sLnk,".*\\",A_Startup . "\")
If (Toggle="")
return FileExist(sLnk)
Else If (Toggle = 1) {
FileIcon := StrReplace(sFile,".ahk",".ico")
If FileExist(FileIcon)
FileCreateShortcut, %sFile%, %sLnk% ,,,,%FileIcon% ; will overwrite existing shortcut
Else
FileCreateShortcut, %sFile%, %sLnk% ; will overwrite existing shortcut
} Else If (Toggle=0) {
If FileExist(sLnk) {
FileDelete, %sLnk%
TrayTipAutoHide("Startup setting", "File ''" . RegExReplace(sLnk,".*\\","") . "'' was removed from Startup!")
}
}
Run %A_Startup%
}
Add to Icon Tray Menu
These are the couple of lines to add Icon Tray Menu Settings->Launch on Startup
Menu,SubMenuSettings,Add, Launch on Startup, MenuCb_ToggleSettingLaunchOnStartup
SettingLaunchOnStartup := ToStartup(A_ScriptFullPath)
If (SettingLaunchOnStartup)
Menu,SubMenuSettings,Check, Launch on Startup
Else
Menu,SubMenuSettings,UnCheck, Launch on Startup
Menu, Tray, Add, Settings, :SubMenuSettings
And the menu callback looks like:
MenuCb_ToggleSettingLaunchOnStartup:
If (SettingLaunchOnStartup := !SettingLaunchOnStartup) {
Menu,SubMenuSettings,Check, Launch on Startup
ToStartup(A_ScriptFullPath,True)
}
Else {
Menu,SubMenuSettings,UnCheck, Launch on Startup
ToStartup(A_ScriptFullPath,False)
}
return
No comments:
Post a Comment