add automatic option to run spesific scripts on windows startup

Propose new features and changes
hunter_finn
Posts: 4
Joined: 04 May 2017, 23:03

add automatic option to run spesific scripts on windows startup

12 May 2017, 12:58

first i have to say that.
yes i know that i just need to add those scripts, that i want to run automatically on windows startup folder.
however it would be so much more convenient to just rightclick and select "run on windows startup.
stealzy
Posts: 91
Joined: 01 Nov 2015, 13:43

Re: add automatic option to run spesific scripts on windows startup

14 May 2017, 07:52

Add such item in script tray menu. Much better.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: add automatic option to run spesific scripts on windows startup

14 May 2017, 19:58

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.
Last edited by Exaskryz on 15 May 2017, 09:47, edited 1 time in total.
TAC109
Posts: 1111
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: add automatic option to run spesific scripts on windows startup

14 May 2017, 21:15

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
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: add automatic option to run spesific scripts on windows startup

08 Sep 2017, 10:48

What, are you guys crazy? This is so easy.

I run Windows 7 and have a ton of problems with Scheduled Tasks that wake the system to run. BUT, I have absolutely NO PROBLEM with creating a Scheduled Task that kicks offs at LOGON OF ANY USER. Just create a LOGON.ahk and create a scheduled task that runs it at logon. What could be simpler?

Note: You don't want, IMHO, to create a started task that runs an AHK script that runs continually. Doing that, the Started Task itself never ends.

For me, LOGON then runs MASTER and MASTER then runs whatever I want to be running constantly. I break down my AHK routines to a few categories. One just handles all my VERY MANY hotkeys. Another just handles everything I want done on a TIME basis, another just sits and captures power events , etc. (I found having one script to do everything was just too ridiculous. too large, too difficult to debug and I believe very problematic for AHK to handle so much in one script.)

If you can't create a started task, you have my sympathy. I control my computer. It does not control me. I suspect this is the case in many corporations. Dunno... It's been 15 years since I worked as a programmer.


Joe Petree
some guy

Re: add automatic option to run spesific scripts on windows startup

13 Apr 2018, 19:17

If anyone is looking for the command line for this:

schtasks /create tn <YourTaskName> /sc onlogon /rl highest /ru Users /tr <PathToexe>

This will run your exe with elevated privileges on every user when they logon

Kudos :superhappy:

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 24 guests