Bulk convert text files to PDF

Jun 06, '11 07:30:00AM

Contributed by: tedw

Here is a quick AppleScript droplet to bulk convert text files (plain text, rich text, html, some code files like C and javascript files...) to PDF format.

Copy the following script into the AppleScript Editor (/Applications/Utilities/AppleScript Editor) and save it as an application.

Then drag-and-drop files onto it to convert them to PDFs.

on open theFiles
  set oldTID to AppleScript's text item delimiters
  repeat with thisFile in theFiles
    -- get file path as posix path
    set inputFilePath to POSIX path of thisFile
    
    -- create output path - same name with .pdf extension
    set AppleScript's text item delimiters to "."
    set outputFilePathBits to text items of inputFilePath
    set last text item of outputFilePathBits to "pdf"
    set outputFilePath to outputFilePathBits as text
    
    -- create convert command and send to shell
    set AppleScript's text item delimiters to " "
    set cmdList to {"/System/Library/Printers/Libraries/convert", "-f", quoted form of inputFilePath, "-o", quoted form of outputFilePath}
    do shell script (cmdList as text)
  end repeat
  set AppleScript's text item delimiters to oldTID
end open
The new PDF files will be put in the same folder as the originals.

The convert utility is not well documented (enter /System/Library/Printers/Libraries/convert without options in Terminal to see the brief help menu), but it's really just a front-end for the cupsfilter utility, which has better documentation. If you're a CUPS expert you might be able to do more with it than this, but for simple conversions this does nicely.

[crarko adds: I tested this, and it works as described.]

Comments (17)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110605113316760