Automatically Add Windows Shortcuts to the QuickLinks App (AutoHotkey Tip)

Rather than Manually Creating Windows Shortcuts for QuickLinks.ahk, Use the AutoHotkey FileCreateShortcut Command

*          *          *

Recently, while working with the QuickLinks.ahk script, I’ve encountered so many AutoHotkey learning points involving a number of different techniques that I plan to spend the next few blogs discussing the various possibilities. If you regularly use QuickLinks, then you’ll likely want to fashion it to your needs. While most of the tailoring gets done by working directly with the target folders, you’ll find times when changing the code works best. Rather than attempting to deliver a final product for final download, I offer instruction on how to add various features to your version of QuickLinks.ahk and leave the work up to you. The example shown in the image below reflects the changes I’ve made to my personal copy and do not appear in the posted version.

*          *          *

One of the characteristics I like most about the QuickLinks.ahk script is its simplicity. It operates on a basic backbone which includes two loops (the files and folders Loop command). The first loop works through the folders found in the QuickLinks directory creating the top level menu. The second loop adds the individual links in each folder to each main menu item.


While looping, the script accesses the names of the folders in the main QuickLinks folder and loops through each of those folders adding the associated submenu items from the shortcuts contained within:


This makes it possible to customize all the menus by merely making changes in each Windows QuickLinks folder and subfolder.

While working directly in the Windows folders makes tailoring the menus easy, each addition requires a couple of manual steps. First, create the Windows shortcut (usually by selecting Create Shortcut or New⇒Shortcut  from the Windows right-click menu). Next, drag or copy the new shortcut into the appropriate QuickLinks subfolder. Wouldn’t it be nice if we could add a shortcut directly to the QuickLinks menu using that same menu? With the FileCreateShortcut command, we can do just that!

Using QuickLinks to Add Programs to QuickLinks

The current structure of the QuickLinks.ahk script makes it relatively easy to add a Windows shortcut creation routine to the menu using the FileCreateShortcut command. We only require the name and path of the Windows program or file and the name of the QuickLinks folder where we plan to create the new shortcut. (You can find a fairly complete discussion of how to setup and use QuickLinks at the “A Free QuickLinks Windows App from Jack” Web page.  Three chapters of the e-book Digging Deeper Into AutoHotkey discuss the QuickLinks.ahk script. Plus, Chapter Thirty of AutoHotkey Applications explains how to add icons to AutoHotkey menus.)

First, we capture the name and path of the program or document from Windows Explorer (File Explorer) in the same manner as used in the Eliminate Hotkeys with AutoHotkey Menus (AutoHotkey Tip) blog.

Tip: Use the easy way for finding and selecting any program or app in Windows Explorer by either selecting “Open file location” from the right-click context menu for any app shortcut or opening the Properties window for the appropriate app shortcut (again, you’ll find the option in the right-click menu) and clicking the “Open File Location” button (see image below). Whether you need to open the Properties window or can directly open the file location depends on the type of shortcut (for Windows 10, Taskbar: right-click on Taskbar icon, right-click again on the program name, then Properties; Start Menu: type part of program name, right-click on listing, then select “Open file location” from the menu; Desktop: right-click on icon and select “Open file location” from the menu.

The option “Open file location” often appears in the right-click context menu (AppsKey), but the “Open File Location” button in any Windows shortcut Properties window does the same job.

Next, we need a method for creating the new shortcut in the proper folder. For that, we build a new AddlinksMenu from the current main loop by adding one line of code:

 Menu, AddLinksMenu, Add, %A_LoopFileName%, LinksHandler

Place this first line inside the main loop. It creates the submenu which selects the appropriate folder for Windows shortcut creation.

Note: Many people (including myself) might write an additional loop just to create this menu. However, why not take advantage of the current main loop which already cycles through all the desired folders? Take the time to think about where you might get more work done by taking advantage of the current code. It saves you time and debugging.

Next, we add a new permanent menu item to the main menu to attach the new AddLinksMenu to the top level menu:

 Menu, QuickLinks, Add, Edit QuickLinks, QuickLinksHandler
 Menu, QuickLinks, Add, Add Links, :AddLinksMenu
 Menu, QuickLinks, Add, Reload QuickLinks, ReloadHandler
 Menu, QuickLinks, Add, QuickLinks Help, HelpHandler

Place this line of code after the completion of the two loops but before the end of the auto-execute section of the script which runs whenever loading or reloading.

Finally, we insert the LinksHandler subroutine which closely resembles the code from the last blog:

 Clipboard =
 Send, ^c
 ClipWait 0
 SplitPath, Clipboard, Name, Dir, Ext, Name_no_ext, Drive
 IfExist, C:\Users\%A_UserName%\QuickLinks\%A_ThisMenuItem%\%Name_no_ext%.lnk
   MsgBox, %Name% previously added to %A_ThisMenuItem% QuickLinks folder.
   FileCreateShortcut, %clipboard%, C:\Users\%A_UserName%\QuickLinks\%A_ThisMenuItem%\%Name_no_ext%.lnk ,%Dir%
   MsgBox, %Name% added to the %A_ThisMenuItem% QuickLinks folder.

You can place this snippet of code anywhere after the auto-execute portion of the script as long as it does not interfere with the flow of any other Label subroutine, function, Hotkey, or Hotstring. For simplicity, you might insert it at the end of the file.

These three separate pieces of code build the interface for automatically adding the create shortcuts feature to the QuickLinks menu for any program selected in Windows Explorer. After selecting the target program or file in Windows Explorer, activate the QuickLinks menu, then select Add Links and the appropriate location for the new shortcut:

The Add Links option appears among the permanent menu items at the bottom of the main QuickLinks menu. The attached submenu replicates all of the folders in the main menu. Selecting a folder creates a new Windows shortcut in that folder, then reloads the menu.

While the first two lines of code mentioned above set up the new Add Links menu, the Label (subroutine) LinksHandler does the work. After copying the selected program name to the Windows clipboard, AutoHotkey breaks apart the file name into its components. The SplitPath command parses the filename into its various parts (Name, Dir, Ext, Name_no_ext, Drive). Since, the FileCreateShortcut command uses different pieces of the filename, splitting the filename apart makes creating the shortcut easy.

The script checks to see if the shortcut already exists (IfExist command). If so, a MsgBox pops up confirming it. If not, AutoHotkey uses the FileCreateShortcut command with the appropriate pieces from the SplitPath command to create it, then pops up a MsgBox to that effect.

Finally, AutoHotkey uses the Reload command to rewrite the menu with the new shortcut included.

The only quibble I have with the FileCreateShortcut command occurred when I highlighted a URL for a Web page. The command created a regular shortcut (.lnk) rather than a Web shortcut (.url). However, the link functioned properly by loading the target Web page.



Leave a Reply

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

You are commenting using your 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