Created
June 12, 2009 03:00
-
-
Save feldpost/128400 to your computer and use it in GitHub Desktop.
Print To Kindle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PRINT TO KINDLE in Mac OSX (for Kindle DX) | |
# | |
# HUH? | |
# - Kindle file depository: | |
# - drag any supported Kindle file to a local folder | |
# - file will be automatically transfered whenever Kindle is connected | |
# - "Print To Kindle" Print menu option (PDF): | |
# - when Kindle is connected it will print as PDF straight to the Kindle (from anywhere) | |
# - when Kindle is not connected, it will print to the local folder and is transfered as soon as Kindle is connected | |
# SETUP: | |
# 1. Create a local Kindle folder. Default is: | |
# ~/Desktop/Kindle | |
# | |
# 2. Set your CONFIGURATION options below | |
# | |
# 3. Save as .kindle_export.rb to your home folder | |
# | |
# 4. Set up a cron job (if you want automatic transfers): | |
# crontab -e | |
# */5 * * * * ruby ~/.kindle_export.rb >/dev/null 2>&1 | |
# | |
# 5. Download the "Print to Kindle" Automator action at: | |
# http://www.feldpost.com/mementos/assets/print_to_kindle.zip | |
# | |
# 6. Unzip and drag Automator action to: | |
# ~/Library/PDF Services | |
# | |
# TODO: | |
# | |
# If a file is unsupported by the Kindle, it should be automatically emailed | |
# to the free Kindle conversion service. | |
CONFIGURATION = { | |
# a local folder or drive that you will use to store your Kindle-bound documents | |
:local_kindle_folder => "/Users/#{ENV['USER']}/Desktop/Kindle", | |
# The username you have set up for your kindle email account | |
:kindle_email_name => '', | |
# Your kindle drive -- in most cases the kindle will mount to /Volumes/Kindle | |
:kindle_drive => '/Volumes/Kindle' | |
} | |
module Kindle | |
class Export | |
require 'fileutils' | |
SUPPORTED_FILE_FORMATS = { | |
:documents => ['mobi', 'prc', 'pdf', 'txt', 'azw', 'azw1'], | |
:music => ['mp3'], | |
:audible => ['aa', 'aax'] | |
} | |
def self.run(source_file,options) | |
export = new(options) | |
export.prepare(source_file) if source_file | |
export.run | |
end | |
def initialize(options) | |
@kindle_drive = options[:kindle_drive] | |
@local_kindle_folder = options[:local_kindle_folder] | |
@kindle_email_name = options[:kindle_email_name] | |
end | |
def prepare(source_file) | |
file_name = File.basename(source_file) | |
FileUtils.mv source_file, File.join(@local_kindle_folder,file_name), :force => true | |
end | |
def run | |
return false unless kindle_is_connected? | |
process_files if files_to_be_processed? | |
end | |
protected | |
def kindle_is_connected? | |
File.directory? @kindle_drive | |
end | |
def process_files | |
files_to_be_processed.each do |file| | |
transfer_to_kindle_or_email_for_conversion file | |
end | |
end | |
def files_to_be_processed | |
@files_to_be_processed ||= Dir.entries(@local_kindle_folder).reject {|f| f.match(/^\./) }.map {|file| File.join(@local_kindle_folder,file) } | |
end | |
def files_to_be_processed? | |
!files_to_be_processed.empty? | |
end | |
def transfer_to_kindle_or_email_for_conversion(file) | |
target_folder = map_folder_to File.extname(file) | |
if target_folder | |
FileUtils.mv file, target_folder, :force => true | |
else | |
send_file_for_email_conversion file | |
end | |
end | |
def send_file_for_email_conversion(file) | |
begin | |
#TODO: send email to free.kindle.com for conversion | |
rescue | |
#emailing didnt work | |
ensure | |
# at this point we should probably delete or move the file, so it won't try to process at every execution | |
# FileUtils.rm file | |
end | |
end | |
def map_folder_to(file_extension) | |
return nil unless file_extension | |
file_extension = file_extension.delete('.') | |
folder = SUPPORTED_FILE_FORMATS.find(proc{[]}) { |folder_map| folder_map[1].include?(file_extension) }[0] | |
folder ? File.join(@kindle_drive,folder.to_s) : nil | |
end | |
def conversion_email | |
'%[email protected]' % @kindle_email_name | |
end | |
end | |
end | |
Kindle::Export.run(ARGV[0],CONFIGURATION) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Kindle Scribe uses MTP instead of USB bulk storage for file transport; thus this script will not work on MacOS. From the Mac, can only use email to device or the Kindle USB File Manager app.