emendelson Posted March 12, 2011 Share Posted March 12, 2011 (edited) I'm working on a script in which I want to close a program (the open-source document viewer Evince) after it finishes printing. Is there any way to detect whether Windows is currently printing, so that I can wait to close the program until printing is complete? (If I close Evince too early, it seems to stop printing the current file.) I won't know which printer is being used. I've been experimenting with the following code, but it never reports whether that printing is actually occurring, even during a very long print job: $intPrinters = 1 $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer") While 1 For $objItem In $colItems If $objItem.PrinterStatus = 4 Then MsgBox(0, "", "Printing") EndIf $intPrinters = $intPrinters + 1 Next Sleep(100) WEnd I adapted this from the posts in this thread: But probably I've misunderstood the original code. The purpose of this test is simply to make sure that printing is NOT occurring, so that I can close the document window safely. I don't need to know that printing DID occur - only that it is not occurring at the present moment. Many thanks for any help. Edited March 13, 2011 by Edward Mendelson Link to comment Share on other sites More sharing options...
emendelson Posted March 13, 2011 Author Share Posted March 13, 2011 I figured out what I was doing wrong, and this code seems to work as a function to detect whether Windows is printing. I hope someone else might also find it useful: Func TestPrinting() Dim $intPrinters, $objWMIService, $objItem, $colItems, $printing While 1 $printing = 0 $intPrinters = 1 $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Printer") For $objItem In $colItems If $objItem.PrinterStatus = 4 Then $printing = 1 ; MsgBox(0, "", $objItem.Name & " Printing") EndIf $intPrinters = $intPrinters + 1 Next If $printing = 0 Then ExitLoop EndIf Sleep(500) WEnd EndFunc ;==>TestPrinting I don't have any good reason to set the Sleep interval at 500; it's just a wild guess. If there's any reason to make it larger or smaller, I'd be grateful to know what it ought to be! 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