One of the most popular downloads on the site is the “Print Clipboard” script. This AppleScript prints any text copied to the clipboard without the need to paste the text into a text editor or word processor prior to printing. One easy way to do this is to create an applescript, seems an easy thing to do but there are a couple of interesting aspects that make this worth sharing. One way to print in the background is to use the UNIX “lp ” command, there is an excellent summary of UNIX printing here. However when you copy text to the clipboard if often contains invisible non-printing characters that can cause lp to fail. So first we we convert the text to plain text and remove any application specific formatting. The as text portion grabs the stylized text from whatever other junk may be surrounding it (which can be significant), as record splits out the style and text portions, and «class ktxt» grabs the plain text portion of that. The text is then written to a file the location of which is defined within the temporary items folder. lp is then used to print the file. I was recently asked if it might be possible to have an option to save the text to a pdf file rather than printing. So I’ve added the option to print to pdf.
Copy any text to the clipboard and run the script. The first dialog shows the content of the clipboard
Click OK, and then the second dialog gives the option to Print directly (default option) or you can save as a pdf on your desktop. You can of course choose your own file name.
The AppleScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
set the clipboard to «class ktxt» of ((the clipboard as text) as record) set the_clip to the clipboard --Comment out if not needed display dialog the_clip tell application "Finder" to set the_folder to (path to temporary items folder) as text --If you want to keep the file use set the_path to path to desktop as string set pdf_path to POSIX path of the_path --set the_pdf to pdf_path & "foo.pdf" --display dialog the_pdf set target_file to the_folder & "JUNK" --display dialog target_file my write_to_file(the_clip, target_file, false) set posix_path to POSIX path of target_file --display dialog posix_path set theResponse to display dialog "Do you want to print or save pdf to desktop? Type filename in box below" default answer "Myfile" buttons {"Print", "Save PDF"} default button 1 --copy the result as list to {text_returned, button_pressed} if the button returned of the theResponse is "Print" then set to_print to "lp " & posix_path do shell script to_print else set the_pdf to pdf_path & (text returned of theResponse) & ".pdf" set to_print to "cupsfilter " & posix_path & " > " & the_pdf do shell script to_print end if --set to_print to "lp " & posix_path --set to_print to "cupsfilter " & posix_path & " > " & the_pdf --do shell script to_print --For testing --tell application "Terminal" --activate --do script to_print --end tell on write_to_file(this_data, target_file, append_data) try set the target_file to the target_file as text set the open_target_file to ¬ open for access file target_file with write permission if append_data is false then ¬ set eof of the open_target_file to 0 write this_data to the open_target_file starting at eof close access the open_target_file --display dialog "file_done" return true on error try close access file target_file end try return false end try end write_to_file |
You can download the AppleScript here.
One thought on “Print Clipboard”
Comments are closed.