Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: How to look for application path

by BrowserUk (Pope)
on Sep 30, 2002 at 10:24 UTC ( #201666=note: print w/replies, xml ) Need Help??

in reply to How to look for application path

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!

Replies are listed 'Best First'.
Re: Re: How to look for application path
by juo (Curate) on Sep 30, 2002 at 11:48 UTC

    This is working great. I didn't know we could get the application links from Windows parsed into Perl. I was going the though way while it could be done so easy. Thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://201666]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-09-23 14:15 GMT
Find Nodes?
    Voting Booth?
    Eventually, "covfefe" will come to mean:

    Results (191 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!