Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

(Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows

by svsingh (Priest)
on Jul 24, 2003 at 16:40 UTC ( #277596=perlmeditation: print w/ replies, xml ) Need Help??

Note: This isn't really a Perl thing, but I'm using it for Perl scripts and it's too handy not to share.

I have a few scripts that I run frequently for work. Most parse the files in a directory and create an HTML report file. I wanted to share these with coworkers, but didn't want to have to explain the command prompt to them. My solution was going to be telling everyone to stick the .pl file in their local directory and then double-click it. (They already have Perl installed.) The problem is that we need to run these scripts in many, many directories and there are too many potential problems that can occur when you have multiple copies of the same script all over a hard drive.

I was thinking about a way to make this easier. The best idea I had was to allow them to right-click on a folder in Explorer and run the script from the context menu. I just had no idea how to do that. Thanks to Windows 98 Right Mouse Click Customisation, I was able to figure it out.

If you're interested in trying this, here are the steps.

Note: These instructions were written and tested using Windows 2000. They may be different for your version. Oh, and it's the registry, be careful in there. If you don't know the risks of editing registry keys, then don't do it.

  1. Open the Registry Editor by running regedit.
  2. Expand HKEY_CLASSES_ROOT > Directory.
  3. Right-click on shell under Directory and select New > Key.
  4. Enter a name for the script you're adding. I don't know if there are restrictions, but since this is not for display, don't use anything by letters and numbers (just to be safe).
  5. Left-click on the name you just entered. You'll see a (Default) value in the main window. Right-click on (Default) and select Modify.
  6. Enter the display name for your script in the Value data field. Click OK when you're done.
  7. Right-click on the name you entered in step 4 and select New > Key.
  8. Enter command.
  9. Click on the command folder. You'll see a (Default) value in the main window. Right-click on (Default) and select Modify.
  10. Enter the command line for your script in the Value data field. For example, perl.exe c:\work\perl\qaHelpTitles.pl. Click OK when you're done.
  11. Open an Explorer window and test by right-clicking on a folder and selecting the display name you entered.
  12. If everything worked, then close the Registry Editor. If there were problems, then you may want to verify that perl.exe is in your path or the path and filename for the script are correct. Try pasting the command line into a Command Prompt window and running it that way. If it works, then I'm not sure what went wrong here. Sorry.

Hope this helps!

Comment on (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
Select or Download Code
Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by revdiablo (Prior) on Jul 24, 2003 at 21:36 UTC

    This is very cool. It seems to work fine on WinXP as well. The only question I have is, how do you find out which directory was right clicked on to run the script? I tried using Cwd, but that gives me the directory one level up. (i.e. when I right click on the directory c:\devel\perl\test and select my context-menu script, it gets Cwd as c:\devel\perl). Any ideas?

      Off the top of my head I'm guessing it's either passed as an arg or piped in, so try looking at @ARGV or <STDIN>..
      Strange. I tried the following code and it printed the directory I clicked on. I think when you right-click on a directory and run a program, it runs out of that directory. What happens if you run the Cwd script from c:\devel\perl\test as a command line app?
      use Cwd; my $curDir = cwd; print "$curDir\n";

      If you modify svsinghs step 10 as follows:

      perl.exe c:\work\perl\qaHelpTitles.pl "%1"

      Then the name of the directory that you right-clicked on to run the script will be supplied to the script as $ARGV[0].

      A word of caution though. The path supplied is one of those dratted shortened thingies. It's been so long since I encountered one of these that I forgot what they are called?. These work okay for many purposes, but are likely to confuse some things. There's probably a simply way of retriving the expanded version from the shortened one, but I haven't looked that up yet.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

        Perhaps you mean a relative path? If that's the case, it's simply a matter of concatenating it with the Cwd. In any case, thanks for the tip. I'll be sure to play with it next time I'm on a winbox. :)

        A word of caution though. The path supplied is one of those dratted shortened thingies. It's been so long since I encountered one of these that I forgot what they are called?. These work okay for many purposes, but are likely to confuse some things. There's probably a simply way of retriving the expanded version from the shortened one, but I haven't looked that up yet.

        You don't have to look it up, because I know and now I'm going to tell you.

        Substitute "%L" (case does not matter, but in many font faces it is maddeningly difficult to tell a lowercase "l" from a numeral "1") for "%1".

        There's a strong connection between this node and the one I created here; this (the top node) basically explains how to do what I didn't cover over there.

        The one item not covered in the top node discussion was that using Regedit manually is not the best way to do this. Creating a .REG file by exporting a correctly done new key as detailed in the instructions given, and then merging it with the target Registry is the better way (this is commonly accepted). Only one person has to get it right, and then it's easy to propagate.

        One .REG file can serve as a template for any number of Perl scripts that one would want to run as context-click GUI add-ons. Only a few characters of the keynames and the path to the perl script need to be changed.

      I noticed this myself on the weekend. I'm not really sure why, but when I right-click on a directory in the directory-tree (left-hand side of the Explorer window), the script runs from inside the directory. When you right-click on a directory in the file list (right-hand side), then it runs from the parent directory.

      Sorry this isn't much of a solution, but it's a decent workaround.

      you can use right click - SendTo and $ARGV[0]. 1. create your script/exe: $inputfile = $ARGV[0]; .... 2. create link in SendTo folder pointing to your exe 3. now you can simple right click on any file and choose SendTo -> your exe. it works with perl2exe and perlapp, but not with PAR. -- strom
Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by svsingh (Priest) on Jul 25, 2003 at 00:05 UTC
    Instead of making my coworkers play with the registry, I thought I'd try to automate these steps too. If you're interested, here's a generic installer I whipped up. Just copy the code into a text editor and save it as a .reg file. Then double click on the .reg file to populate the registry.
    REGEDIT4 ; Replace the following values with your own information. ; ; <INTERNALNAME> = A name for the shortcut. This will not ; be seen, so just use alphanumeric chars. ; You must change this twice below. ; <DISPLAYNAME> = The name of the right-click shortcut. ; <PATH_AND_FILE> = The path to the Perl script. Use double ; backslashes. For example: ; c:\\scripts\\foo.pl [HKEY_CLASSES_ROOT\Directory\shell\<INTERNALNAME>] @="<DISPLAYNAME>" [HKEY_CLASSES_ROOT\Directory\shell\<INTERNALNAME>\command] @="perl.exe <PATH_AND_FILE>.pl \"%L\""

    Update: Added "%L" to pass the directory to the script. Thanks BrowserUK and Intrepid.

Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by GermanHerman (Sexton) on Jul 27, 2003 at 12:22 UTC
    Does anyone know how to do this in context menu in a (any) browser? Or how about the context menu in mozzila? On x? -Douglas
      I run Redhat Linux 8, and currently use the Ximian Desktop. If you right click on the desktop there is a 'scripts' menu item, and scripts folder. Here is the description
      All executable files in this folder will appear in the Scripts menu. C +hoosing a script from the menu will run that script. When executed from a local folder, scripts will be passed the selected + file names. When executed from a remote folder (e.g. a folder showin +g web or ftp content), scripts will be passed no parameters. In all cases, the following environment variables will be set by Nauti +lus, which the scripts may use: NAUTILUS_SCRIPT_SELECTED_FILE_PATHS: newline-delimited paths for selec +ted files (only if local) NAUTILUS_SCRIPT_SELECTED_URIS: newline-delimited URIs for selected fil +es NAUTILUS_SCRIPT_CURRENT_URI: URI for current location NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window<b +r>
      You should be able to move any perl scripts to the appropriate folder, (on my system this is "/home/user/.gnome2/nautilus-scripts/"), on your system (if available) for similar results, YMMV.

      >>Does anyone know how to do this in context menu in a (any) browser?

      This might help...

      1. Write a script that you want to execute.

      <SCRIPT LANGUAGE="JavaScript" defer> window.open("http://developer.ebay.com","","height=800,width=800,lef +t=80,top=80,scrollbars=yes"); </SCRIPT>

      2. Point to the script from your IE Menu Extensions. Here's an example of a generic installer (leveraging the excellent example from svsingh):

      REGEDIT4 ; Replace the following values with your own information. ; ; <INTERNAL AND DISPLAY NAME> = Name of the context menu item. ; This will be seen in the navigation pane of the ; registry editor AND in your IE context menu. ; Use alphanumeric chars. Spaces are OK. ; <PATH_AND_HTM_FILE> = The path to the script file. Use double ; backslashes. For example: ; C:\\IEScripts\\foo.htm [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\<INTER +NAL AND DISPLAY NAME>] @="file://<PATH_AND_HTM_FILE>"

      See MSDN for additional options that control which context menus in which the item will appear, and how to set the DWORD in the above .reg file: About the Browser > Controlling the Context Menus.

Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by Anonymous Monk on Oct 06, 2004 at 18:51 UTC
    It work great on folders but, how can you get it to work on indivual files? I would like to link scripts to specific file types (.txt, .m3u, etc) so I can right-click and execute the command.
      This is an old thread, but it came up when I searched for a possible solution for an issue I am having.

      What I do for AutoCAD dwg files (under windows xp) is go to Folder Options->File Types->DWG->Advanced->New

      Then in Action I put a description, like Print 11x17 and under Application used to perform action it would be:

      "c:\program files\autocad 2004\acad.exe" "%1" /nologo /b plot11x17.scr

      What it does may be irrelevant but it runs a script then closes. This means you can highlight many dwgs, right click and have 12 copies of autocad open, run a script, then close.

      This brings me to a question I have.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://277596]
Approved by Popcorn Dave
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-09-22 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (185 votes), past polls