Macro Fails When Unattended

iMac Pro, Catalina.

I have a fairly simple macro which opens the "printers and scanners" prefs pane, clicks some buttons to initiate a nozzle check and closes the prefs pane. I want it to run once a day, to keep, or at least help keep, the nozzles from clogging (it's an Epson SC-P7500).

It works just fine when I activate it using a keystroke, and on timer if I'm using the iMac when the timer triggers. But if the iMac is unattended, it fails with the attached message. The iMac is set to dim the display after 10 minutes of inactivity, but it never sleeps.

image

I suspect the reason is that although the iMac never sleeps, it does require a password when the displays reactivate. Is there a way round that? For security reasons, I'm reluctant not to require the password, although I wouldn't mind including it in the macro so it could be typed into the login window by KM, if that would help.

The macro looks like this:

Any suggestions gratefully received.

Jeremy

A quick search of this forum threw up Screen Wake Password Entry Macro Or Avoid Screen Lock When Connected To Specific Wifi Macro Help which would imply entering the password in this state can't be done.

My recommendation is to do a web search for something like ‘Epson test page print’ and see if you can find either the same one that it prints or something which will serve the same purpose.

Once you find it, save it as a PDF (use the “Print to PDF” feature in macOS if needed) and save it as a PDF to something like ~/Documents/TestPage.pdf.

Create a new Keyboard Maestro macro and add the action “Open a File, Folder or Application” to it.

It will look like this:

Note the area I have circled in blue and the red rectangle.

Click on the icon in the blue circle and select your ~/Documents/TestPage.pdf file. It should now look like this:

Now click where it says “Default Application” and it will show you a list of your running applications. But you want to ignore all of those and go down to “Other…” as shown here:

Keyboard Maestro will open a window to your “Applications” folder, which is usually what we would want. But not in this case.

Press Command+Shift+G (⌘⇧G) which will bring up “Go to Folder…” and enter ~/Library/Printers/

It should look like this:

Click “Go” and now you should see some “apps” for all of your printers. Select the one for your “Epson SC-P7500”. I don’t have that exact same printer, but here’s how it looks if I select my Brother HL 2270DW:

Now, when that macro runs, it should print that PDF that you told it to open. When you run it, you should see the app open in the dock.

If the app stays in the Dock…

For some reason, when we do this the printer app does not quit itself, and ends up hanging around in the dock, which is not what we want.

If that happens to you, add a “Quit a Specific Application” action, and in the list of running apps, look for PrinterProxy:

Once you select PrinterProxy Keyboard Maestro should show you the actual name of the app that you were expecting:

Now you have a Keyboard Maestro macro which should printer your test page and quit the printer application, and it should work even if your Mac screen is locked.

Voilà

End of lesson…


“But TJ! What if I want a geekier solution which is going to be slightly harder to set up and involves a shell script!?!”

Well, ok, since you asked…

(Note: the following is another option that you could use instead of what I described above. You do not need both.)

There is a Unix command lpr which you could use.

If you only have one printer, or if the Epson is your default, it’s extremely easy. Just use this:

lpr "$HOME/Documents/TestPage.pdf"

in a Keyboard Maestro macro “Execute a Shell Script” and BAM! you should be done.

It gets trickier if you have more than one printer because then you need to use the -P flag and the CUPS name of the printer, which (for me) is Brother_HL_2270DW_series so I would use this:

lpr -P "Brother_HL_2270DW_series" \
"$HOME/Documents/TestPage.pdf"

To find the CUPS name, go to System Preferences » Printers & Scanners then select your printer, and finally click the ‘Options & Supplies’ button:

Once there, look for the “Device Name” shown here:

Pro Tip: If you double-click on the Device Name you can “copy” it to the clipboard and then paste it into your Keyboard Maestro macro.

Questions?

2 Likes

Tadeusz, a quick search does indeed reveal that thread, which is four years old.

TJ, thanks. That’s helpful.

Jeremy

Jeremy Roussak
Photography: jbr.is

Four years old or not, KM sill can’t enter the password into the Lock Screen!

Thanks very much, TJ. The advantage of your “geekier” version is that it allows an option to print in landscape, which wastes less paper.

Jeremy

Jeremy Roussak
Photography: jbr.is

I found that opening a preference pane had issues getting the timing right. here's my best guess at an applescript that seems to be working. Perhaps you can adapt it for your needs:

I am not a developer, but I would 100% bet that macOS prevents any automation tool from filling in a “Secure Field” like that, as a security measure at the OS level that Keyboard Maestro cannot do anything about.

AH! I didn't realize that would help, or I would have mentioned it specifically, but yes lpr can control almost all of your printer's features.

(I was going to say “all” but then I was sure someone would tell me about an edge case that does not work, so… “almost all” will have to suffice.)

Yes, exactly.

The issue with printing a test page, of course, is that it won't run ink through both PK and MK channels. The driver is capable of telling the printer to print a nozzle check; it would be great if I could find out what the command it sends looks like, as I'd then be able to do it myself. This is getting rather beyond KM, I realise, but is there a way of snooping on its communication with the printer, to find out what they're saying to each other?

@ikenassi, thanks but that's not the problem. KM runs it beautifully when the Mac is unlocked; the issue, which would stop the script from working as well, is what happens when it's locked.

That's well beyond my knowledge set, unfortunately.

Done it! Following a suggestion, I snooped around the printer spool files after printing a test page, and found one which contained

#CUPS-COMMAND
PrintSelfTestPage

If I send that to the printer using lpr, it works very nicely. So thanks for the tips. Problem solved.

Jeremy

1 Like

Excellent! I've learned my new thing for the day. Will have to remember that one.

KM cannot, but an AppleScript can. I use the attached one frequently and it works like a charm. Simply tell KM to execute an AppleScript and use the Execute Text Script option to insert the text of the attached script

NOTE: This can be a security risk. Having your password exposed in an AppleScript may not be advisable depending your personal situation. Shared computers, residences with roommates, work environments where computers might be left unlocked, etc., will all EXPOSE YOUR MASTER PASSWORD and is not recommended.

Other than that caveat I have found no problems with using this script to unlock a password-protected screen before a macro runs.

That’s interesting. I can see how that AppleScript works, but if the screen is locked how can you get KM to run it?

Ah, good question - I should have specified this and apologize for not doing so.

Use the "Wake Screen" Action in KM to make the password field appear. I inserted a pause for a few seconds and then run the script.

Thanks. It's an interesting approach but as I keep large amounts of client-confidential data on my iMac, and as I could be fined up to £5,000 for each data breach (which could amount to quite a lot of money for thousands of breaches), unlocking the screen in my absence is not something I'd wish even to consider.

As I've indicated, now that I've found out exactly what the driver sends to the printer to get it to print a test page, the problem is solved much more neatly and safely.

Jeremy