Printing a File

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Printing a File Sat Jun 15, 2019 9:55 am • by RobbieSnr
I want to take an attachment from an email and move it into the Desktop and then check that it has some particular words. If it has Hazel will rename the file and move it into a particular folder. I've worked out how to do this. However I'd also like Hazel to be able to print the file, selecting a particular set up on my printer. I do have a Keyboard Maestro shortcut that prints an opened file in the way that I want. Is there any way of incorporating my KM shortcut in Hazel's actions?
RobbieSnr
 
Posts: 6
Joined: Sat Jun 15, 2019 9:45 am

Re: Printing a File Mon Jun 17, 2019 10:23 am • by Mr_Noodle
There is no built-in print action but you can use Automator to do the printing.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Printing a File Mon Jun 17, 2019 10:45 pm • by luomat
If the files are PDFs, then you can use the `lpr` command, which will allow you to use various options as well. That's probably your best plan.

If they are in other formats, you'd need to look at seeing if you can convert it to PDF and then send that to `lpr`.

The only other way I know how to do it. Automator will let you send documents to a specific printer, but won't let you specify options for them.
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Re: Printing a File Tue Jun 18, 2019 11:11 am • by RobbieSnr
luomat wrote:If the files are PDFs, then you can use the `lpr` command, which will allow you to use various options as well. That's probably your best plan.

My document is a PDF one. I don't recognise the "lpr" command - where is it available and how do I call it?
RobbieSnr
 
Posts: 6
Joined: Sat Jun 15, 2019 9:45 am

Re: Printing a File Tue Jun 18, 2019 2:51 pm • by luomat
Aha! Well, I'm glad that we should be able to find a solution for you.

`lpr` is a command-line tool, which means that the easiest way to find it is to launch the Terminal.app (located in the /Applications/Utilities/ folder) and type `man lpr` (without the backticks).

Let's say that you had a file called "My Resume.pdf" on your Desktop and you wanted to print it.

You could go into Terminal.app and type:

lpr ~/Desktop/"My Resume.pdf"

and it would print it on the default printer.

If you have more than one printer, or if you want to specify different options than the default, that can be done, although it does get tricker.

Here is how you find the "Printer Name" that `lpr` needs. First go to System Preferences, then "Printers and Scanners" and select your printer.

Image

Then click on the "Options & Supplies" button (shown above). That will open the window below:

Image

The "Device Name" is what you are looking for. Note that lack of spaces (it uses '_' instead).

If I wanted to specify that printer, I would need to add `-P` (not `-p`) and then the "Device Name" so it would look like this:

lpr -P 'Kyocera_TASKalfa_2552ci' ~/Desktop/"My Resume.pdf"

I can see what other options I can use with `lpr` by using `lpoptions`.

This time I need to use a lowercase "L" and a lowercase "p" (unlike `lpr` which wanted an uppercase P) like this:

lpoptions -l -p 'Kyocera_TASKalfa_2552ci'

Those options tend to be different for different models of printers, so you'll have to figure out what the option is called if you want to specify things like black & white vs color or two-sided or not.

If you have questions, feel free to post a followup here. I'm tracking this thread so I should see it and will do my best to respond in a day or so (possibly sooner).
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Re: Printing a File Tue Jun 18, 2019 6:23 pm • by RobbieSnr
Many thanks for that. So I could use run an embedded script from Hazel on the lines you suggest but how would I pass the file name from Hazel?
RobbieSnr
 
Posts: 6
Joined: Sat Jun 15, 2019 9:45 am

Re: Printing a File Tue Jun 18, 2019 8:00 pm • by luomat
Yes, you would use a shell script. The filename gets replaced with "$1" so instead of

lpr ~/Desktop/My\ Resume.pdf

you could use

lpr "$1"

(basically put "$1" -- including the quotation marks -- at the end of the line with `lpr` and any arguments that you add.)
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Re: Printing a File Wed Jun 19, 2019 6:04 am • by RobbieSnr
That's great, many thanks. I'm new to Hazel and wasn't sure how it passed items to scripts. I was intending to get Hazel to open the file first before I ran the embedded shell script. I'll give this a try and see how it goes.
RobbieSnr
 
Posts: 6
Joined: Sat Jun 15, 2019 9:45 am

Re: Printing a File Wed Jun 19, 2019 12:07 pm • by luomat
I was intending to get Hazel to open the file first before I ran the embedded shell script.


Well, I tend to be a bit of a command-line nerd, so I solve a lot of problems that way, but I understand that not everyone is as comfortable in Terminal as I am.

That being said, if you can learn some shell commands as you go along, you can do some powerful things.

For example, if you wanted to look at a PDF before you printed it, you could do something like this:

Code: Select all

open -n -W -a Preview "$1"

lpr "$1"


which will open the file (represented by "$1") in the Preview app (-a), and will wait (-W) until the Preview app quits before it prints the file.

The "-n" says "If Preview.app is already running, start a NEW version of Preview app just for this file" and when THAT version of Preview quits is when the `lpr` command will run.
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Re: Printing a File Thu Jun 20, 2019 12:43 pm • by RobbieSnr
Well, I tend to be a bit of a command-line nerd, so I solve a lot of problems that way, but I understand that not everyone is as comfortable in Terminal as I am.

I have used Terminal from time to time but am not very proficient in it's use!

I'm having some bother just with the printing part. If I open Terminal and use lpr (filename) it works OK so the lpr command does work. However it's not working in the embedded script. It seems that the problem is that the file I'm trying to print has spaces in its name. When I read the debug file the filename passed to lpr is cut short before the first space.

For example the file I'm trying to process has a name like" P1234 0694L EDI.pdf". I look for a file that has "0694L EDI" in its name and it picks up this one. I'm just using the simple script "lpr $1" and what Hazel seems to be passing to this script is just P1234 so the script fails.

How do I ensure that the whole filename and extension is passed to the embedded script?
RobbieSnr
 
Posts: 6
Joined: Sat Jun 15, 2019 9:45 am

Re: Printing a File Thu Jun 20, 2019 2:38 pm • by luomat
Hrm…

try this:

Code: Select all
#!/bin/zsh -f

/usr/bin/lpr "${1}"

exit 0



and see if that helps. Sometimes `"$1"` seems like it should be enough, but `"${1}"` should definitely be enough.

(Then again, irritations like this are while I still use '-' instead of spaces in filenames, but that's me. I realize the rest of the world isn't broken the way I am :-)
luomat
 
Posts: 78
Joined: Wed Mar 10, 2010 3:57 pm

Re: Printing a File Fri Jun 21, 2019 11:18 am • by RobbieSnr
Many thanks, that worked fine.
RobbieSnr
 
Posts: 6
Joined: Sat Jun 15, 2019 9:45 am


Return to Support

cron