Printing files with «folder action» scripts ...

Hi,

I still have an OS 9 application running on my Tiger machine. I also want to upgrade my printer to a new wireless network printer.

For sure there is no driver to work from Classic to this printer. Since my OS 9 app can print to a postcript file, I would like to use «folder action» to print my file on my new printer. When a file is created in a specific folder how can «folder action» automatically:

¢ convert the Postscript file to a PDF file
¢ print the file to a specific printer
¢ move the printed file to a specific «Print completed» folder

I looked around in Mac Scripter and this kind of script seems to be work for AppleScript pros.

Does anybody know if this problem has already been solved ?

Regards.

Robert

Hi,

to convert PS to PDF look at /usr/bin/pstopdf

to print files to a particular printer look at /usr/bin/lpr

to move files look at /bin/mv

Hi Stefan,

I thank you for your post. I have looked into this problem and found that scripts behind «action folder» are complex and often require shell scripting abilities because of all the managing of error conditions.

I know that most of the time it is important to try it our selves. Because of important work now and of my father’s health, I do not have the time to invest in scripting very much. I maybe have to return the printer if this does not work.

My problem is not focused on having fun to script because time is missing. Shell scripting is well over my head and I was looking for something already scripted. In other condition I would have taken that time, but now I cannot. I realize that scripting is more of a hobby than an ability, because I don’t do that frequently. Lats year I spent so much time on scripting because of a old printer problem that I can say that, while I had much pleasure, it was not efficient. Scripting in a multi user/computer and shared environment requires experience. That, I do not have

I want to see if I can use my new printer from within OS 9 with simplicity with a iMac running Tiger (10.4.11). My idea is that from the OS 9 application I can print to a postscript file. If I double click on it, it converts automatically to a PDF file. Understanding this possibility, I thought that if I print the PS file to a folder with «action folder», the script could execute for me in background all these steps:

¢ convert the Postscript file to a PDF file
¢ print the file to a specific network printer
¢ move the printed file to a specific «Print completed» folder

Thanks for reading.

Robert

I found a script that translates a PS file to PDF. I found also a model for the printing part, but it does not work. In order to see what is not working in the print part, I am trying it separately. Here it is:

on adding folder items to this_folder after receiving these_items
	try
		repeat with i from 1 to number of items in these_items
			do shell script (getShellCommand(quoted form of (POSIX path of "Officejet Pro 8500"), 1, 150, false, false, i))
		end repeat
	end try
end adding folder items to



on getShellCommand(thePrinter, upNumber, brightness, isDuplex, islandscape, this_item)
	(* arguments description
thePrinter: choose a printer (lpstat -p ENTER, in Terminal, shows all your printer)
upNumber: integer {1,2,4,6,9,16}
isDuplex: boolean, use true or false
brightness: integer, Values greater than 100 will lighten the print, while values less than 100 will darken it
this_Item: alias (!) to the File you want to print
also see: http://www.cups.org/doc-1.1/sum.html for more settings
*)
	
	set theCommand to "lp -d " & thePrinter & " -o number-up=" & upNumber & " -o brightness=" & brightness
	if isDuplex then
		if islandscape then
			set theCommand to theCommand & " -o sides=two-sided-short-edge "
		else
			set theCommand to theCommand & " -o sides=two-sided-long-edge "
		end if
	end if
	
	set theCommand to theCommand & quoted form of (POSIX path of (this_item as alias))
	return theCommand
	
end getShellCommand

Regards.

Robert

simplest version, specify myPrinter and destinationFolder (slash at the end is required)
You can get the correct spelling of the printer in Terminal with lpstat -p
uncomment the third shell script line to delete the postscript file


property myPrinter : "Pro_600"

on adding folder items to this_folder after receiving these_items
	set destinationFolder to POSIX path of (path to desktop) & "testFolder/"
	repeat with oneItem in these_items
		set {name:Nm, name extension:Ex} to info for oneItem
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		if Ex is "ps" then
			set destinationFile to quoted form of (destinationFolder & Nm & ".pdf")
			do shell script "/usr/bin/pstopdf " & quoted form of POSIX path of oneItem & " -o " & destinationFile
			do shell script "usr/bin/lpr -P " & myPrinter & space & destinationFile
			-- do shell script "/bin/rm " & quoted form of POSIX path of oneItem
		end if
	end repeat
end adding folder items to

Your are a genius Stefan … I thank you so much.

PS files are generated consecutively to the «action folder». There seem to be a problem if another file is created while one is printing or not yet completely printed. The second file does not print. I tried to manually move 2 files to the «action folder», and they both printed. So there seem to be a difference between «moving 2 files» and «moving consecutively 2 files» to the «action folder».

Also my OS 9 app generates PS files with the same name. Since the PS file is deleted after being converted to PDF, I thought of «date & time» stamping all new PDF files before printing them, to avoid overwritting one file to another one with the same name.

Are these add-on complicated to manage ?

Regards.

Robert

Hi Stefan,

I copleted my script and was able to find a way to stamp the printed file. The script works with Snow Leopard but does not print on Tiger. Why would this script not print on Tiger:

property myPrinter : "OfficeJet_Pro-8500"
property destinationFolderName : "Impression"
property fileStamp : ""


on adding folder items to thisFolder after receiving theseItems
	
	set destinationFolderPath to POSIX path of (path to desktop) & destinationFolderName & "/" & "Fichiers imprimés" & "/"
	
	repeat with i in theseItems
		
		set {name:fileName, name extension:fileExt} to info for i
		
		if fileExt is missing value then set fileExt to ""
		if fileExt is not "" then set fileName to text 1 thru ((count fileName) - (count fileExt) - 1) of fileName
		if fileExt is "ps" then
			my makeStamp((current date), "-", "H")
			set destinationFilePath to quoted form of (destinationFolderPath & fileName & " " & fileStamp & ".pdf")
			do shell script "/usr/bin/pstopdf " & quoted form of POSIX path of i & " -o " & destinationFilePath
			do shell script "usr/bin/lpr -P " & myPrinter & space & destinationFilePath
			do shell script "/bin/sleep 10"
			do shell script "/bin/rm " & quoted form of POSIX path of i
			end if
end repeat
end adding folder items to



on makeStamp(Now, DelimD, DelimT) -- the first delim for date, the second for time.
	tell Now to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
		to set dateStamp to text 2 thru 3 & DelimD & text 4 thru 5 & DelimD & text -2 thru -1
	tell Now to tell ((1000000 + (its hours) * 10000 + (its minutes) * 100) as string) to set timeStamp to text 2 thru 3 & DelimT & text 4 thru 5
	set fileStamp to "(" & dateStamp & " " & timeStamp & ")"
	return fileStamp
end makeStamp

Regards.

Robert

folder actions don’t send error messages, they just abort the script when an error occurs.
Comment out the adding folder event handler and add the line

set theseItems to choose file with multiple selections allowed

Then you can test the script directly and you get error messages.
It might be that the CLI pstodpf doesn’t exist yet in Tiger.

The timestamp can be easily created with a shell script.
Delete the filestamp property and the makeStamp() handler and replace the my makeStamp() line with


set fileStamp to do shell script "/bin/date +'('%d-%m-%y' '%HH%MM%S')'"

I added seconds in case of you want to print more than one file within a minute

Hi Stefan,

Thanks for your input …

set theseItems to choose file with multiple selections allowed

Where exactly in the handler do I put this line ? I tried it and I get no error message.

As I said the handler works perfectly on my Snow Leopard machine. On my Tiger machine, the stamped file is created but never prints, neither is it deleted to the «Fichiers imprimés» folder as it is supposed.

I think that your assumption about the print statement could be the problem. Is there any way to print the file that would be compatible with Tiger ?

Regards.

Robert

.and add the choose file line at the beginning

I am confused … If the files to be selected are not physically moved on the «action folder» in the Finder, but chosen through the finder selection window as it appears to be, how will this trigger the «action folder» ?

I tried it and did not get any result … no moved file, no printing, no error message. I am missing something, that’s sure.

as mentioned above, a folder action script doesn’t report any error messages
just test the script as a normal script run in Script Editor.
The files will be passed in the choose file line instead of the action handler.
Beginning and end of the script should look like


-- on adding folder items to thisFolder after receiving theseItems
set theseItems to choose file with multiple selections allowed
.

.
-- end adding folder items to

Hi Stefan,

It took long to get back … I was working on something else and forgot this little project. Reality got me on track when it was required again. Here I am …

I now understand what you mean by «comment out …». New vocabulary !!! I did it and got this error message:

What do I do from there ?

Regards.

Robert

I have been looking … but still didn’t find was is going wrong.

Did you confirm that you have the printer name correct? The unix name is not always exactly the same as how it shows elsewhere. If you haven’t done this already, open the Terminal app in the Utilities folder. Then type:

lpstat -p

and hit return. Your printer should show on a line that says it is “idle”. Make sure the name that appears there matches exactly what you have on this line:

property myPrinter : “OfficeJet_Pro-8500”

Based on what you have for the property line, you should see something like this in terminal:

printer OfficeJet_Pro-8500 is idle. enabled since Fri Oct 10 13:17:31 2008

If that is OK, I agree with Stefan’s earlier post that you should confirm that the pstopdf command is available in Tiger. This may be a newer command that appeard in Leopard.

On your Tiger machine, launch Terminal again. Type:

man pstopdf

and hit return. If you get the “manual” page explaining how to use the command then it is OK. If you get an error then that command is not available in Tiger.

Do you happen to have Acrobat Distiller? It’s part of the Adobe creative suite so if you didn’t specifically buy and install it then you won’t have it. Distiller is scriptable to convert a PS file into a PDF.

Hope that gives you some direction.

Hi Stefan,

Here is my script:

property myPrinter : "OfficeJet_Pro_8500"
property destinationFolderName : "Files_to_print"
property fileStamp : ""


--on adding folder items to thisFolder after receiving theseItems

set theseItems to choose file with multiple selections allowed

-- Define the PDF file's two (2) destination folders.
set destinationFolderPath1 to POSIX path of (path to desktop) & destinationFolderName & "/"
set destinationFolderPath2 to POSIX path of (path to desktop) & destinationFolderName & "/" & "Printed file" & "/"

-- For each file dropped on the folder ...
repeat with i in theseItems
	
	-- ... define the file's info; ...
	set {name:fileName, name extension:fileExt} to info for i
	
	--  ... if the file's extension is «.ps», ...
	if fileExt is missing value then set fileExt to ""
	if fileExt is not "" then set fileName to text 1 thru ((count fileName) - (count fileExt) - 1) of fileName
	if fileExt is "ps" then
		
		-- ... define the file stamp, ...
		set fileStamp to do shell script "/bin/date +'('%d-%m-%y' '%HH%MM%S')'"
		
		-- ... define the name and first localisation of the file to be converted , ...
		set destinationFilePath to quoted form of (destinationFolderPath1 & fileName & " " & fileStamp & ".pdf")
				
		-- ... convert the PS file to PDF and place it in destinationFilePath1, ...			
		do shell script "/usr/bin/pstopdf " & quoted form of POSIX path of i & " -o " & destinationFilePath
		
		-- ... delete the original PS file,
		do shell script "/bin/rm " & quoted form of POSIX path of i
		
		-- ... and then, print the new PDF file on the selected printer.
		do shell script "usr/bin/lpr -P " & myPrinter & space & destinationFilePath
		do shell script "/bin/sleep 10"
		
		-- If printing succeeds, move the PDF file to destinationFilePath2
		-- move PDF file to destinationFilePath2?		
		
		
	end if
end repeat

--end adding folder items to

Everything works until the file is supposed to print. I get this error message: lpr: error - unable to print file: client-error-not-found. The «if printed move the file» part is also missing … If I print manually the PDF file everything is ok.

I found a script on the web that prints without converting to PDF but does not stamp the file nor move it either. The print output has no top margin. I tried to merge the 2 scripts without success. It print like it did and no conversion occurs nor is the PS file deleted. I will need a little help of my friends:

Here is the script:

on adding folder items to this_folder after receiving these_items
	try
		tell application "Finder"
			repeat with i from 1 to number of items in these_items
				try
					set this_item to item i of these_items
					set the path_string to this_item as string
					set the final_path to POSIX path of the path_string
					do shell script "/usr/bin/lp '" & final_path & "'"
				on error error_message
					tell application "Finder"
						display dialog "Error for item " & (this_item as string) ¬
							& ": " & error_message buttons {"Continue", "Cancel"} ¬
							default button 1 giving up after 120
					end tell
				end try
			end repeat
		end tell
	on error error_message
		tell application "Finder"
			display dialog error_message buttons {"Cancel"} ¬
				default button 1 giving up after 120
		end tell
	end try
end adding folder items to

Thanks in advance.

Robert

Hi Matt-Boy,

My printer’s user name is different from the printer’s system name. In Terminal I got: “Officejet_Pro_8500_A909n__HPFEF102_” while the name that I see in the printer selector is “OfficeJet_Pro_8500”. With the name I got in Terminal it worked. Thank you. I still have two concerns:

¢ the page layout has no top margin: is it possible to select print options in from the shell script ?
¢ when printed, what is the shell script to move the file to the «Printed files» folder ?

Thanks again to you 2 for your continous help to all of us not expert, and that gives us power to just keep trying.

Regards.

Robert

Model: iMac Intel (10.6.2) + iMac G5 (10.4.11)
AppleScript: 2.3 + 2.1.1
Browser: Safari 531.9
Operating System: Mac OS X (10.6)

Funny, the file converted to PDF displays correctly on the screen but does not print with the display margins.
I fixed the Page Setup problem by tweeking the Page Setup in the app that creates the PS file, but I still don’t understand why the file that displayed correctly did not print accordingly.

To move the file, this is my actual script (it does not work):

set originalFilePath to quoted form of (destinationFolderPath1 & fileName & " " & fileStamp & ".pdf")
		set destinationFolderPath to quoted form of destinationFolderPath2
		tell application "Finder"
			move (originalFilePath as alias) to (destinationFolderPath as alias) with replacing
		end tell

Any comments ?

Regards.

Robert

you can set the margins with the -o switch

From http://www.cups.org/doc-1.1/sum.html

[i]Setting the Page Margins

Normally the page margins are set to the hard limits of the printer. Use the -o page-left=value, -o page-right=value , -o page-top=value, and -o page-bottom=value options to adjust the page margins:

lp -o page-left=value filename ENTER
lp -o page-right=value filename ENTER
lp -o page-top=value filename ENTER
lp -o page-bottom=value filename ENTER
lpr -o page-bottom=value filename ENTER
The value argument is the margin in points; each point is 1/72 inch or 0.35mm.[/i]

You can not coerce a quoted POSIX path to alias, you have to remove the quotes

Shell for move is

mv [POSIX path to source file] [POSIX path of destination directory]

As a general note, the “quoted form of” is only necessary for a POSIX path. It will keep the shell command from erroring if there are any spaces in the file path (the file name or any folder name). The “quoted form of” should not be used when dealing with plain applescript such as your “Finder” code.