How to use KBM to print Hazel selected files

Hello,
I use Hazel to select PDF files in my download folder based on name content, matches, etc, which is very easy to do.
I would then like to print those files directly without going to the trouble of opening them in an app and giving the print command.
To my surprise, Hazel does not have a print file action. Is there any way I could use a KBM macro and insert it into Hazel as a rule.
Thank you !

1 Like

Hello Ronald,

You can use the Run ApplesScript and embed your script. Here is an example that you will need to modify to suit your needs but you should get the idea. I have a Text file called test.txt and within the file it has the words "Please print me". Once it hits the Downloads folder, it is moved to the Desktop and printed. Hopefully, this will help you create something to fit your needs.

3 Likes

That's good to know. I didn't know how to do that with AppleScript.

I always like to know more than one way of doing something, so allow me to share this:

If you are dealing with PDFs, you can also use lpr to print files in a shell script:

The simplest option is something like this:

/usr/bin/lpr "$1"

That assumes that the default printer is the one you want.

If you have a printer capable of printing two-sided copies, you can use this:

/usr/bin/lpr -o sides=two-sided-long-edge "$1"

There are lots of other features you can use with lpr however I think it only works with PDFs because they're already "formatted" in a way that the printer can understand…but I might be wrong. (I suspect the AppleScript method works with more kinds of files, but I might be wrong about that, too.)

2 Likes

thank you for your post.
I don't understand your script.
What is "$1" ? Doesn't Hazel request that one use theFile variable name ?

thank you for your help.
I wrote the following Hazel rule, using your appleScript.
The first time I tested the rule, the file printed.
I then created more files in Download with test print. None were printed but all were moved to my Download dump temp folder.
thank you again

Ronald,

I have a laser printer and mine jetted through 10 test pages quickly but they were one line tests. The AppleScript gurus here may be able to help or even asking the forum members on the Noodlesoft Forum.

You did what I would have done by moving the script earlier before the move. I just slapped it together to get it to you quickly.

What happens if you add a delay?

Try:

tell application "Finder"
   activate
   delay 5
   print document file theFile
end tell

or this:

tell application "Finder"
   activate
   print document file theFile
   delay 5
end tell
2 Likes

thank you for suggesting a solution.
I added the delay as you suggested. Funny thing: I looked for a pause action in Hazel and could not find one. The idea of adding it to the apple script s great.
Sometimes it works, other times I get this strange error message, strange because I have never seen it before when I print. In this case, the file was a PDF.
By the way, I find Noodlesoft forum support really poor.
thanks again very much

image

Maybe change your date format to 07-06-2019 instead of the 07.06.2019 and see how that works. Sometimes it's the smallest change. Maybe the name format is confusing the system in thinking it's an extension. I'm just guessing at this point. I have nothing as my tests work.

1 Like

My apologies, I should have been more clear.

Hazel uses "theFile" when using AppleScript, but when you use a shell script, it uses "$1" to stand for the filename.

That error message indicates that macOS considers the file to be "quarantined". Unfortunately the system has been a little buggy for the past few versions of macOS, because the error message that is there should only appear for unsigned applications not files yet here we are.

Try a "Run Shell Script" action with these lines:

#!/bin/zsh -f

/usr/bin/lpr "$1"

in place of the AppleScript action, and see if that works more reliably.

As bonus, you won't have Finder popping up and potentially stealing focus from whatever you were doing, as the shell script should be able to run completely in the background.

3 Likes

The shell script works perfectly, and you are right: I avoid having the Finder window popup.
Thank you very much for your time and your excellent solution. I am very grateful.

Thanks very much for your suggestion. Nothing works including adding a delay, and changing the time to test print.pdf
Interestingly, @tjluoma solved my problem with a shell script. Worked with this file and all files I had tried previously, so I will stick to his solution.
Once again, thanks very much for your time and help.

image

1 Like

I’m hurt that you didn’t use my solution over the infamous TJLuoma solution! :joy:. Just joking!!!
I’m glad you found a solution.

One of the main things I’ve learned, is that this forum will step up and provide help if they can. It is contagious. I’m far from the sharpest knife in here but I try to assist because the members here pay attention. They know who are the givers and the takers. Involvement makes it easier to ask for assistance. I’m so humbled to be in this community.

Take care.

2 Likes

thanks again very much

Hey @ronald,

That's a very surprising oversight.

If you have time then please pose a feature request to the developer.

-Chris

1 Like

I wrote to hazel support today and will give you a follow up
Thank you for the suggestion

Hello CC,

So I wrote to the developer who answered:

In Automator, you can create a workflow with “Print Finder Items” by itself. You can then trigger that workflow from Hazel using the “Run Automator workflow” action.
Alternatively, you can run a script. Here’s a current thread on the topic: https://www.noodlesoft.com/forums/viewtopic.php?f=4&t=11796

I solved (and tested) the problem by identifying the file with a Hazel rule, and print using the following script:
#!/bin/zsh -f
/usr/bin/lpr "$1"

1 Like

For anyone who might be curious… I'm @tjluoma here but @luomat on the Hazel forums…

So if you were surprised to see the same suggestion pop up on both places, now you know why :wink:

1 Like

thank you, and I apologize for not making it clear enough in my post above that I used YOUR solution to solve the problem.

1 Like

Oh, no worries. I was mostly amused because the same issue came up and crossed-over at roughly the same time on two different forums :slight_smile:

I'm always just happy when something new works for someone.

1 Like