
What changed
- A more clean, robust source code.
- Added a requested feature: to be able to choose the source folder(of the input file) as the destination folder.
- Fixed a path error by properly escaping the file string.
- It works now in the temporary folder, so no more files flashing around.
Download Version 1.1
No need for additional software on Mac, if the software we already have is maintained, to succeed in performing virtuously tasks.
Download Convert To PDF
Ruby Dopamine
Here is still the Automator Ruby code that accomplish the painful task of batch processing files to PDF. It got a little bit longer, but can handle bug tracking better that way. It was a mess to figure out what kind of string; escaped or not, cupsfilter would expect.
require 'tmpdir'
require 'fileutils'
ARGF.each do |f|
#
# Input file
#
input_file = f.chop # get rid of the newline charachter
input_file_extension = File.extname(input_file)
input_file_basename = File.basename(input_file, input_file_extension)
input_file_name = "#{input_file_basename}#{input_file_extension}"
input_folder = File.dirname(input_file)
#
# Final output folder
#
if ENV['same_folder'] == '1' then
output_folder = input_folder
else
output_folder = File.expand_path((o = ENV['output']).nil? ? "~/Desktop" : o)
end
#
# Final output file
#
output_file_name = "#{input_file_basename}.pdf"
output_file = File.join(output_folder, output_file_name)
#
# Temporary input/output file
#
# cupsfilter doesn't handle well filenames with latin-1 encoding.
# tried to escape the filename but doesn't help cupsfilter
# require to work on a copy with ASCII encoding.
input_copy = File.join(Dir.tmpdir,'original.cupsfile')
temporary_file = File.join(Dir.tmpdir,'converted.cupsfile')
#
# Execute cupsfilter
#
FileUtils.copy(input_file, input_copy)
`cupsfilter "#{input_copy}" 2> /dev/console > "#{temporary_file}"`
FileUtils.remove_file(input_copy) if File.exists?(input_copy)
#
# Check if cupsfilter converted successfully
#
if $?.success? then
FileUtils.mv(temporary_file, output_file)
puts output_file # return the new file to automator
else
FileUtils.remove_file(temporary_file) if File.exists?(temporary_file)
$stderr.puts "Cannot convert #{input_file_name}"
exit 1
end
end
Related posts:
- Convert to PDF was astonished not to discover an easy way to...
- Flatten Movies Version 1.1 latten Movies is an Automator action to flatten every movie...
- Doc to pdf with Automator As requested here is one possible way to convert doc...
- Convert to PDF Version 1.2 A long time has passed and our favorite OS has...
- Flatten Movies latten Movies is an Automator action to flatten every movie...
isn’t it possible to convert .doc? will it ever be possible?
further more: i want to send the converted files via email, so the pdf needn’t be saved anywhere. is there an option?
thanks for the great script!
I address the question “convert to doc” in a new post Doc to pdf as it might be useful for everyone and as I googled around it’s a topic of its own.
For the second point I have to investigate the capabilities of Automator’s variables, a little bit and the possibility of storing the intermediated files in a temporary directory.
As I understand the Automator philosophy is: get files, modify files, create files, modify files etc and than trash the files you don’t want? If an action creates temporary files some where, you aren’t aware of, who will be responsible/supposed to delete those files? It gets messy. I think it’s a design choice to let the user see what’s happening when the workflow runs.
Hello,
I love this..
One thing i would suggest.. instead of making it a workflow, make it a bit more friendly by making it an Application.
Like some of these…
What would help to make it an application is not to let uses add the files, but use the “Ask for Finder Items” and make sure the “Allow Multiple Selections” box is ticked… then just add a Growl notification.. some confirmation messages and your off…
Although you would not get the option of where to save it..
Good Luck!
I have tried installing and using this script. I see the script in my Automator, but cannot for the life of me figure out how I can use it. I cannot figure out where to drag and drop my .doc or .xls file so that the conversion begins even??
HELP!
Thanks.
Install the action and then create a service using Automator. It’s very easy.
Select documents in Finder and then add the action ‘Convert to PDF’.
After saving the service, go to your file, click right and select ‘Convert to PDF’
Enjoy!
About your Automator action ‘Convert to PDF’
Hi,
I’m happy because finally I found an Automator action to transform files to PDF. Otherwise, the action has a ‘bug’… For example, if I try to convert an horizontal PS file, the result is a vertical PDF without all the information. The action cuts the page.
If you have the intention to solve this problem, let me know “bernat.puigdomenenech@gmail.com”
Thanks for sharing,
Bernat
Thank you for the script! Works great too. I do have one issue/question though. I convert .tiff to .pdf just fine. But it’s rotating the pdf. is there a way to change that? I don’t know code very well yet at all.
Any advice or help would be wonderful.
This might be a good idea if the developer had provided decent directions on how to use it. As it is, it appears to have been designed by a geek for geeks. I’m not enough of a geek to figure it out. I suggest the author go back to the drawing board – or find someone who knows how to use it to write a decent set of instructions. I think the suggestion above to make it an application has merit.