11 Nov 2008

Invoking a Script from the Windows context menu

After having written a useful script to convert long filenames to short ones, I will now explain how to create a new menu (titled, say "Get Short Path for this") under the windows context menu and invoke this script when this is clicked. Needless to say, these instructions can be used with pretty much with any script.
I am going to use the script discussed in my previous article to illustrate the process. In short, we have a script that we execute against ALL (or specific type of files). The best way to make it useful to the user is when it becomes available via the right-click looking like the screenshot below

image

Here's how I did it. But a short but important warning before I proceed
The steps below involve making changes to a critical part of the Windows Operating System - the registry. If you've never made changes to the registry or do not know what "the registry" refers to - then doing this maybe a bad idea! Please do so at your own risk.
Let's get started!.

Start the registry editor (Start >> Run >> regedit). In the pane on the left-hand, navigate to the key My Computer\HKEY_CLASSES_ROOT\*\Shell  and create a new key under it named "shortpath". A quick tip here: Choosing the * key means that this change will apply for ALL files. If your script works on or with specific extensions, you may want to make a different choice. Ask me in the comments below if you can't figure it out.

Now navigate into your newly created key My Computer\HKEY_CLASSES_ROOT\*\Shell\shortpath and in the right hand pane double click on the value named "(Default)" to edit it. Type in "Get Short Path for this". As you may have guessed, this value contains the menu name (see first screen shot in this post). You can change it to anything else that you find suitable. See screen shot below.
image

In the left hand pane create a new sub-key under My Computer\HKEY_CLASSES_ROOT\*\Shell\shortpath named "command" (it is important that you do not change this name) and navigate into it. In the right-hand double click on the value name "(Default)".

image
In the dialogue box that appears, type in the command-line to be executed when our new menu is clicked.  Which in our case is:

wscript.exe "D:\Utils\myscripts\longtoshort.vbs" "%1"

You may want to change the path (shown in bold above) to match the folder where your script is placed. It is important that you do not change anything else and not miss any double quotes. Click OK and you are done! Note that if you are doing this using a different script, this is where you use your specific path and name. Remember this particular will always need point to the actual command that Windows executes when  the user clicks on the specific menu item that we created in the previous step.

Close the registry editor and test this by right-clicking any file in any folder. In my case the new menu item appeared instantly. If you don't see the menu, you may want to try rebooting your PC and try again.

Let me know if it works for you and whether you went on to do anything different with this.

Previous Post: Getting 8.3 (or short) path from a long one
Also worth reading: A short discussion getsatisfaction.com wh