Is there a nice clean generic way to detect that a print job has been started (command-P or the Print button for any app)? I’d like to run a script to turn the printer on when that happened.
Hello.
I’d look into Bonjour, that is my conclusion. I have thought some about it, hopefully someone has something better than my thoughts about a clean solution.
Somehow your printer needs to be in a stand by mode anyway, as AppleScript doesn’t do the dishes.
So, your printer needs to be in standby mode too. Then maybe you could configure it by CUPS, to wake up at some point, I haven’t looked into CUPS for ages, but that is where I’d start if I were you. So that you just have CUPS send a packet to initialize the printer, and you can forget all about it. It is either this, or something similiar by Bonjour, triggered up closer to the printer manager, which should be the clean transparanet solutions, which also will be triggered on demand, or install something that listens to to CUPS socket, far below.
I hope someone has something better.
Edit
The elegant thing, would be if you could make a device file (maybe just read only), that pointed to the CUPS port.
I don’t know if that is possible, but I have hopes that it is, but no time to look into it.
Then you could have a LaunchAgent with that file as a watch path. Your launch agent, could then send a packet to the printer to tell it to wake up with netcat if you have an ip-printer. From then onwards the launch-agent would get a timestamp every time it is “disturbed” and not do anything, before the diferrenece between the last time-stamp, and the current one, is greater than the period of yoru printers “go to sleep when idle time”.
Edit+
Maybe it is possible to make CUPS send something to initialize the printer? That would be the cleanest, And I am not sure what you put into “clean” :9, the launchAgent solution, would need some C-programming, at least it seems to me like that right now, if there isn’t some Unix command that can make a permanent devicefile to a socket or port. It wouldn’t surprise me at all if that exists, but however interesting it is, I haven’t time to look into it now.
P.S.
Should anybody feel the urge, or wants all the fun, they can look into man -s2 socketpairs, and man -s2 socket for starters.
After some fiddling around today, I’ve found to my surprise that, at least for my old hp Laserjet printer, if the USB cable is plugged in, the printer is recognized as “ready” even if it is powered down. For example
-- With the printer powered down but the USB cable connected:
set pStat to (paragraph 1 of (do shell script "/usr/bin/lpq"))
--> "Hewlett_Packard_hp_LaserJet_1012 is ready"
Similarly, if I print something and the printer is off, lpq returns: “Hewlett_Packard_hp_LaserJet_1012 is ready and printing” followed by data about what is being “printed”. The printer icon in the dock gets a yellow dot.
If I run a stay-open idle handler app like so with the printer off and a print job pending I get steady beeps and if there is no job pending nothing, but no indication that the printer is OFF. To find that out, I’ve got to go and look at the damned thing.
on idle
tell application "Printer Setup Utility"
set pn to name of current printer
if status of current printer is printing then beep 2
return 2
end tell
end idle
What I’m really hoping to do is discover whether the printer is ON when a print job is pending.
Hello.
This gets worse. The printer should have a USB driver then. So you should be able to connect to read it status via USB, but that is nothing you can do from AppleScript guarranteed! For starters, you can try to to see if you can find the Printer via ioreg, or the gui app that comes with Developer tools. ioregistry explorer. The best thing would be if you could establish that it was off, or on, by looking or grepping into the output of ioreg.
Then you would need some mojo, to turn it on, via USB if it is off line. Maybe very complicated, or maybe just something like having Print Manager or Bonjour find devices.
You could start a dummy job, and see if that job times out? and if the job has timed out, then the printer is off?
Don’t know how to do just that, but that would be starter for me, and I’d accept the extranous almost blank sheet of paper for that functionality!
You may have a look at [b]http://localhost:631/help[/b], if your HP Printer’s driver is configured/connected via CUPS. (Then looking into that queue would be a better alternative, if the Printer Manager tells you errantly that the print job is done. (You should be able to inspect CUPS’s queue via curl at least.
What I mean is to have the applet do it like this.
Send almost blank sheet to printer
Sleep for say a minute, or two
Wake up
Inspect Printer Manager's queue or CUPS's queue
If the job is pending, then the printer is off.
Edit
It may even be so, that your printer’s status can be queried by CUPS, so you end up with its online status via curl.
Edit+
Sometimes other’s problems seems more interesting than my own, I know there is a thing called USB prober, maybe that “thing” is usable, for finding out if the device in the other end is online? USB is a simple protocol, your problem, is that accessing USB through the Operating System isn’t that simple, but once you have done that, there must be a way to query for online status. (Or find that a query times out within like 10 millsecs.) Hopefully USB prober is a kind of tool that can help you with just that.
I’d also look into ASObjC-Runner, maybe there is something clever there for quering the printer status, or usb ports for all what I know.
Edit++
I guess you have the Developer tools installed, there somewhere, you should find the USB prober in the Applications Utilities folder, hopefully as I am still on Snow Leopard, you should also find usbtracer here:
/Developer/Applications/Utilities/USB Prober.app/Contents/Resources/usbtracer.
I must say, those tools works for me, or at least USB prober, as I haven’t had any need for trying usbtracer, but, this blogpost shows how to use usb tracer, hopefully that works for you as well, without installing debug drivers.
If the tools didn’t come with Developer tools originally, then you’ll have to have your Apple ID handy, as you need to log in to download from here.
Then you could watch the traffic back and forth, over the usb port, when the printer is answering, and not, that way, it should if not be easy, so possible to write something that can deduce whether your printer is online or not.
PS. Here is a link to a USB Document that may come in handy: Universal Serial Bus. (What you want to look for in the output form usbtracer eventually, is an ack signal from your printer, within a second or two after you have sent the print job to the printmanager. It is really a forensics job.