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

Hello,

I am having an issue with my perl script running on windows. I am trying to automate some tests where it requires me to launch various applications in a workflow. However, once the perl script opens the application, the application sits in a focused view and I have to close it manually. Thereafter the perl script will continue once I do this.

I am using ActivePerl. I have tried to run the command that launches the app with backticks, system, exec and also created a .bat file to run it. And I am using the pstools program to periodically check when a process occurs (iexplore), which is how I know to kill the processes (app & internet explorer) thereafter with pskill. But again, the perl script is sitting behind the open windows and does nothing until I close them.

What am I missing here?

Thanks
  • Comment on Perl script stops running on windows after launching another app

Replies are listed 'Best First'.
Re: Perl script stops running on windows after launching another app
by GrandFather (Saint) on Jul 10, 2011 at 03:50 UTC

    This sort of thing actually requires a couple of important steps. You need to spawn a separate process to run the application, and you need to interact with the running application from your script to "push the buttons".

    Under Windows Win32::Process is probably the best way to run the application independently of your Perl script.

    Interacting with the running application from your Perl script is easiest with Win32::GuiTest. Using either of these modules requires a little work. The following is a very brief example to get you started:

    use strict; use warnings; use Win32; use Win32::Process; use Win32::GuiTest; my $fileName = 'delme.txt'; my $filePath = "$ENV{TEMP}\\$fileName"; my $app = "$ENV{SystemRoot}\\Notepad.exe"; my $cmd = "notepad $filePath"; my $ProcessObj; unlink $filePath; Win32::Process::Create ($ProcessObj, $app, $cmd, 0, NORMAL_PRIORITY_CL +ASS, ".") || die ErrorReport (); #FindWindowLike($window,$titleregex,$classregex,$childid,$maxlevel) my @windows = Win32::GuiTest::WaitWindow (qr"^Notepad"i, 3); die "Expected to find Notepad's new document dialog\n" if ! @windows; Win32::GuiTest::SetForegroundWindow ($windows[0]); Win32::GuiTest::SendKeys ("%y"); @windows = Win32::GuiTest::WaitWindow (qr"^$fileName"i, 3); die "Expected to find Notepad\n" if ! @windows; Win32::GuiTest::SetForegroundWindow ($windows[0]); Win32::GuiTest::SendKeys ("Hello World%fs%fx"); sub ErrorReport { print Win32::FormatMessage (Win32::GetLastError ()); }
    True laziness is hard work
Re: Perl script stops running on windows after launching another app
by Marshall (Canon) on Jul 10, 2011 at 03:08 UTC
    I think your question is how to launch a process in the background on Windows. The easy way is to use the "start" command. At the command prompt, type: help start. start someprog will return immediately to you and someprog will run as a completely detached thing. If you have a .bat file and have say a line with just someprog.exe in it, it will wait until that program finishes. The start command can be used in the .bat file.

    Notice that there are also options to "start" like the CPU priority, and others.

      Yes, exactly. thanks for sending this. I had figured out a temporary solution in the mean time. What I did was create a link to the executable file. If you right click it, go to properties, you can specify to minimize the app when opening. This seems to work fine, but your solution looks like the proper way to go about it. :)
Re: Perl script stops running on windows after launching another app
by Anonymous Monk on Jul 10, 2011 at 03:07 UTC

    various applications in a workflow.

    What is a workflow?

    What am I missing here?

    :) An answerable question

    Maybe you need Proc::Background, who knows