Using Applescript to send articles to my Kindle

I’m constantly finding new ways to use my Kindle e-book reader. Lately, I’ve found a way to save eye strain and trees when reading long magazine articles posted online. Instead of reading them on my laptop, and making my eyes watery and tired, or printing them out on a zillion pieces of paper, wasting all that tree pulp, I send them to my Kindle and get the best of all possible worlds.

Initially, I’d copy the text of each article, open a blank file in TextEdit, paste in the article, attach that file to an email, and send the email to my unique Kindle address. Amazon converts the file to a Kindle-readable format and sends it to my Kindle. Score.

But Apple includes a free scripting program with OS X called, logically enough, AppleScript. After a lot — way too much in fact — of grunt work figuring out the correct scripting vocabulary and so on, I wrote a script that works. Basically, it’s an adaptation of this sample from MacOSXHints for saving web text with Apple’s own create email message script.

Now all I do is highlight the text of an article and click on the script. It does all the rest of the work, creating a file and emailing it to my Kindle. Here’s the script:

tell application “Safari”

set selecTxt to (do JavaScript “(getSelection())” in document 1)

set theurl to (get URL of document 1)

set pgTitl to (do JavaScript “document.title” in document 1)

end tell

set dat to (current date) as text

set clipFil to (path to desktop folder as text) & “ToKindle.txt”

try

close access file clipFil

end try

set filRef to open for access file clipFil with write permission

write (dat & return & (theurl) & return & (pgTitl) & return & “——————-“ & return & selecTxt & return & “~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~” & ¬

return & return) to filRef starting at eof

close access filRef

tell application “Mail”

set newMessage to make new outgoing message with properties {subject:”Convert”, content:”Please convert and send” & return & return}

tell newMessage

set visible to true

set sender to “you@youremail.com”

make new to recipient at end of to recipients with properties {address:”yourkindle@kindle.com”}

make new attachment with properties {file name:alias “LunarMod:Users:gravitate:Desktop:ToKindle.txt”} at after the last paragraph

end tell

end tell

One bit you will need to customize is the location of the file ToKindle.txt. As you can see, that’s listed in my script as “LunarMod:Users:gravitate:Desktop:ToKindle.txt”. You’ll need to change “LunarMod” to the name of your hard drive and “gravitate” to your logon name. And, of course, fill in the proper the email address you’re sending from and your Kindle’s special email address (which you can find on Amazon’s web site under the account settings of your Kindle if you don’t know it).

There are a couple of annoying limitations. I haven’t figured out a way to give the created file a unique name each time I run the script. So after the script runs, you have to delete the ToKindle.txt file from your desktop. If you don’t Applescript will append the article you want to read onto the article you read last time you ran the script.

Also, there’s little info about running Applescript with Firefox 3.0 so the script only runs with Safari. As always, I’m happy to hear suggested improvements or criticism in the comments.

  • http://casdra.com/blog Mike Houser

    Thanks for this great script. After banging my head against it for an hour I got it to create and send a file with the name of the web page. You can adapt this to yours I think. The key is storing the path to the file as an alias. Thank again.

    tell application “Safari”
    set selecTxt to (do JavaScript “(getSelection())” in document 1)
    set theurl to (get URL of document 1)
    set pgTitl to (do JavaScript “document.title” in document 1)
    end tell
    set dat to (current date) as text
    set clipFil to (path to desktop folder as text) & “to kindle:” & pgTitl & “.txt”
    try
    close access file clipFil
    end try
    set filRef to open for access file clipFil with write permission
    write (dat & return & (theurl) & return & (pgTitl) & return & “——————-” & return & selecTxt & return & “~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~” & ¬
    return & return) to filRef starting at eof
    close access filRef
    set attachPath to (“Macintosh HD:Users:username:Desktop:to kindle:” & pgTitl & “.txt”) as alias
    tell application “Mail”
    set newMessage to make new outgoing message with properties {subject:”Convert”, content:”Please convert and send” & return & return}
    tell newMessage
    set visible to true
    set sender to “your@email”
    make new to recipient at end of to recipients with properties {address:”yourkindleemail@Kindle.com”}
    make new attachment with properties {file name:attachPath} at after the last paragraph
    end tell
    send newMessage
    end tell

  • Susan Rubendall

    This is a wonderful idea. I've had a lot of fun working with both scripts but don't have either of them working quite right. Aaron's script fails to attach the file. Mike's doesn't save the file on the desktop so the script fails to find it. Obviously, I've made errors in both scripts since they ran for you.
    Is this line right as is:
    set clipFil to (path to desktop folder as text) & “to kindle:” & pgTitl & “.txt”
    or does it need to be
    set clipFil to (path to desktop folder as text) & “to kindle:” & pgTitl & “.txt”

    I'd love to get this working, but if you don't have time to help an individual, I understand completely.

  • http://gravitationalpull.net/wp/ ampressman

    Susan – there's a weird delay at the very end of my script, after the
    new email message has been created but before the tokindle.txt file is
    attached. At first, I thought it wasn't attaching, but I was just
    interrupting the script too soon when it wasn't finished.

  • Pingback: Gravitational Pull - Instapaper’s instantly useful for sending articles to Kindles()

  • Pingback: Gravitational Pull on the Kindle and Instapaper | TeleRead: Bring the E-Books Home()

  • Adam Gerson

    tell application “Safari”
    (* Get a reference to the document *)
    set myDoc to document of front window

    (* Get the source of the page *)
    set mySrc to source of myDoc

    (* Get a file name *)
    set pgTitl to (do JavaScript “document.title” in document 1)
    set myName to pgTitl & “.html” — the # will be modified later

    (* Make sure there are no “:” in our file name *)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to “:”
    set myName to text items of myName
    set text item delimiters of AppleScript to “-“
    set myName to “” & myName
    set text item delimiters of AppleScript to prevTIDs

    tell application “Finder”
    (* Get a path to the front window *)
    set myPath to “Macintosh HD:kindle:” as string

    (* Get a file path *)
    set filePath to myPath & myName

    (* Create a brand new file *)
    set openRef to open for access (myPath & myName) with write permission

    (* Save the document source *)
    write mySrc to openRef

    (* Close the file *)
    close access openRef
    end tell
    end tell

    tell application “Mail”
    set newMessage to make new outgoing message with properties {subject:”Convert”, content:filePath & return & return}
    tell newMessage
    set visible to true
    set sender to “you@youremail.com”
    make new to recipient at end of to recipients with properties {address:”yourkindle@kindle.com”}
    set attachPath to filePath as alias
    make new attachment with properties {file name:attachPath} at after the last paragraph
    end tell
    end tell

  • Adam Gerson

    Nevermind, that saved it as html which the kindle shows as HTML not as the rendered webpage. How can we save it as txt?

  • Adam Gerson

    tell application “Safari”
    (* Get a reference to the document *)
    set myDoc to document of front window

    (* Get the source of the page *)
    set mySrc to source of myDoc

    (* Get a file name *)
    set pgTitl to (do JavaScript “document.title” in document 1)
    set myName to pgTitl & “.html” — the # will be modified later

    (* Make sure there are no “:” in our file name *)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to “:”
    set myName to text items of myName
    set text item delimiters of AppleScript to “-“
    set myName to “” & myName
    set text item delimiters of AppleScript to prevTIDs

    tell application “Finder”
    (* Get a path to the front window *)
    set myPath to “Macintosh HD:kindle:” as string

    (* Get a file path *)
    set filePath to myPath & myName

    (* Create a brand new file *)
    set openRef to open for access (myPath & myName) with write permission

    (* Save the document source *)
    write mySrc to openRef

    (* Close the file *)
    close access openRef
    end tell
    end tell

    tell application “Mail”
    set newMessage to make new outgoing message with properties {subject:”Convert”, content:filePath & return & return}
    tell newMessage
    set visible to true
    set sender to “you@youremail.com”
    make new to recipient at end of to recipients with properties {address:”yourkindle@kindle.com”}
    set attachPath to filePath as alias
    make new attachment with properties {file name:attachPath} at after the last paragraph
    end tell
    end tell

  • Adam Gerson

    Nevermind, that saved it as html which the kindle shows as HTML not as the rendered webpage. How can we save it as txt?

  • Pingback: The Definitive Kindle Guide for College Students()

  • Pingback: The Definitive Kindle Guide for College Students - CollegeTimes™()

  • http://www.therichjanitorreviews.com the rich janitor

    Hello, As usual, your info and thoughts are organized, good article.

  • George

    Thanks for this!  One quick tip:  Instead of using a hardcoded string to specify where the ToKindle.txt file goes, you could use (((path to desktop) as string) & “ToKindle.txt”) instead.  This version is username-agnostic, so it works on every system and username with no modification needed!