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

Hi Guys, I need to remove few printers that I have installed on lots of WinXP PCs. I tried few things, which all failed, and then I found a resource kit utility “cleanspl.exe” which works fine. Now I need to automate this utility by feeding it a list of host names to remove the printers from.

I started first by running the utility. Then I ran this code.

$|++; use strict; use Win32; use Win32::GuiTest qw(:ALL :SW); use vars qw /%data/; #$data{apps_list} = shift @ARGV; $data{host} = Win32::NodeName(); system (cls); my @windows = FindWindowLike(0, 'Clean Spooler'); for my $win (@windows) { print "Found window $win with title '", GetWindowText($win), "'\ +n"; my @children = GetChildWindows($win); for my $child (@children) { my $text = GetWindowText($child); next if ($text =~ /^\xff/); # you've found the icon next if ($text =~ /^OK|Cancel|Abort|Retry|Ignore|Yes|No$/); # + you've found a button print "Found child $child with text '$text'\n"; } }

And the out put I get is this

Found window 5440342 with title 'Clean Spooler' Found child 1245952 with text 'To restore the spooler to its original +state, enter the name of the computer on which to restore th e spooler, and click the Clean Spooler button. All print jobs, printe +rs, printer drivers, and spool files on the specified comput er will be deleted.' Found child 787128 with text 'Computer &Name:' Found child 5309426 with text '' Found child 787116 with text 'S&elect...' Found child 787136 with text '&Save Spooler Registry to File' Found child 1245870 with text '&File:' Found child 1835774 with text '' Found child 852698 with text '&Browse...' Found child 983782 with text '&Clean Spooler' Found child 656042 with text 'C&lose' Found child 918252 with text '' Found window 1245952 with title 'To restore the spooler to its origina +l state, enter the name of the computer on which to restore the spooler, and click the Clean Spooler button. All print jobs, prin +ters, printer drivers, and spool files on the specified comp uter will be deleted.' Found window 983782 with title '&Clean Spooler'

I need to insert the values (“\\\\”.host names) from a list into the filed ‘Computer &Name’ and click ‘Clean Spooler’ button. This followed by a sequence of yes, no ,no, no, no, no , yes. Then restarting the loop again with another host name…..easy!

Can someone please help me here and show me how I can insert a value in the ‘Computer &Name’ field to start of with?

Thanks

This is what I have done, but I don't think its good programming

use strict; use Win32::GuiTest qw( FindWindowLike SetForegroundWindow SendKeys PushChildButton GetChildWindows GetWindowText ); my @whnds = FindWindowLike( undef, "^Clean Spooler" ); if( !@whnds ) { die "Cannot find window with title/caption specified\n"; } else { printf( "Window handle of specified application is %x\n", $whnds[ 0 + ] ); SetForegroundWindow( $whnds[ 0 ] ); SendKeys( "\\\\SN02GET18a" ); SendKeys("{ENTER}"); SendKeys("{RIGHT}"); SendKeys("{ENTER}"); SendKeys("{RIGHT}"); SendKeys("{ENTER}"); SendKeys("{ENTER}"); }

Blackadder

janitored by ybiC: Balanced <readmore> tags around longish codeblock, as per Monastery convention

Replies are listed 'Best First'.
Re: Usin Win32::GuiTest to remove printers.
by Albannach (Monsignor) on Oct 14, 2004 at 18:26 UTC
    If you mean not good programming as in it looks clumsy and delicate, then I agree, however Windows apps are often built in such a way that this is the only approach. What I do in these cases is make it as robust as possible with what the original app designer gave you.

    For instance, to jump to a particular edit box, you can use SendKeys("{ALT}f");. This should put your cursor in the File: edit box, based on the child names you found (listing all the children is always a good starting point in my experience). Similarly you can press a button using the assigned alt-key sequence.

    Tabbing is also handy for moving between fields, especially if you can go directly to one field using an alt-key sequence, then count tabs from there. Beware that it is not uncommon for the tab sequences to be nonsensical (where the original designer rearranged his widgets on screen without re-sequencing the tabs), and sometimes edit boxes are not in the tab list at all. When that happens, I've resorted to locating the mouse pointer and then clicking it to move the cursor into the required box. Yes it's messy, but it works... so long as the target application is not upgraded!

    Good luck!

    --
    I'd like to be able to assign to an luser