46

Each time I restart my computer, I have to rebuild all my AHK Scripts so the keyboard shortcuts will work.

For instance, I have a script that assigns Ctrl+j to set up an instance of the MEAN stack and open my web site. After restarting my computer, pressing ^j does nothing until I go into Sublime and ^b (Build). I could build from AHK, but I do a lot of work in Sublime, so it's easier to build there.

How can my AHK Scripts automatically build on startup?


What I've tried so far:
  • Looking to build a post startup, startup script: This Is concerned with using an AHK script to start many programs on starup. I use Windows' Task Manager > Startup to do this, thus has nothing to do with making. I want to not have to rebuild all my scripts after every restart.

  • Windows 10 - run script on windows startup problem: This assumes the start script is already written and implemented. However, it doesn't appear to give the script.

  • AutoHotKey FAQ: I eventually found a solution here, but it took a long time to dig through the questions so I figured I'd post a question anyway to help others who run into this.

Community
  • 1
  • 1
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
  • Not sure you want to "build" the script and not simply launch/start it. You can launch it at startup by adding a shortcut to it in your startup folder (req. you have set ahk.exe as default to open .ahk files) – Thierry Dalon Feb 16 '21 at 09:49
  • https://stackoverflow.com/questions/52909114/how-to-launch-ahk-file-when-pc-starts/70814043#70814043 someone already asked that question… – Anonymous Jan 22 '22 at 15:03

8 Answers8

60

Simply add a shortcut of your script in the Windows 'Startup' folder.

Three ways to get there:

1- In Windows Explorer, go to %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup (for current user startup list) or %ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Startup (for every user startup list)

or

2- Open Windows' Run application (Windows + r) write shell:startup (current user) or shell:common startup (every user) in the edit field and click on the 'ok' button.

or

3- Start > Programs > Startup (old Windows versions)


In AutoHotKey you can access this folder with the Built-in Variables %A_Startup% (current user) or %A_StartupCommon% (every user)

To create the (current user) shortcut automatically from your script, use the following line:

FileCreateShortcut, %A_ScriptFullPath%, %A_Startup%\shortcutname.lnk

To do the same for all users, use this line instead:

FileCreateShortcut, %A_ScriptFullPath%, %A_StartupCommon%\shortcutname.lnk

(in the case of having a file with the same name in the folder, the file would be overwritten)


ps: Win10 blocks scripts in startup with AHK running as admin... read the comments of this post for extra info.

L777
  • 7,719
  • 3
  • 38
  • 63
  • Any idea why what could go wrong when the ahk shortcut dont works there (after a windows fresh install)? (They start as expected when I click on it from this folder) – JinSnow Jun 01 '19 at 04:05
  • 1
    maybe its set to 'run as admin' as mentioned in @JerryGoyal's answer (below)? Besides it I have no clue. – L777 Jun 02 '19 at 14:53
  • 3
    thanks! That was it! Disablbing the "run as admin" of the autohotkey.exe, and adding this to the top of my script solved it: `; run script as admin (reload if not as admin) if not A_IsAdmin { Run *RunAs "%A_ScriptFullPath%" ExitApp }` – JinSnow Jun 03 '19 at 07:43
  • 1
    I think `uiAccess` can help with run as admin issue. However, it usually requires reinstalling AHK and check "install UI Access" (because it is not default). What's more, since it uses some self-signed certificate and inserts it to system, and it can bypass UAC, so it can cause security breaches. – InQβ Nov 02 '19 at 14:57
  • in the new AutoHotKey Dash just open "Launch settings" and check "UI Access" checkboxes. – xmedeko Jul 25 '23 at 06:25
10

Run at startup in Windows 10:

  1. Compile the script to *.exe
  2. Put the shortcut of that exe in startup folder "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup"
  3. That's all. Do NOT set it to run as admin. Programs in Win10 don't run at startup which are marked as run as admin.

Run Script as admin at Startup in Windows 10:

  1. Go to ahk script and mark it run as admin.
  2. Create the VBScript using a text editor
'put it in startup folder to run the mentioned ahk script at startup
Set WshShell = CreateObject("WScript.Shell" )    
WshShell.Run """C:\Users\jerry\Downloads\scripts\some_script.ahk""", 0 'Must quote command if it has spaces; must escape quotes   
Set WshShell = Nothing

Replace C:\Users\jerry\Downloads\scripts\some_script.ahk with the path to your script with extension and save it as .vbs.

  1. Place this .vbs script at startup folder %appdata%\Microsoft\Windows\Start Menu\Programs\Startup

PS: My fav AHK scripts: https://gourav.io/blog/autohotkey-scripts-windows

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • For those having trouble compiling the script to .exe : https://www.thewindowsclub.com/how-to-convert-an-ahk-file-into-an-exe-file-in-windows-10 – arara Dec 26 '22 at 01:29
3
  • Open note pad or any other text editor

  • write Start "" "C:\ahk\yourdirectory\yourahk.ahk"

  • press save as

  • navigate to %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup

  • name the filename anything.bat

Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32
1

As a Windows 7 user, I have limited experience with Windows 10. I have heard that W10 can be finicky with regard to running programs underground administratie or limited user. In W7, you just add the .AHK file link to the startup dir.

Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
1

A simple solution without writing any code, using the Windows Task Scheduler, set the script to start when the user logs on. If it is set to system startup it will error because it is too early.

weiya ou
  • 2,730
  • 1
  • 16
  • 24
0

To allow the script to automate administrative programs without running as admin, here're the required steps:

  1. When installing AHK, check the "Add 'Run with UI Access' to context menus" option.
  2. After installation, find AutoHotkeyU64_UIA.exe on your disk.
  3. Open the .ahk script's properties, change the "Open with" option to the AutoHotkeyU64_UIA.exe you just found.
  4. Create a shortcut to this script.
  5. Open %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup in the file explorer.
  6. Move that shortcut to this folder.

And you're done.


How does it work?

The "Add 'Run with UI Access' to context menus" option provides a workaround for common UAC-related issues by allowing the script to automate administrative programs without running as admin.

Reference: Run with UI Access.

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
0
  1. Open windows run (win+r) and type shell:startup
  2. Copy paste the ahk file into there
  3. Close the folder

And that‘s all!

7urkm3n
  • 6,054
  • 4
  • 29
  • 46
Anonymous
  • 168
  • 12
0

Another way to do this is via the registry editor.

1: Open the windows registry editor; Press "Windows Key + R" and type "regedit" into the run window. This boots the Registry Editor.

2: You will see a NoSql Like Database, follow this directory: "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run". Click the "Run" folder.

3: Inside the run folder, you will see a list of Values/Entrys of Type "REG_SZ". Inside the white space, right click. New>String Value. Once you click string value, a new entry will be created.

4: Right click the new entry, and click "Modify". Name the entry whatever you want, but put the directory to your autohotkey (.ahk) file in the "Value Data" field. Example "C:\MyCoolScript.ahk" or "E:\MyCoolExecutable.exe".

Next time the system is booted, all entrys in the Run folder will be evaluated, all files will be opened or executed. If you have the AutoHotKey installed, the ".ahk" file will execute.

starball
  • 20,030
  • 7
  • 43
  • 238
STC2
  • 23
  • 4