September 1st, 2005

How Can I Add a Shortcut to the Quick Launch Tray?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add a shortcut to the Quick Launch tray?

— ZR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZR. Before we answer this question we need to first answer a different question. And here’s the answer: Frank “Home Run” Baker, who led the major league in homers four times, even though he never hit more than a dozen home runs in a single season.

Oh, wait; wrong question. Here’s the answer: no, unfortunately there’s really no way to enable and disable the Quick Launch tray using a script. There are some workarounds floating around the Internet that use the SendKeys commands, but SendKeys is a bit … flaky … (to say the least) so we don’t really recommend it. But if you search for Quick Launch and SendKeys you should be able to find a sample without any problem.

Assuming that the Quick Launch tray is already enabled, however, it’s pretty easy to add a new shortcut. This script, for example, adds Notepad.exe to the Quick Launch tray:

Set objShell = CreateObject(“WScript.Shell”)
Set colEnvironmentVariables = objShell.Environment(“Volatile”)

strFolder = colEnvironmentVariables.Item(“APPDATA”) & _ “\Microsoft\Internet Explorer\Quick Launch”

Set objShortCut = objShell.CreateShortcut(strFolder & _ “\Notepad.lnk”) objShortCut.TargetPath = “Notepad.exe” objShortCut.Description = “Open Notepad” objShortCut.HotKey = “Ctrl+Shift+N” objShortCut.Save

This script actually accomplishes two tasks. First, it determines the location of the Quick Launch folder for the logged-in user. (We should point out that this script must be run on the local computer. That’s because it relies on Windows Script Host to actually create the shortcut; without yet another set of workarounds WSH can’t be used against remote computers). To get the path to the Quick Launch folder we create an instance of the WScript.Shell object and then use this line of code to retrieve a collection of environment variables:

Set colEnvironmentVariables = objShell.Environment(“Volatile”)

As it turns out, there’s an environment variable named AppData that pinpoints the location of the Application Data folder. (If you don’t believe us, type echo %appdata% at the command prompt and see what happens.) Application Data is the “host” folder for the Quick Launch folder; the path to the Quick Launch folder will always be the path to the Application Data folder plus \Microsoft\Internet Explorer\Quick Launch. In other words, suppose this is the path to the Application Data folder:

C:\Documents and Settings\kenmyer\Application Data

In that case, this will be the path to the Quick Launch folder:

C:\Documents and Settings\kenmyer\Application Data\Microsoft\Internet Explorer\Quick Launch

In turn, that means we can use this code (which combines the value of the AppData environment variable with the string \Microsoft\Internet Explorer\Quick Launch) to get the location of the Quick Launch folder and store that value in a variable named strFolder:

strFolder = colEnvironmentVariables.Item(“APPDATA”) & _
 “\Microsoft\Internet Explorer\Quick Launch”

So much for task number 1. Task number 2 involves creating the shortcut for Notepad.exe, something we do here:

Set objShortCut = objShell.CreateShortcut(strFolder & _
 “\Notepad.lnk”)
objShortCut.TargetPath = “Notepad.exe”
objShortCut.Description = “Open Notepad”
objShortCut.Save

We start off by calling the CreateShortcut method, passing it the path to the Quick Launch folder plus \Notepad.lnk, the file name for our new shortcut. We then set the following properties of the new shortcut:

Property

Description

TargetPath

File name/path to the – in this case – executable file we want to run any time we click the new shortcut. Because Notepad.exe is in the Windows path all we need to do is specify the file name: Notepad.exe.

Description

“Tool tip” for the shortcut. (That is, the comment that appears when you hold the mouse over the shortcut.)

That’s all we really need to do. There are other properties we can set for our shortcut; for a more complete list, take a look at the Windows Script Host Reference on MSDN. For our purposes, however, we’ll simply set the TargetPath and Description, and then call the Save method to save the new shortcut to the Quick Launch bar:

objShortCut.Save

Child’s play.

By the way, we know of another interesting piece of trivia regarding Home Run Baker, but TechNet probably wouldn’t appreciate it if we related that information here. If you’re interested, though, just search for Home Run Baker and – uh, well, never mind. Let’s just remember Home Run Baker for leading the league in home runs four times and call it good.

Author

0 comments

Discussion are closed.