Try this. It will run the associated application (if it has one) for any file you selected from the list and load that file.

It avoids having to do any registry lookups for install paths by simply starting cmd.exe passing the name of the file you wish to edit as a parameter.

It relies on CMD's smarts to work out which application to run in the same way as when type just the name of a file on the command line and hit enter.This greatly simplifies the code for your application.

I've tested it on .doc (WORD), .pdf (ACROBAT), .html (Opera on my system), .jpg (ACDSee), .zip (WinZip), .mpg (WinAmp) and they all worked with the code below. It should work for any file for which you have an association.

In fact, with a minor change, it even works for .pl file starting perl and leaving the command shell window open even after the script completes, but I intend writing that up seperately.

I've hacked your code around a lot getting this to work, so if you have any questions about what or why I did it, feek free to ask.

# !perl -sw use Tk; use strict; use Win32::Process; local $|=0; my $W = MainWindow->new; $W->title("Select Job"); $W->iconname('select job'); my $file_selected; # Read the directory contents - Put in a path were you have files plac +ed # use a ARG of . to select from the current directory. my @dirContents = readDir ($ARGV[0]||"C:\\public"); my $TOP = $W->Toplevel; $TOP->title("data contents"); $TOP->geometry("+70+120"); $TOP->configure(-background => lc("aquamarine2")); my $tFrame = $TOP->Frame; my $bFrame = $TOP->Frame; $tFrame->configure(-background => lc("PaleTurquoise")); $bFrame->configure(-background => lc("PaleTurquoise")); # Create the label for editing the file my $lab = $tFrame->Label(-text => "Double click to edit file: ", -background => "PaleTurquoise", -anchor => 'e'); # Create a listbox with the contents of the directory. my $ent = $tFrame->Scrolled(qw/Listbox -setgrid 1 -height 16 -width 40 + -scrollbars e -selectmode single/); for(@{dirContents}) { $ent->insert(0, "$_"); } $ent->bind( '<Double-Button-1>', sub { my $pid; $file_selected = $ent->get('active'); print .$/; Win32::Process::Create( $pid, $ENV{COMSPEC}, "/c \"$file_selected\"", 0, 0, "." )|| die ErrorReport(); } ); # Quit the edit directory contents popup my $quitButton = $bFrame->Button(-text => "Quit", -width => 12, -background => "PaleTurquoise", -activebackground => "PaleTurquoise", -command => sub { $TOP->destroy; } # strictly not nece +ssary ); # Instead of pack, using grid to place more precisely. Tk::grid($tFrame, -row => 0, -column => 0, -sticky => 'w', -padx => 10 +, -pady => 10, ); Tk::grid($bFrame, -row => 1, -column => 0, -sticky => 's', -padx => 10 +, -pady => 10, ); Tk::grid($quitButton, -row => 0, -column => 0, -sticky => 'w', -padx = +> 10, -pady => 10, ); Tk::grid($lab, -row => 1, -column => 0, -sticky => 'w', -padx => 10, - +pady => 10, ); Tk::grid($ent, -row => 1, -column => 1, -sticky => 'w', -padx => 10, - +pady => 10, ); MainLoop; sub readDir { my ($DirName) = @_; opendir (INDIR,$DirName) or die "\nCannot open $DirName: $!\n"; my @DirContents = grep{ !/^\.\.?$/ && -f $_ } map{ "$DirName\\$_" }readdir(INDIR); close (INDIR); return @DirContents; }

Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

In reply to Re: How to look for application path by BrowserUk
in thread How to look for application path by juo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.