Convert to PDF

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Convert to PDF Fri Feb 24, 2012 4:42 pm • by bitsandbooks
I'm working on a Hazel action that will watch a folder and, if a file appears, will convert it to a PDF and delete the original. The conditions are simple: if Name is not blank, run this embedded AppleScript:

Code: Select all
set _list to {"txt", "doc", "docx", "xls", "xlsx", "ppt", "pptx"}

tell application "Finder"
   set _path to POSIX path of file theFile
   set _name to name of file theFile
   set _ext to name extension of _info
   set _info to info for theFile
end tell

if _list contains _ext then
   set _inputFile to _path & _name & "." & _ext
   set _outputFile to _path & _name & ".pdf"
   do shell script "/System/Library/Printers/Libraries/convert -f " & quoted form of _inputFile & " -o " & quoted form of _outputFile
   delete _inputFile
end if


However, even with a simple .txt file, this rule comes back with a failure. Can anyone tell me why?
bitsandbooks
 
Posts: 2
Joined: Fri Feb 24, 2012 2:42 pm

Re: Convert to PDF Sat Feb 25, 2012 4:45 pm • by Mr_Noodle
How do you mean the rule comes back with a failure? Does it fail to match or does it fail to execute? Use the preview to determine the former and the logs to determine the latter. If the latter, post the relevant log messages.

Thanks.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Convert to PDF Sun Feb 26, 2012 12:37 am • by Pe8er
Your Applescript was failing in a few places. I fixed it for you:

Code: Select all
set _list to {"txt", "doc", "docx", "xls", "xlsx", "ppt", "pptx"}

tell application "Finder"
   set _info to info for (get POSIX path of theFile)
   set _path to POSIX path of (get container of file theFile as alias)
   set _name to text 1 thru -5 of (get name of file theFile)
   set _ext to the name extension of _info
end tell

if _list contains _ext then
   set _inputFile to _path & _name & "." & _ext
   set _outputFile to _path & _name & ".pdf"
   
   do shell script "/System/Library/Printers/Libraries/convert -f " & quoted form of _inputFile & " -o " & quoted form of _outputFile
   tell application "Finder" to delete theFile
end if
Pe8er
 
Posts: 21
Joined: Sun Nov 06, 2011 12:52 pm

Re: Convert to PDF Sun Feb 26, 2012 4:41 pm • by sjk
Are you sure you want to unconditionally delete original files without verifying successful PDF conversion?
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

Re: Convert to PDF Mon Feb 27, 2012 1:20 pm • by bitsandbooks
I'd rather it do a lot more checking (e.g., for a duplicate output file before processing or that the conversion succeeded before deleting the original) but I'm not much of an AppleScripter.

Here's my situation. At work, I sometimes need to turn files into PDFs, but since we don't have Acrobat Pro at work, I thought I could use Dropbox and Hazel (on my Mac at home) to accomplish the task. The idea is this:

  1. At work, put file into specified Dropbox folder (via web, since they won't let us use Dropbox at work, either).
  2. Since this folder is shared, it downloads to my Mac at home automatically.
  3. Hazel sees the file, turns it into a PDF.
  4. The original is deleted, leaving the new PDF for me to download.

If anyone has a better way to accomplish this, I'm all ears.
bitsandbooks
 
Posts: 2
Joined: Fri Feb 24, 2012 2:42 pm

Re: Convert to PDF Mon Feb 27, 2012 2:01 pm • by Pe8er
I don't want to sound negative but it seems you've chosen a quite complex way to accomplish a relatively simple task. There are tons of free apps to convert documents to PDFs. If you can't install any software, then you can print to PDF. On Mac it's baked into OS X, on Windows you'd have to install a virtual printer. If you're out of luck here (Windows and no installations), there are websites which convert to PDF for you.

What's the catch, why Dropbox and Hazel?
Pe8er
 
Posts: 21
Joined: Sun Nov 06, 2011 12:52 pm

Re: Convert to PDF Mon Feb 27, 2012 4:35 pm • by sjk
Pe8er wrote:There are tons of free apps to convert documents to PDFs.

Like the bundled convert (aka cupsfilter) command that bitsandbooks' script is using. I never found anything comparable bundled with Windows during my brief periods of using that OS.

If you can't install any software, then you can print to PDF. On Mac it's baked into OS X, …

Yup.

… on Windows you'd have to install a virtual printer.

CutePDF Writer (free) has worked well enough for my purposes, after installing Ghostscript.

[quote If you're out of luck here (Windows and no installations), there are websites which convert to PDF for you.[/quote]
If bitsandbooks is restricted from installing software on the work system(s) I'd suspect that would also include a virtual printer, regardless of any other requirements (e.g. Ghostscript). And maybe there's a restriction on web service conversion usage.

What's the catch, why Dropbox and Hazel?

As the current method of getting around work limitations, which is just speculation until bitsandbooks clarifies. :)

If possible, doing the conversions directly at work, even through a web service, surely seems preferable.
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene


Return to Support