Jump to content

Detect whether Windows is printing?


Recommended Posts

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 by Edward Mendelson
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...