Can I Pin a File to the Start Menu by Using a Script?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Can I pin a file to the Start Menu by using a script?

— ZD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZD. For those of you who haven’t bought Windows XP yet (hey, what’s the matter with you; we Scripting Guys have families to feed!), by default XP’s Start Menu changes over time: the more you use an application, the more likely it is to appear on the Start Menu. The less you use an application – well, use it or lose, huh? (Incidentally, this refers solely to the application shortcuts that appear when you first click the Start button. Shortcuts that show you up when you click All Programs stay put, and don’t appear or disappear depending on use.)

Most of the time this works out pretty good; after all, the applications you use most often are probably the applications you use most often. However, there might be an application or two that you always want to appear on the Start menu even if you don’t use the program all that often. In that case you can “pin” the item to the Start menu; once pinned, the application will stay on the Start menu forever (or until you unpin it), even if you never use the application.

In Windows Explorer, you can pin an application to the Start Menu by right-clicking the application icon and then clicking Pin to Start Menu. Nothing wrong with that, of course, but if want to really be cool you can pin items to the Start Menu using a script. Here, for example, is a script that pins the Windows Calculator (calc.exe) to the Start Menu:

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(“C:\Windows\System32”)
Set objFolderItem = objFolder.ParseName(“calc.exe”)

objFolderItem.InvokeVerb(“P&in to Start Menu”)

As you can see, we use the Shell Application object in order to perform this task. After creating an instance of the Shell object, we use the Namespace method to bind to the folder (C:\Windows\System32) where Calculator resides. We then use the ParseName method to bind to the actual executable file (calc.exe).

Why can’t we just bind directly to the file? Who knows; that’s just the way the Shell object is designed. But hey, it’s just one extra line of code, and the net result is the same: we get hooked up to C:\Windows\System32\Calc.exe.

After that we use the InvokeVerb method to pin the application to the Start Menu. Verbs are items that appear on the context menu when you right-click the application icon in Windows Explorer. And that’s not a misprint in P&in to Start Menu; the ampersand is part of the command name, and indicates the shortcut key for the item (in this case, the letter i). It looks weird, but it has to be there.

Ok, ok, how did we know the ampersand has to be there? Simple: we ran this script, which returns a list of verbs that can be applied to Calc.exe:

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(“C:\Documents and Settings\gstemp\Desktop\Stuff”)
Set objFolderItem = objFolder.ParseName(“bob.vbs”)

Set colVerbs = objFolderItem.Verbs For Each objVerb in colVerbs Wscript.Echo objVerb Next

And, yes, these verbs are pretty cool, although they aren’t as cool as you might expect. That’s because when you invoke a verb, that verb functions exactly the same as if you had chosen the item from the context menu. For example, if you right-click a file and choose Delete, a dialog box pops up asking if you want to send the file to the Recycle Bin. The same thing happens if you call the Delete method programmatically; instead of automatically deleting the file, that same confirmation box appears. Give this script a try, and you’ll see what we mean:

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(“C:\Windows\System32”)
Set objFolderItem = objFolder.ParseName(“calc.exe”)

objFolderItem.InvokeVerb(“&Delete”)

Still, something worth playing around with.

And before you ask, the answer is yes: you can also unpin an item from the Start Menu by using a script:

Set objShell = CreateObject(“Shell.Application”)
Set objFolder = objShell.Namespace(“C:\Windows\System32”)
Set objFolderItem = objFolder.ParseName(“calc.exe”)

objFolderItem.InvokeVerb(“Unp&in from Start Menu”)

Could life get any better?


0 comments

Discussion is closed.

Feedback usabilla icon