wago15 Posted April 7, 2017 Share Posted April 7, 2017 (edited) Hi, The purpose of this script is to install network printers automatically. But there is an issue when the installation is almost finished. Here is the script: expandcollapse popup; this will ask the 4 digits of the printer $printer = InputBox ( "Printer", "Please give the last 4 digits of the printer: " ) If @error Then Exit ; this will start the printer wizard and look for the printer Send ( "#r" ) Sleep ( 500 ) Send ( "cmd" & "{ENTER}" ) Sleep ( 500 ) Send ( "rundll32 printui.dll,PrintUIEntry /il" & "{ENTER}" ) Sleep ( 500 ) WinClose ( "C:\Windows\system32\cmd.exe" ) WinActivate ( "Add Printer" ) Sleep ( 500 ) Send ( "!w" ) Sleep ( 500 ) Send ( "!r" ) Sleep ( 500 ) Send ( "!f" ) Sleep ( 500 ) Send ( "!n" ) Sleep ( 500 ) WinActivate ( "Find Printers" ) Send ( "!p" ) Sleep ( 500 ) Send ( "!a" ) Sleep ( 500 ) Send ( "*" & $printer ) Sleep ( 500 ) Send ( "!i" ) Sleep ( 2000 ) ; this will check if the printer exists in the database, if it doesn't, it will ask for the digits again $null = "No items match the current search" While ControlGetText ( "Find Printers", "No items match the current search", "ActiveDsQueryBanner1" ) = $null $printer = InputBox ( "Printer", "Please give the correct 4 digits of the printer: " ) If @error Then Exit WinActivate ( "Find Printers" ) Sleep ( 500 ) Send ( "!a" ) Sleep ( 500 ) Send ( "*" & $printer ) Sleep ( 500 ) Send ( "!i" ) Sleep ( 2000 ) WEnd ; user needs to select the printer now MsgBox ( 0, "Select printer", "Please select the printer from the list." ) ; this checks if there are drivers and it will install them If WinWaitActive ( "Printers", "&Install driver" ) Then Sleep ( 500 ) Send ( "!i" ) Sleep ( 500 ) EndIf WinWaitActive ( "Add Printer", "This printer has been installed" ) Sleep ( 500 ) Send ( "!n" ) The problem is when I have selected the printer and it goes to the next screen. When it needs to install drivers, it does. But when the printer is installed and I need to press ALT-N, it doesn't send the ALT-N while the window is active. Can someone please help. Thanks in advance, Edited April 13, 2017 by Jos Link to comment Share on other sites More sharing options...
MattHiggs Posted April 7, 2017 Share Posted April 7, 2017 Hello sir. So what very well could be the case is that the window is somehow losing focus during this procedure you are trying to perform. It can be a tricky business automating processes when you using that kind of methodology, as the window has to remain in focus in order for it to work. To get around it, what I do is I have each "step" in my series of events take place inside a "do...While" loop, so that the script will repeat the step until it has confirmed the Window for the next step is present. Here is an example of what I mean: Run ( "rundll32 printui.dll,PrintUIEntry /il" ) Do $win1 = WinWait ( "Add Printer" ) WinActivate ( $win1 ) WinWaitActive ( $win1 ) #cs This is where you input code that needs to be performed in this step or on this window in question. #ce Sleep ( 500 ) While Not WinExists ( "Window in Next step", "This will be the Window which will appear in the next step, and you would include pretty much the exact same schema listed above over and over again." ) Let me know if that makes sense. Link to comment Share on other sites More sharing options...
MattHiggs Posted April 7, 2017 Share Posted April 7, 2017 Sorry. For some reason I can't seem to edit my previous response. I apologize, but the "While" in the "do...while" loop should be an "Until". SO it would be a "Do...until" loop. I have been working too much in powershell these days. Link to comment Share on other sites More sharing options...
MattHiggs Posted April 7, 2017 Share Posted April 7, 2017 Also get rid of the "not" beside the until, so code would look like: $printer = InputBox ( "Printer", "Please give the last 4 digits of the printer: " ) If @error Then Exit ; this will start the printer wizard and look for the printer Run ( "rundll32 printui.dll,PrintUIEntry /il" ) Do $win1 = WinWait ( "Add Printer" ) WinActivate ( $win1 ) WinWaitActive ( $win1 ) #cs This is where you input code that needs to be performed in this step or on this window in question. #ce Sleep ( 500 ) Until WinExists ( "Window in Next step", "This will be the Window which will appear in the next step, and you would include pretty much the exact same schema listed above over and over again." ) Link to comment Share on other sites More sharing options...
wago15 Posted April 10, 2017 Author Share Posted April 10, 2017 thx for your reply whiggs2, I'm gonna try this today Link to comment Share on other sites More sharing options...
wago15 Posted April 12, 2017 Author Share Posted April 12, 2017 (edited) still a no go I'm afraid, the script works when drivers doesn't need to be installed, so I made that part as comment. So the problem is, I think, with the install driver part. But I have no clue how to fix this ... expandcollapse popup#RequireAdmin ; this will ask the 4 digits of the printer $printer = InputBox ( "Printer", "Please give the last 4 digits of the printer: " ) If @error Then Exit ; this will start the printer wizard and look for the printer Run ( "rundll32 printui.dll,PrintUIEntry /il" ) Sleep ( 500 ) WinActivate ( "Add Printer" ) Sleep ( 500 ) Send ( "!w" ) Sleep ( 500 ) Send ( "!r" ) Sleep ( 500 ) Send ( "!f" ) Sleep ( 500 ) Send ( "!n" ) Sleep ( 500 ) WinActivate ( "Find Printers" ) Send ( "!p" ) Sleep ( 500 ) Send ( "!a" ) Sleep ( 500 ) Send ( "*" & $printer ) Sleep ( 500 ) Send ( "!i" ) Send ( "{ALTUP}" ) Sleep ( 2000 ) ; this will check if the printer exists in the database, if it doesn't, it will ask for the digits again $null = "No items match the current search" While ControlGetText ( "Find Printers", "No items match the current search", "ActiveDsQueryBanner1" ) = $null $printer = InputBox ( "Printer", "Please give the correct 4 digits of the printer: " ) If @error Then Exit WinActivate ( "Find Printers" ) Sleep ( 500 ) Send ( "!a" ) Sleep ( 500 ) Send ( "*" & $printer ) Sleep ( 500 ) Send ( "!i" ) Send ( "{ALTUP}" ) Sleep ( 2000 ) WEnd ; user needs to select the printer now MsgBox ( 0, "Select printer", "Please select the printer from the list." ) ; this will check if drivers needs to be installed #comments-start If WinWaitActive ( "[TITLE:Printers;CLASS:#32770]", "&Install driver" ) Then Sleep ( 500 ) Send ("!i") Sleep ( 500 ) Send ( "{ALTUP}" ) EndIf #comments-end ; this doesn't work anymore :'( WinWaitActive ( "Add Printer", "&Printer name:" ) Sleep ( 500 ) Send ("!n") Send ( "{ALTUP}" ) Sleep ( 500 ) ; this will check if the default printer window comes up If WinActive ( "Add Printer", "Set as the &default printer" ) Then Local $default = MsgBox ( 4, "Default printer", "Do you want to set this as the default printer?" ) If $default = 6 Then ControlCommand ( "Add Printer", "You can choose a default printe", "Button14", "Check" ) Sleep ( 500 ) Else ControlCommand ( "Add Printer", "You can choose a default printe", "Button14", "UnCheck" ) Sleep ( 500 ) EndIf EndIf Sleep ( 500 ) Send ( "!f" ) Send ( "{ALTUP}" ) I also inserted some lines for the ALT key, since it sometimes get stuck. My apologies, I only started to use AutoIT for 2 weeks. Edited April 12, 2017 by wago15 Link to comment Share on other sites More sharing options...
wago15 Posted April 12, 2017 Author Share Posted April 12, 2017 It worked for a few times on my laptop, then I copied it to another laptop and it stopped at the install driver menu. I re-tried it again @ my laptop, same issue again ... I changed this: If WinExists ( "[TITLE:Printers;CLASS:#32770]", "&Install driver" ) = 1 Then Sleep ( 500 ) Send ("!i") Sleep ( 500 ) Send ( "{ALTUP}" ) EndIf I read a post on this forum about the return value, so I tried it and like I said, it only worked a few times Link to comment Share on other sites More sharing options...
wago15 Posted April 13, 2017 Author Share Posted April 13, 2017 Hi, I somehow solved this. Here is the code: expandcollapse popup#RequireAdmin ; this will ask the 4 digits of the printer $printer = InputBox("Printer", "Please give the last 4 digits of the printer: ") If @error Then Exit ; this will start the printer wizard and look for the printer Run("rundll32 printui.dll,PrintUIEntry /il") Sleep(500) WinActivate("Add Printer") Sleep(500) Send("!w") Sleep(500) Send("!r") Sleep(500) Send("!f") Sleep(500) Send("!n") Sleep(500) WinActivate("Find Printers") Send("!p") Sleep(500) Send("!a") Sleep(500) Send("*" & $printer) Sleep(500) Send("!i") Send("{ALTUP}") Sleep(2000) ; this will check if the printer exists in the database, if it doesn't, it will ask for the digits again $null = "No items match the current search" While ControlGetText("Find Printers", "No items match the current search", "ActiveDsQueryBanner1") = $null $printer = InputBox("Printer", "Please give the correct 4 digits of the printer: ") If @error Then Exit WinActivate("Find Printers") Sleep(500) Send("!a") Sleep(500) Send("*" & $printer) Sleep(500) Send("!i") Send("{ALTUP}") Sleep(2000) WEnd ; user needs to select the printer now MsgBox(0, "Select printer", "Please select the printer from the list.") ; this will check if drivers needs to be installed $driver = "&Install driver" Sleep(5000) If WinActive("Printers", $driver) Then Sleep(500) Send("!i") Sleep(500) Send("{ALTUP}") EndIf ; this doesn't work anymore :'( WinWaitActive("Add Printer", "installed") Sleep(500) Send("!n") Send("{ALTUP}") Sleep(500) ; this will check if the default printer window comes up If WinActive("Add Printer", "Set as the &default printer") Then Local $default = MsgBox(4, "Default printer", "Do you want to set this as the default printer?") If $default = 6 Then ControlCommand("Add Printer", "You can choose a default printe", "Button14", "Check") Sleep(500) Else ControlCommand("Add Printer", "You can choose a default printe", "Button14", "UnCheck") Sleep(500) EndIf EndIf Sleep(500) Send("!f") Send("{ALTUP}") I don't know how to change the title to SOLVED, so my apologies. Link to comment Share on other sites More sharing options...
Developers Jos Posted April 13, 2017 Developers Share Posted April 13, 2017 30 minutes ago, wago15 said: I don't know how to change the title to SOLVED, so my apologies. Simply edit the first post in the topic... it has the title field. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
wago15 Posted April 14, 2017 Author Share Posted April 14, 2017 16 hours ago, Jos said: Simply edit the first post in the topic... it has the title field. Jos Ow, thanks Jos Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now