Upload File Selection Via Transmit

September 23, 2011 by Gabe | [mmd] |

 

In the never-ending pursuit of hard-won laziness, I often develop scripts to do simple tasks. Sometimes the joy is in the problem solving and sometimes it's in the final product. In this case it was in both.

Yesterday I was working on an as yet to be published post that has a large number of images. My typical workflow is to make an annotation in the text of the image I want to insert. I then transfer the html to MarsEdit and begin the image insertion process. I quickly decided that there had to be a better way to upload a bunch of images and grab their url at that same time.

Here's what I came up with. It's an AppleScript that uses Transmit to FTP the current Finder selections to my Wordpress content directory for the current year and month and give back the exact url's to the files. It's not that pretty or efficient. What I like about it is how it shows just how flexible and powerful AppleScript can be. In one script, there is AppleScript doing most of the integration and basic logic, with a shell script to do some date conversion and finally python to perform the url encoding of the file name.

The following script could be triggered by FastScripts, Keyboard Maestro, LaunchBar or Alfred. That's the real beauty of scripting on Mac OS.

There are a couple of assumptions for this script to work. First, you must have Transmit from Panic Software. If you need any kind of FTP access, quit messing around and go buy it.

Second, you need to save your ftp location as a favorite in Transmit. Using favorites in Transmit is nice, because you can have all of your login credentials securely handled by Transmit and omit them from an scripts you may publish to some silly blog.

[cc lang="AppleScript"](* This is my WordPress upload root *) set urlPath to "http://www.macdrifter.com/uploads.html" (* This will set the upload path to year/month/ *) set myMonth to do shell script "date +\"%m\"" set myYear to 1 * (year of (current date)) set clipContents to {} tell application "Finder" set these_items to the selection set myFolder to (POSIX path of (target of the front window as alias)) end tell tell application "Transmit" (* Find the favorite for the FTP Site *) set myFav to item 1 of (favorites whose name is "Macdrifter_WP") tell current tab of (make new document at end) (* Connect to the favorite *) connect to myFav change location of remote browser to path "/wp-content/uploads/" & myYear & "/" & myMonth & "/" change location of local browser to path myFolder (* Process all of the finder selections *) repeat with i from 1 to the count of these_items set this_item to (item i of these_items) as alias set this_info to info for this_item set myPath to (POSIX path of this_item) set myFileName to name of this_info as text (* If we want to use the files in a blog post we need the URL's. But we need the URL's encoded because we may have spaces in the file names *) set myURLFileName to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of myFileName set myURLFileName to urlPath & myYear & "/" & myMonth & "/" & myURLFileName set end of clipContents to " " & myURLFileName (* Upload the files. Duplicates will require user interaction. That's safer *) tell remote browser upload item at path myPath to "/wp-content/uploads/" & myYear & "/" & myMonth & "/" end tell (* Enable if we want the window to close when the deed is done *) (* close remote browser *) end repeat end tell end tell (* Convert our list to a single block of text *) set AppleScript's text item delimiters to " " set clipContents to clipContents as string return clipContents[/cc]

 

blog comments powered by Disqus