Upload Droplet

Date: 2008-11-02 11:52:02 Created: null

Upload is my very first AppleScript to actually do something useful, and it's highly based on template code available from Apple's site. My goal was to create a droplet that would upload files to my site. A droplet is an AppleScript that runs when you drag and drop a file on it, so using it to upload stuff seemed like a great thing. A shortcut directly to the site on the desktop, without needing to switch to Terminal (or whatever other FTP program one might be using) and re-enter login information all the time.

Anyway, I started with the template script for a droplet to process all files of certain types to be dropped onto it. The essential addition I had to come up with was the code to actually upload the files to the right place. In earlier, short-lived and unsuccessful, experiments with scripting I'd come across a way to execute shell commands and I figured that'd come in handy here. The linked Do Shell Script information page pointed me in the right direction, even providing the information that using curl instead of ftp was the right way to go. So I did. I jumped in and experimented in a terminal window until I came up with a curl line that uploaded a file the way I wanted it. Cut and paste into the script and replace with variables in the right places.

Beeep! Error!

Okay, another dive into the do shell script made me throw use the POSIX path (that means using the slashes unixes and similar things expect to separate directory names instead of the colons Mac OS otherwise uses) of the local filename. Now the final hurdle was the fact that the remote path got all screwed up. Since I wanted the files I dropped to have the same names on the server I didn't provide a remote filename. This makes curl use the local filename when storing the file on the server, just what I wanted. Or so I thought. The problem was that curl added on the whole specified filename to the remote path, and that included the local path to the file. So curl ended up trying to upload to a very strange path including a double slash, failing and getting understandably confused.

I figured there had to be a convenient property for a file that contained just the filename, sans path, and proceeded to look for info on such a property. Whatever it was I was unable to guess it, or find any information about it. Come on, there has to be one, right? In any case, I still haven't found it. My workaround was to create a new variable containing the path and filename and then go through it and discard everything but the actual filename. The path is split up using colons in Mac OS, so I just put the path into a new variable one character at a time and emptied the variable every time I got to a colon. A nice and fully functional solution, but surely there has to be an even more elegant way?

With that out of the way I suddenly found myself victorious, looking at my very first self-created and fully functional AppleScript! A minuscle or smaller step for mankind, but a small step for me! On to new challenges in the exciting world of programming and scripting on Mac OS!