Automatically Launch Apps at Windows Startup (AutoHotkey Tip)

Discover Autohotkey Tricks By Perusing Code In Other Scripts, Plus How to Load Any Windows Program at Startup

Many users find it easy to manually setup a program to auto-launch whenever they log onto Windows, but creating a shortcut and placing the new file into the Windows Startup folder requires a number of steps. With AutoHotkey, the same actions take just one command.

A couple of weeks ago, I highlighted the EitherMouse.ahk AutoHotkey script. If you’re left-handed, ambidextrous, use two monitors at the same time, or need two separate cursors on the screen (one for each hand), then EitherMouse.ahk does the job.

EitherMouse.ahk takes advantage of AutoHotkey code for altering the Windows Registry. Commands such as RegRead and RegWrite make it possible to directly change the Windows’ mouse actions. The OnMessage() and DllCall() functions make up significant sections of the scripts. These commands and functions represent advanced topics for anyone aspiring to the highest levels of AutoHotkey scripting. However, since I target new and intermediate AutoHotkey users, I don’t plan to address these topics—at least, not at this time. (I do reference these topics in some of my books and make use of these commands and functions in a couple of the free scripts, but I have not dug into them too deeply.)

AutoHotkey Library Deal

You can find plenty of useful tips in most AutoHotkey scripts without wading too far into the weeds. For example, if you want to auto-load an app on Windows startup, you can easily add a little routine found in EitherMouse.ahk. Plus, by modifying that same auto-startup routine, you can create a new Hotkey which turns any program into an auto-launch app when Windows loads.

How to Toggle Automatic Loading of AutoHotkey Scripts

As shown in the image below, the EitherMouse user can force the loading of the app automatically at Windows startup by selecting “Start with Windows…” from the System Tray menu. To disable the auto startup, simply toggle it off by clicking the same menu selection again. (The menu displays a check mark after activating the auto-startup.) I describe how to manually setup the auto-launching of your AutoHotkey scripts via the Windows Startup folder in Chapter Nine of the free e-book AutoHotkey Tips and Tricks. However, I don’t discuss the AutoHotkey command which makes it possible to add the Windows shortcut directly to the Startup folder through a script: FileCreateShortcut.

Start With Windows
By selecting “Start with Windows…” from the Tools menu (the little wrench icon at the top right of the System Tray menu), you can toggle on and off automatic launching of the EitherMouse app on Windows startup.

By tracing the Menu command in the EitherMouse.ahk code (searching for “Start with Windows…”), I found that ToggleStartup acted as the auto-launch subroutine:

Menu, Configure, Add, Start with Windows..., ToggleStartup

Then, I simply searched for the Label ToggleStartup:

ToggleStartup:
  IfExist, %A_Startup%\%Name%.lnk
  {
    FileDelete, %A_Startup%\%Name%.lnk
    Menu, Configure, Uncheck, Start with Windows...
  }
  Else
  {
    FileCreateShortcut, "%A_ScriptFullPath%"
            , %A_Startup%\%Name%.lnk
            , %A_ScriptDir%
    Menu, Configure, Check, Start with Windows...
  }
Return

After activation of the Label (subroutine) ToggleStartup, when the shortcut for launching does not exist in the Startup folder (%A_Startup%), the FileCreateShortcut command creates it. If AutoHotkey finds the shortcut exists in the Startup folder, it deletes it. This technique provides an easy way to turn the auto-launch feature on and off. The toggling routine also adds or removes the check mark to and from the Menu item.

Tip: To view the contents of your Startup folder, simply Run the command shell:startup (WIN+r or microsoft_key+r to open the Run dialog box).

Setup a Hotkey to Auto-Startup Any Windows Program

Now that we have an AutoHotkey routine for auto-launching the app upon Windows startup, we can modify it to load (as a shortcut) any program selected in Windows Explorer into the Windows Startup folder. (In Windows 10, Microsoft renames Windows Explorer to File Explorer. Both open with the WIN+E (microsoft_key+E) key combination.)

The new Hotkey (CTRL+WIN+3) simplifies the process for making any app an automatic part of your Windows experience:

^#3::             ; Activates with CTRL+WIN+3
  Clipboard =    ; Empties Clipboard
  Send, ^c        ; Copies filename and path
  ClipWait 0      ; Waits for copy
  SplitPath, Clipboard, Name, Dir, Ext, Name_no_ext, Drive
  IfExist, %A_Startup%\%Name_no_ext%.lnk
  {
    FileDelete, %A_Startup%\%Name_no_ext%.lnk
    MsgBox, %Name% removed from the Startup folder.
  }
  Else
  {
    FileCreateShortcut, "%clipboard%"
           , %A_Startup%\%Name_no_ext%.lnk
           , %Dir%   ; Line wrapped using line continuation
    MsgBox, %Name% added to Startup folder for auto-launch with Windows.
  }
Return

For this to work, you must select the target file from Windows Explorer. The new shortcut placed in the Startup folder opens either the target program or the default program for the specific type of target file.

The FileCreateShortcut command depends upon capturing the filename and path for the shortcut target, then parsing it into its components (path, filename, extension, folder, etc). Fortunately, the Send ^c copy command works on any filename highlighted in Windows Explorer. Whenever referencing the Clipboard variable in a script, AutoHotkey automatically converts the contents to the filenames in text (with the full path).

For greater reliability, we clear the Clipboard by setting it equal to nothing (Clipboard =). Then, the ClipWait command waits until the Send, ^c command copies the file into the Windows Clipboard. (Tip: The ClipWait command does not work unless the Clipboard is empty before issuing the copy command.)

The SplitPath command parses the filename into its various components (Name, Dir, Ext, Name_no_ext, Drive). Since, the FileCreateShortcut command uses different pieces of the filename, breaking it up into components makes creating the shortcut easy.

Note that we enclose the first parameter in the FileCreateShortcut command in double quotes (“%Clipboard%”—the complete filename with path). This allows the target filename string to include spaces without modification. Without the double quotes, any filename/path including one, or more, space causes problems.

The second parameter uses the filename without an extension (Name_no_ext) for the shortcut name replacing the extension with .lnk. The A_Startup variable inserts the path into the user’s Startup folder. (Windows computers launch anything found in the Startup folder when the user logs in.)

Tip: If you want to create a shortcut on the Windows Desktop, use the variable A_Desktop rather than A_Startup. However, pinning a shortcut to the Windows Taskbar involves more complications. I advise creating the shortcut on the Desktop, then drag it the Taskbar. Or, merely drag the program name from Windows Explorer to the Taskbar to pin a program shortcut.

Finally, the last parameter specifies the working directory (path) for the app or file (Dir). That’s all you need, although a number of other parameters exist to further tailor the FileCreateShortcut command.

If you accidentally add a program to the Startup folder, use the Hotkey a second time to deletes the shortcut. Of course, you can always open the Startup folder to remove unwanted links.

*          *          *

Like anybody else, I have expenses and a need to make ends meet. As “Jack’s AutoHotkey Blog” increases in popularity, coding the test scripts and writing the blogs takes up more of my time. That means I’ve less time to pursue other income earning opportunities. I don’t plan to ever move “Jack’s AutoHotkey Blog” behind a paywall, but if you think my efforts are worth a bit of your hard-earned cash, then you can offer a token of your appreciation by purchasing one or two of my AutoHotkey books. You may not need the references yourself, but you might know someone who can benefit from them.

Thank you,

jack

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s