Printing a PDF should be simple enough? No?

I’ve been trying my darndest to make simple “print from finder” script. Basically I want to drag a PDF (or bunch of PDFs) to a folder have them open up automatically and print, then close and delete themselves, if possible. So far though, I’ve been stumped at the simple print command!
Here’s the script I’m working on so far. It opens the file and closes it, but no print dialog box turns up and it just closes. Any ideas? Thanks!

display dialog "Do you want to print? " buttons {“Cancel”, “Ok”} default button 2 with icon 1
tell application “Finder”
activate
select every item of folder “test”
open selection
end tell
tell application “Acrobat 6.0.2 Professional”
print
close all docs
end tell

replace the print command with this one:

print pages of front document

Another way to print your pdf’s is to use LPR - much less overhead and way faster.

Here’s something close to what I’ve been using and it has worked well for me.

lpr -P some_printer /path/to/the/file.pdf

1st. Launch Terminal (under application/utilities) and type the following to get a list of your printers.

lpstat -l

This will list your printers and should look something like.

Find the printer you want to send to, and copy the name, or the name and IP if shown. Depends on how you have it set up.

At this point, you could try out lpr by typing
"lpr -P " then paste your printer name here, put a space after that and drag a pdf of your choice to the terminal window. Then, just hit enter.

Now, how to get that in a script to work.

Since you have the printer name your script can now set a variable to this.

set myPrinter to "whateveryoucopied"

Now, you just need a file path - and it has to be a posix path “/directory/directory/file”.
Here’s a snippet that lists a folder, and gets the posix path of each item.

set myfolder to "Your Computer name:Users:SomeUser:Desktop:Jobs:"
set myFiles to list folder myfolder without invisibles

repeat with x from 1 to count myfiles
set thisPath to posix path of file (myFolder &(item x of myFiles) as string)
--should return /Users/SomeUser/Desktop/Jobs/afile.pdf
end

Now, since we are getting the posix path for each file - we have all the information we need for our shell script. Include your printer name snippet as well.

set myPrinter to "whateveryoucopied"
set myfolder to "Your Computer name:Users:SomeUser:Desktop:Jobs:"
set myFiles to list folder myfolder without invisibles

repeat with x from 1 to count myfiles
set thisPath to posix path of file (myFolder &(item x of myFiles) as string)
--should return /Users/SomeUser/Desktop/Jobs/afile.pdf
set theShell to "lpr -P "  & myPrinter & " " & thisPath
try
do shell script thisPath
--optionally log success here
on error err
--or, if there was a problem log that or send it to someone
end try
end repeat

Just thought I’d share something that’s been working for me. Hope it helps.

One thing I found using lpr (it is way faster than printing from Acrobat) is that you can print ONLY PDFs that have exactly the same size as you paper size. If you want to print a PDF that is 9x12” on a 8.5x11” you will receive an error. Maybe there is a way, I’m just not aware of it :shock:

You guys are awesome! Thanks! I will try every single suggestion and get back to you-all. :smiley:

OK here’s what happened with all the suggestions:

Replacing the print command with ‘print pages of front document’: The document opened and printed, however as my PDFs are all large documents, they printed just a porion of it. My normal print command in Acrobat 6 has Page Scaling set to Shrink Large Pages. The other thing is, if I have more than one PDF in there, only one will get to be printed. I was hoping that all the PDFs in the folder will be printed. Is there any way to get these commands to fit in the script?

Using Terminal seemed like a great idea…however, I got derailed right at the start. Typing in lpstat -l somehow just wouldn’t bring up any list of printers! Here’s what I got:Last login: Wed Nov 17 09:09:37 on ttyp1
Welcome to Darwin!
[Mark-Pans-Computer:~] markpan% lpstat -l
[Mark-Pans-Computer:~] markpan% lpstat -l
[Mark-Pans-Computer:~] markpan%

That’s exactly as it appears in Terminal. I don’t know why no list appears. I have a laserprinter connected through the network and printing works fine. By all intents and purposes, the list should appear. Any ideas?

Thanks!

if you have PDF files larger than your paper size you should use:

print pages of front document with shrink to fit

For Printer Name you can always use the name you see in Printer Setup Utility. Select your Printer, get info on it and copy-paste Printer name. :wink:

Hi guys,
Why not just batch print from Acrobat. You can edit your sequence to allow you select either a folder or several files and to not save after completion. You don’t even need to have a PDF open, just the application.
Just a thought.
mike

Thanks a bunch costicladop! Works like a charm! In fact, to get multiple PDFs to print, I just changed the ‘print front document’ to ‘print every document’ and it did as expected.

Now based on this script, is it possible to make an application that I can drag and drop PDFs onto and they would print and close automatically? My original intention when I started was to select a PDF or (multiples of PDFs) in the finder and put an applescript application on my window toolbar and it would print with a click. I couldn’t get the ‘slection’ command to work, which is why I came up with the folder idea.

Is it possible to make such an application?
Thanks! :rolleyes:

I’ve got it! Here’s the final code. I basically save the script as an application, drag it to my window toolbar, then whenever I need to print something, I simply select it in finder and click the app in the window toolbar. Thanks for all your help guys!

I only wish I could figure out the command-line thingy…and one day I will :smiley:


display dialog "Do you want to print? " buttons {"Cancel", "Ok"} default button 2 with icon 1
tell application "Finder"
	activate
	open selection
end tell
tell application "Acrobat 6.0.2 Professional"
	print pages of every document with shrink to fit
	close all docs
end tell

Mike Helbert,

I believe that your idea has a lot of potential and it might fix one of my problems…

Here’s one of my scripts (folder action):

on adding folder items to thisFolder after receiving addedItems
repeat with anItem in addedItems
tell application “Acrobat 6.0.2 Professional”
activate
open anItem
print pages of front document with shrink to fit
close front document
end tell
end repeat
end adding folder items to

A script like this one should print every single PDF file dropped inside the “watched” folder. In reality, when PDFs come faster than are printed, some of the PDFs are not printed and some are printed multiple times.

Just to have an idea of what I’m talking, think about Distiller setup on a server and multiple users are printing to the Distiller hot-folders. Creating PDF files takes less time than printing from Acrobat and i believe this is the problem…

You were saying about creating a sequence and so on. Do you believe that it might help my printing process?

To print to a printer, this works very well.
Under the ‘Advanced" menu there is a list for "Batch Proccessing…’
There should be a sequence for ‘Print all’.
You can edit that to your own liking.
Have fun!
I’m out of the office for the next week. I hope that all goes well for you.
mike

If you do this for use with command line, be sure to replace any spaces with an underscore.

As for non-letter page sizes via lpr, I’ve never done it, but you can print to other sizes by setting up special print queues I believe. Maybe someone here has done that - I couldn’t find anything after a brief search.

Sorry - I am an idiot. That should be

lpstat -p

Mytzlscript: hehehe, yes this worked, thanks. I’ll try out the terminal script you suggesteed. The applescript that I posted here is the final one that I am using and works exactly as I intended. This stuff has gotten me really interested in Applescript though, and I’m curious as to how it can control terminal too.

Mike Helbert: Thanks for your suggestion…I didnt’ even know the batch processing command existed! Heh, that’s how much I know acrobat! Basically, I use Illustrator mostly, but then save the files to low-res PDF for client approvals and archiving. I will surely explore the batch processing command now!

costicladop: Your batch printing script looks interesting. Does it allow you to print in sequence?