techcode has asked for the wisdom of the Perl Monks concerning the following question:

The guy I'm somewhat regularly working for on web development projects, now wants something different...

He wants and email > print desktop application.

Getting emails, reading it, parsing attachments and other things related to it (filtering for instance) isn't going to be a problem.

Things I'm worried about :

Replies are listed 'Best First'.
Re: Printing various files from Perl and few other things.
by Adrade (Pilgrim) on Jul 09, 2005 at 23:12 UTC
    On creating an exe, check out Perl executables? or perl to exe - they go through a couple of methods you can use.

    For the printing, I would check out Win32::CtrlGUI, for instance:
    use Win32::CtrlGUI; # Open file - do other stuff my $window = Win32::CtrlGUI::wait_for_window(qr/$filename/); $window->send_keys("^p{2}{ENTER}!fx");
    You'll have to play around with it - I have no idea if this is right or if it works... but I hope it sets you on the right track.

    Best,
      -Adam

    Update: I played around with this a little - and actually think this is quite a cool set of utilities... Now this isn't perfect, but I have some working code for you - it opens a file, sends command-p to print, hits enter in the new window - then exits the app, by hitting alt-f, then 'x' - again, not perfect, but quite enjoyed playing around with this mod...

    use Win32::CtrlGUI; $file = 'Untitled-1.pdf'; system("start $file"); my $window = Win32::CtrlGUI::wait_for_window(qr/$file/); $window->send_keys("^p"); my $window2 = Win32::CtrlGUI::wait_for_window(qr/Print/); $window2->send_keys("{ENTER}"); $window->send_keys("!f{1}x");

    Update 2: Also, if you have any trouble finding Win32::Setupsup, as I did, just (ActiveState): ppm install http://theoryx5.uwinnipeg.ca/ppms/Win32-Setupsup.ppd

    --
    By a scallop's forelocks!

Re: Printing various files from Perl and few other things.
by davidrw (Prior) on Jul 10, 2005 at 01:10 UTC
    I second the PAR suggestion and related links above.. as for printing, you can probably use Win32::OLE for a lot (IE, MS Word/Excel/PPT, etc) of the common file types. It can run the target application behind the scenes (no visible window), too..

    (hmm--spur of the moment though: maybe you could just use Win32::OLE to control outlook to do all the printing for you)
Re: Printing various files from Perl and few other things.
by jonadab (Parson) on Jul 10, 2005 at 02:04 UTC
    How to print any file-type? The app he referenced to seems to open application that is set as default for that file-type, calls print, and then close the app. Anyone knows how to do that?

    In the general case, this is not possible. However, you may be able to handle enough of the common file types to make people happy.

    Here are just a couple of the obstacles you will run into:

    • For some file types, nothing knows how to open the file except the app that created it, which may not be installed. A good example of this is Publisher; nothing else can read its files, and it is usually not installed (even when Office is installed, Publisher frequently is not), but it is common enough that people *will* try to send that format as an email attachment. For that matter, somebody will try to send some hyper-obscure file format that is proprietary to some shareware app nobody ever heard of. In rare cases, the app needed to open the file may not even be available for Windows (e.g., some popular Mac software).
    • If the content-type and the extension do not agree, it is not always clear which is correct. A human can use trial and error, because if he tries to open it in the wrong app it'll "look like garbage", and he won't hit print. You may need to employ magic-number checking in these cases. Joy.
    • Some things are not, technically speaking, sensible to print, but people will want to anyway. Somebody will get sent a MS-TNEF attachment, a screen saver, or an EXE, and want to print it. This is especially true of TNEF, which frequently does not contain what the message claims it contains. People freqently try to attach digital photographs, believe they have done so, and say in the message what it's a picture of, when in fact the attachment is 20 bytes and contains nothing of the kind.

    If it were me, I would ask the guy for an exhaustive list of all the file formats he wants supported, and I'd evaluate that list *before* agreeing to do it. It's easy to handle BMP, PNG, JPEG, and GIF, and it's not hard to pass HTML off to IE, and Office formats off to Office if it's installed. You can look inside ZIP files and see if there are any filetypes you know how to print in there. Plain text of course is no problem, and attached email messages you can handle easily enough (recursively, probably). Find out if that's enough to make him happy.


    "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
Re: Printing various files from Perl and few other things.
by pg (Canon) on Jul 09, 2005 at 19:40 UTC

    One suggestion to the first question as how to distribute.

    Web application is not the only way to share without spreaded installation. Take a look at Citrix, or similar products. Citrix allows you to install your application at one central location and being executed remotely without you coding any particular logic.

Re: Printing various files from Perl and few other things.
by Thelonius (Priest) on Jul 10, 2005 at 19:46 UTC
    For many file types, there is a shell extension installed so that you can print using the (right-click) context menu from Windows Explorer. You can use these shell extensensions from Perl. (This uses Win32::API, which I think comes with Active Perl 5.8 and above.)
    #!perl -w use Win32 qw(SW_SHOWNORMAL); use Win32::API; use strict; my $shellexec = Win32::API->new('shell32', 'ShellExecute', 'NPPPPI', ' +N') or die "cannot import ShellExecute: $!\n"; for my $filename ( "small.pdf", "realfilebutunknownextension.qqt", "notanexistingfile.doc", "knownextbutnoprint.wpl") { my $result = $shellexec->Call(0, "print", $filename, "", ".", SW_SHOWNORMAL); my $msg = ($result>32)?"Success": Win32::FormatMessage($result); printf "ShellExecute(, \"print\", \"%s\", ...) => %s\n", $filename, +$msg; }
    This code checks to see whether the "print" action was started, but there's no way to tell whether it successfully finished.

    You can see whether a file has an associated "print" action in Windows Explorer. Click on "Tools|Folder Options", then the "File Types" tab, and, for any extension, click "Advanced." Note that these usually just use command-line options to the associated windows application. For example, my PDF print command is:

    "C:\Program Files\Adobe\Acrobat 4.0\Acrobat\Acrobat.exe" /p /h "%1"

    There are also inexpensive file viewers that you can get download, either as standalone applications or as ActiveX controls that you can control using Win32::OLE.

    See this Google search, e.g.

      OK I now have several ways to try - I should be able to make at least one of them work.

      But what's bugging me now. Is there any way to control which printer should do the job? This guy has several (network) printers and while they will (probably) all be the same type. They are in different places.

      I believe that ways that you folks suggested would print by using default printer and it's default settings.

      Anyway, the command prompt way seems easiest :)