Adding Startup Folder Shortcuts to a System Tray Menu (AutoHotkey Startup Control)

By Loading the Startup Folder Shortcuts into a Menu, We Can Access the Apps Even When No Icon Appears in the System Tray

Last time (“Collecting File Information from Windows Folders Using AutoHotkey“), I produced a simple MsgBox displaying the Windows shortcuts found in the Startup folder. When Windows launches, it reads and loads the programs or shortcut targets located in that folder. This provides AutoHotkey users an easy method for auto-loading their most-used scripts. However, the more scripts, the more clutter that appears in the System Tray in the form of AutoHotkey icons. You can reduce the crowding by adding the Menu,Tray,NoIcon command line to each script but then you need a technique for quickly reaching those hidden apps.

By inserting the shortcut names into a separate System Tray right-click context menu, you can both generate a list of shortcuts and provide quick access to the scripts. In this barebones AutoHotkey script, I create a menu that opens either the target script in Notepad (.ahk files) or the folder for the program (.exe files).

Add Shortcuts to the System Tray Menu

In my last blog, I wrote a simple Files Loop that scanned the Startup folder while capturing the name of each shortcut—then displayed the shortcuts in a MsgBox. This time, using the same Files Loop presented in the last blog, I insert those shortcut names into a System Tray right-click menu. This auto-startup control routine located in the auto-execute section of the script builds a menu for opening files/folders of the target of each shortcut:

Loop %A_Startup%\*.*
	FileGetShortcut, %A_LoopFileFullPath%
		, Location
		, OutDir
		, OutArgs
		, OutDescription
		, OutIcon
		, OutIconNum
		, OutRunState
	If ErrorLevel
	Menu, Tray, Add , %A_LoopFileName%, MenuAction
	; If separate shortcut icon exist, insert it!
	If (OutIcon != "")
		Menu, Tray, Icon, %A_LoopFileName%
				, %OutIcon%, %OutIconNum%
Menu, Tray, Icon, Shell32.dll, 85

(Line 3 and line 16 in the above snippet uses line continuation techniques to wrap lines of code for display purposes.)

The Files Loop both identifies the Windows shortcuts and extracts key data about the target file and the icon in the shortcut (FileGetShortcut, %A_LoopFileFullPath%). If not a shortcut (If ErrorLevel), the script runs the Continue command—restarting the loop with the next item in the list.

The script uses the shortcut name (%A_LoopFileName%) for each menu item:

Menu, Tray, Add , %A_LoopFileName%, MenuAction

When manually set (If (OutIcon != "")), the script provides easier menu item recognition by inserting the shortcut’s icon. AutoHotkey uses shortcut icon (OutIcon) and number (OutIconNum) data extracted from the shortcut to add the icon:

Menu, Tray, Icon, %A_LoopFileName%, %OutIcon%, %OutIconNum%

AutoHotkey can insert either separate supported icon image files (ICO) or icons found in various other file formats (e.g. EXE, DLL) or Windows System files (e.g. %SystemRoot%\System32\SHELL32.dll).

Tip: You must manually set the shortcut icons to make them AutoHotkey-readable. For more information about changing icons in Windows shortcuts, see Chapter 4.1.8 “Using Unique Icons for Specific Windows Shortcuts” in the book Jack’s Motley Assortment of AutoHotkey Tips.

Each menu item calls the subroutine MenuAction—either opening the .ahk file in Notepad for editing or opening the folder where the target .exe resides—the most likely place to find the uncompiled .ahk script file.

The last line of the auto-execute code sets the System Tray icon to something unique:

Menu, Tray, Icon, Shell32.dll, 85

Tip: You can use an AutoHotkey script such as ImageList.ahk (discussed in AutoHotkey Applications: Ideas and Tips for Writing Practical AutoHotkey Scripts) to investigate various images when searching for icons.

Open Either Notepad to Edit AHK Scripts or Windows File Explorer to View EXE Files

When clicking a menu item, the following short MenuAction subroutine uses the same FileGetShortcut command from the auto-execute section to extract target file data:

	Shortcut := A_Startup "/" A_ThisMenuItem
	FileGetShortcut, %A_Startup%/%A_ThisMenuItem%
			, Location
	If InStr(Location,".ahk")
		Run, Notepad %Location%
	If InStr(Location,".exe")
		explorerpath := "explorer /select," Location
		Run, %explorerpath%

If the target includes the .ahk extension (If InStr(Location,".ahk")), AutoHotkey opens Notepad and loads that file (Run, Notepad %Location%). You can change the opening program to your preferred editor.

Since we can’t edit .exe files (If InStr(Location,".exe")), AutoHotkey opens the target folder location:

explorerpath := "explorer /select," Location
Run, %explorerpath%

In that folder, we just might find the .ahk file sitting next to the compiled .exe file. You can take it from there.

This System Tray right-click menu offers a basic form of auto-startup control. You can add more tools (e.g. removing shortcut the Startup folder, reloading, exiting, etc.) by adding submenus—a topic for next time.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)


This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

One thought on “Adding Startup Folder Shortcuts to a System Tray Menu (AutoHotkey Startup Control)

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 )

Facebook photo

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

Connecting to %s