Automator service to create random dropbox link and email it

I use dropbox a lot to share files and a recurring task is to copy a file into the dropbox public folder, create a link and email it off. Additionally, I like using Merlin Mann’s trick of zipping a file, renaming it to something obscure and sending that link. Recently, I found  Brian Hayes’s trick (via Macsparky) to streamline the process using Textexpander.

I wanted to adopt it to my workflow and created an automator service that I can run in the finder, with a file selected. It does the following:

  1. zip the file
  2. rename the zip to something obscure, using Brian’s code
  3. move the zip to the dropbox public folder
  4. create a link to that file, using this code from Chez Moncef, the dropbox forum and stackoverflow
  5. create a new email in mail.app and put the link into the body

To use this service for your needs, you need to edit two values:

You can save the service and access it in the finder by right-clicking on any file. I combine this with a Hazel rule that cleans out my public folder after a certain time. If you use this a lot you could set up a keyboard shortcut for the service in the keyboard preference pane.

Here is what the automator service looks like:

And here is the applescript:

on run {input, parameters}

set str to {}
set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
repeat 12 times
set end of str to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZa;rndbcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()_+-=[]>?;:'"
end repeat

set randString to str as string

tell application "Finder"
set theFile to file input
set theNewName to (randString & ".zip")
set the name of file input to theNewName

(*Change the dropbox path if necessary*)
set dest_folder to folder "Documents:Dropbox:Public" of home
move file input to dest_folder
end tell

(*Change YOUR_DROPBOX_ID to your unique dropbox id*)
set the theURL to ("http://dl.dropbox.com/u/YOUR_DROPBOX_ID/" & my text2url(theNewName))
set the clipboard to theURL as text

tell application "Mail"
activate
make new outgoing message with properties {content:the clipboard, visible:true}

end tell
return input
end run

on text2url(thetext)
return do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of thetext
end text2url