Printing to specific paper size via applescript

I am trying to make a folder action script take all the pdf files dropped into it and send them directly to a specific printer. The problem I have is it uses all the default settings and I need to use a different paper size. Preview app is not scriptable so I figure I will have to use Acrobat. If I can avoid this it would be even better. I have found very little on printing from applescript so any help would be great.

Note: This folder action will be placed onto several machines running either OS X 10.3.9 or 10.4.8

paper size is not selectable via applescript. your mileage may vary though when it comes to printing from apps using different paper sizes.

There’s a brand new hint at macosxhints.com about this. It should be just what you’re looking for…

http://www.macosxhints.com/article.php?story=20070312100620242

Did anyone actually try this??

Have not, but robg usually tests most things before he posts them

But I asked James, because in this instance he didn’t say so, and he usually does + it didn’t work for me. :frowning:

From what I can tell there is nothing that you could do directly through Preview or Acrobat without using UI scripting (I try to avoid that) baring that you could look to see if there are any hooks in Acrobat via Javascript I don’t have much experience in that so I can’t help there. Another option might be to import the pdf as a Graphic into say Indesign (or Quark for you Quark fans) try printing it from there… I don’t know that may work

I just checked and the hint worked for me. I made a print preset which included changing the paper size as the poster asked in the original question, ran the shell script from the hint, and when I used Preview to print, my preset came up as the default in the print dialog window.

You can send PDFs directly to a printer with the LPR shell command. You can also indicate page size with the “-o media” parameter. I wrote this script to do exactly what you are trying to do. Save it as an application, then you can drag and drop a file, muliple files or a folder full of files (all PDFs of course) onto the script icon.

I wrote it when we had Panther and Tiger machines here so it checks and uses the different naming convention that the two systems use.

The only weird thing about the LPR command is if the PDF is smaller than the page size, it puts the document in the bottom-left corner of the page. It works fine but looks a little odd.

-- This droplet processes both files or folders of files dropped onto the applet
property YourPrinterPanther : "NameOfYourPrinter on Address" --Name as it appears in the Printer List in Panther
property YourPrinterTiger : "NameOfYourPrinter" --Name as it appears in the Printer List in Tiger
property YourPrinter : ""
property NumCopies : ""
on open these_items
	tell application "Finder"
		activate
		set InputNum to the button returned of (display dialog "How many copies?" buttons {"1", "2", "3"} default button "1")
		set NumCopies to InputNum as real
	end tell
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
	tell application "Finder"
		activate
		display dialog ("Finished!") buttons " " giving up after 1
	end tell
end open

-- this sub-routine processes folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine prints the files
on process_item(this_item)
	tell application "System Events"
		set the item_info to info for this_item
		if (alias of the item_info is true) then
			display dialog "No aliases!" buttons "Cancel"
		end if
		if (kind of item_info contains "PDF") then
		else
			display dialog "This file is not a PDF!" buttons "Cancel" default button "Cancel" with icon 0
		end if
	end tell
	
	set PathToDesktop to path to the desktop as text
	
	tell application "Printer Setup Utility"  --This checks which system you are in, thanks to Kai by the way.
		if ((system attribute "sysv") ≥ 4160) then
			set current printer to printer YourPrinterTiger
			set YourPrinter to YourPrinterTiger
		else
			set current printer to printer YourPrinterPanther
			set YourPrinter to YourPrinterPanther
		end if
	end tell
	
	set thisPathTemp to this_item as text
	set thisPath to POSIX path of thisPathTemp as string
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {" "}
	set theScript to every text item of thisPath
	
	set AppleScript's text item delimiters to {"\\ "}
	set thisPath to theScript as text
	set AppleScript's text item delimiters to ASTID
       --On the next line, change "letter" to whatever paper size you need.
	set theShell to ("lpr -P " & YourPrinter & " -#" & NumCopies & " " & thisPath & " -o media=letter")
	try
		do shell script theShell
	end try
end process_item

Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Well the hint appears to be working for me with extra printer options saved as presets for our HP5000 and not only that has shown me how to set the print/fax defaults I was asking about the other week. So I can change the paper size without the presets too. Should be able to proceed with my javaprint in acrobat now so many thanks “regulus6633”

-- Set Selected printer in Print Dialog:
do shell script "defaults write com.apple.print.PrintingPrefs UseLastPrinterAsCurrentPrinter 1"
-- Set Default paper size in Page Setup:
do shell script "defaults write com.apple.print.PrintingPrefs DefaultPaperID iso-a4"

Thanks Matt-Boy, your script works very well except for one problem, the paper size. If I enter “tabloid” as the paper size to use, which is what I choose from the Print dialog, the printer tells me the selected resources are unavailable. I also added a few lines to the end so that it will close the Printer Setup Utility once printing has finished. A few things I will have to add is a way for the script to check what all the default settings are so that upon termination it will restore the users defaults. Thanks again for the great work.

Hmmm. The printer must call “tabloid” something else. I wonder if there is a way to query the printer and have it tell you the paper sizes. Maybe something at the printer itself. I’m thinking maybe it’s as simple as changing “tabloid” to “11x17” or something like that.

Anyone know an easy way to get that info from the printer??

I found more info about the lpr command that may help others. Go to a terminal window and enter:

 lpoptions -p yourprintername -l

This will list out the options available. I used the information for my printer and changed the script.

 set theShell to ("lpr -P " & YourPrinter & " -#" & NumCopies & " " & thisPath & " -o inputslot=Tray2 -o pagesize=tabloid")

Now the printer knows where to get this size paper from. I think if you leave certain options out, it uses default values which are listed with asterisks in the lpoptions command. My printer was trying to feed tabloid from Tray1 as its the default tray.