http://www.perlmonks.org?node_id=277702


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

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?

Replies are listed 'Best First'.
Re: Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by BrowserUk (Patriarch) on Jul 25, 2003 at 00:13 UTC

    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

      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.

        Nice one. Thanks.

        In fairness to the OP, he has corrected the "manual regedit" situation with a follow-up post which appears below.


        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. :)

        No. Not a relative path :)

        The correct term is "short names". Example

        P:\test>dir /x "d:\Program*" Volume in drive D is Winnt Volume Serial Number is D822-5AE5 Directory of d:\ 19/07/03 06:29 <DIR> PROGRA~1 Program Files 1 File(s) 0 bytes 1,153,617,920 bytes free

        Here the full (or long) name of the directory is "Program files". The short name is "PROGRA~1".

        This was the hack MS introduced to allow Dos/Win95 programs that were written expecting the old 8.3 filename format to continue to work once they introduced the long filenames that could include spaces & other 'funny characters' and multiple .s etc. Mostly I had forgotten they still existed, but for some reason, when you use the "%1" syntax as I described above, you get the short form rather than the long one.

        For many things, rename, open, opendir etc. these work just fine, but if you were trying to match file or directory names that followed some pattern then you aren't going to get the expected result when you get given a shortened form.


        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

Re: Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by svsingh (Priest) on Jul 24, 2003 at 22:33 UTC
    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";
Re: Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by BUU (Prior) on Jul 24, 2003 at 22:19 UTC
    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>..
Re: Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by strom (Initiate) on Jan 14, 2004 at 14:56 UTC
    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: Re: (Not Quite Perl) Running Scripts from Right-Click Context Menu in Windows
by svsingh (Priest) on Jul 28, 2003 at 14:43 UTC
    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.