Automating Printing

Goal: When a new file is added to a folder, an AppleScript kicks off to print the document, wait until it’s done, then remove the file from the folder.

How: using Auotmator, Hazel, or KM

Help needed: The following AppleScript is giving me syntax errors. I’m not skilled in AppleScript (nor any other language) to know where to fix it. I did use ChaptGPT for some help but this is as far as I’ve gotten.

Any help is appreciated!

on adding folder items to theFolder after receiving theNewItems
    -- Get the list of new items added to the folder
    repeat with theNewItem in theNewItems
        -- Check if the new item is a document
        if document file type of theNewItem is true then
            -- Print the document
            tell application "Finder"
                print theNewItem
            end tell
            
            -- Wait for the document to finish printing
            repeat while (printer status of default print settings) ≠ idle
                delay 1
            end repeat
            
            -- Close the Print application
            tell application "Print"
                quit
            end tell
            
            -- Move the printed document to the Trash
            tell application "Finder"
                move theNewItem to trash
            end tell
        end if
    end repeat
end adding folder items to
1 Like

In the past, the approach I’ve typically taken is to convert files to PDF and then hand that off to be printed by CUPS. That can be done on the command line with something like lp $HOME/Documents/FileNameToPrint.pdf. This was convenient for me as I then had Hazel watch a Dropbox folder that I could populate from mobile, or even from my work PC, and know that the result was going to print out just fine - a big advantage of always passing in PDF files. In fact I was managing an amateur sports club and regularly used a more sophisticated print script to print “N” copies of things on my home printer when I wasn’t even home.

These days printing for me is pretty rare, but I still have my CUPS setup running should I need it.

Files on the Mac associate with apps, and it is typically those apps that handle the printing - but as you have noted with the inference in the AppleScript, Finder can sub in. However, rather than AppleScript it, there may be a way that is more approachable for you.

My first instinct was to use an Apple Shortcuts shortcut that took a file as input and then passed that to a print action (print accepting files as input). However, I tried this and let’s just say it didn’t handle not so uncommon printable file types graciously.

As a result, I reverted to an older technology, and one that may not be with us for that much longer, Automator.

I created a simple application in Automator, named it “Print File.app”, and placed it in my Applications folder. It consists of just a print action.

A few quick tests and it seemed to work okay with the following Hazel rule.

While the tests were far from exhaustive, perhaps this is something worth a closer look.

Should someone come to this when Automator is no more, then maybe the App will still work (it is just an app wrapper around an Automator workflow, so who knows what will and won’t work at that point), and you can download it here:

Hope that helps.

2 Likes

Thank you!

Your “older” method using CUPS is exactly what I’m looking to accomplish. I want devices (all Apple) regardless if they’re mobile or tethered to drop a file into a folder then have it print. Having it converted to a PDF is nice too. My goal is to keep the printer from being on the network, even though it has the ability to be added.

What steps would I need to take to make this as easy as possible? Your Automator+Hazel is pretty straight forward, but wondering your opinion on which you would prefer?

PS - loved your “recent” (I’m way behind in my listening) guest appearance on the podcast.

Keep in mind CUPS is not doing the conversion to PDF, the PDF is what CUPS is accepting - I was passing it PDFs as PDFs can specify what the printed file should look like.

For more info on CUPS conversions and supported types, check out the pages in the CUPS documentation referenced here:

Because it is not universal, that’s why I provided the other option with the Automator action as I think that should be universal, or at least as universal as your installled apps support.

Simplest is probably just use the run shell script action and create your shell script with a single command, lp "$1". Hazel should then pass the path to the script and the script should print the PDF (or other supported file format) it has been passed.

:laughing:thanks, and over two years is certainly quite the back log!

Lots of good listening ahead for you then.

No judgement LOL! Some how it stopped downloading and I’m playing 2.5x catch-up.

One month of having this implemented has been great. Other than needing to wake up the printer it’s completely automated. Much appreciated the help thinking through this one.

1 Like

If your printer is called (for example) printer1, try first using the command cupsenable printer1 to wake your printer.

1 Like