Search code examples
perlfile-handlingfilehandlefilehandlerfileopendialog

How to call a system file selection dialog using perl on windows?


I know many perl based gui module would do that easily but I am curious about methods other than Prima or Tk etc.
I want to call this dialog (Exactly looking as Windows Explorer) for a command line package.

Is anything similar to system(''); command possible.

Pls Mind that, I need to open and process the file/s selected by such dialog via perl.


Solution

  • It's not easy, first you have to build the data structure needed by the OpenFileDialog api, interact with the system at a low level, track file handles, then get and parse the resulting structure.

    There are some horrible hacks where you save a windows script, and run it from perl using a system as you suggested. Keep in mind this requires the right permissions, and scripting languages installed.

    # From http://www.pcreview.co.uk/threads/launching-a-search-explorer-exe-form-the-command-line.1468270/
    system( q{"echo CreateObject("Shell.Application").FindFiles >%temp%\myff.vbs"} ); #setup script
    my @files = system( "cscript.exe //Nologo %temp%\myff.vbs" ); # Get files
    system( "del %temp%\myff.vbs" ); # Cleanup
    

    The authors of Win32::GUI have done a great job of simplification,

    my @file = Win32::GUI::GetOpenFileName ();
    

    Or with some helpful options like filtering, starting directory and window title.

    # A simple open file with graphic filers
    my ( @file, $file );
    my ( @parms );
    push @parms,
      -filter =>
        [ 'TIF - Tagged Image Format', '*.tif',
          'BMP - Windows Bitmap', '*.bmp',
          'GIF - Graphics Interchange Format', '*.gif',
          'JPG - Joint Photographics Experts Group', '*.jpg',
          'All Files - *', '*'
        ],
      -directory => "c:\\program files",
      -title => 'Select a file';
    push @parms, -file => $lastfile  if $lastfile;
    @file = Win32::GUI::GetOpenFileName ( @parms );