Checkbox to automatically run at windows startup ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
eL Tux
Posts: 66
Joined: 06 Nov 2015, 07:52

Checkbox to automatically run at windows startup ?

15 Oct 2017, 20:02

Hi,

i want to add a checkbox in my GUI to automatically run my ahk script at windows startup

i have something like this :

Code: Select all

Gui, Add, CheckBox, gStartup vStartup, Script will run at windows startup
Gui, Show
Return

Startup:
Gui, Submit, NoHide 
if Startup = 1
MsgBox, run at windows startup ENABLE

if Startup = 0
MsgBox, run at windows startup DISABLE
Return
So.. i need the right script to enable/disable run at windows startup, (i can build a .exe) how to do this ?

Thanks
Sorry for my poor english
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Checkbox to automatically run at windows startup ?

16 Oct 2017, 00:02

When you check the box, would use FileCreateShortcut to stick a shortcut to the script in the Startup folder. This varies from Windows to Windows. On Windows 8.1, it is in %appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\

Then when you uncheck the box, I would delete that Shortcut with FileDelete.

This is code I have (I use #Include, pointing to this script, so that I can add a toggle feature to my scripts right-click menu to decide if I want this script to launch on startup or not.) You may be able to adapt it to your purposes. Focus on the StartUpToggle label; Basically where I have If ErrorLevel, that is where you would check the value of your checkbox being 0 or 1.

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 StartUp 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:
TAC109
Posts: 1112
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: Checkbox to automatically run at windows startup ?

16 Oct 2017, 17:47

Exaskryz wrote:When you check the box, would use FileCreateShortcut to stick a shortcut to the script in the Startup folder. This varies from Windows to Windows. On Windows 8.1, it is in %appdata%\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
Actually there is an AHK variable A_Startup that works it out for you.

I generally include the following code in programs I want to autoload. The first part should be placed in the autoexec (first) part of the script and the second part should be placed with your subroutines. You can run the script below to see how this works.

Code: Select all

#Persistent
Menu Tray, Add, Autoload program, autoload
IfExist %A_Startup%\%A_ScriptName%.lnk, Menu Tray, Check, Autoload program
return

autoload:                        ; <========== ; Toggle 'autoload'
IfExist %A_Startup%\%A_ScriptName%.lnk,FileDelete %A_Startup%\%A_ScriptName%.lnk
else FileCreateShortcut %A_ScriptFullPath%,       %A_Startup%\%A_ScriptName%.lnk
Menu Tray, ToggleCheck, Autoload program
return
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Checkbox to automatically run at windows startup ?

17 Oct 2017, 03:21

Isn't it possible to create/enable/disable an autorun option at the registry?
eL Tux
Posts: 66
Joined: 06 Nov 2015, 07:52

Re: Checkbox to automatically run at windows startup ?

28 Oct 2017, 14:09

Thanks this is ok for me :

Code: Select all

IfExist %A_Startup%\%A_ScriptName%.lnk
{
Startup = 1
StartCheck = Checked
}
else
{
Startup = 0
StartCheck = 
}

.....
Gui, Settings: Add, CheckBox, y430 x20 w180 h25 gStartup vStartup %StartCheck%, Start with windows
.....
return

Startup:
Gui, Settings: Submit, NoHide 
if (Startup = 0){
FileDelete %A_Startup%\%A_ScriptName%.lnk
Startup = 1
}
else
{
FileCreateShortcut %A_ScriptFullPath%,%A_Startup%\%A_ScriptName%.lnk
Startup = 0
}
Return


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Rohwedder and 59 guests