Use Automator to automatically FTP images

One of the common steps I have to take with writing posts is getting images pre-loaded to my site so the all the image links can be set properly as I write in my Byword. [1] For a while I have wanted a way to automatically FTP images to my site and I finally have a working system. [2] Here are the steps to create your own system.

Software needed:

You will need the following on your Mac:

  • OS X Automator (built in to current OS X versions) [3]
  • FTP client [4]
  • Dedicated FTP folder on your site

Prep Work

There is a couple of things to make sure you have done or may need to do. Review this quick checklist before opening Automator.

  • Determine the folder you will use on your Mac as your default upload folder. This folder will be the folder Automator will monitor to upload images to your site.
  • Determine how/where you want to save the images to your site. This could be a subdomain like http://share.example.com or a dedicated folder like http://example.com/images. Just think about how you want this done because you're committing to a strategic process for your site.
  • Make sure your FTP account credentials are correct. [5]

Automator

The automatic portion of the work is utilizing a Folder Action via Automator. Use the following steps to create the Folder Action in Automator.

  1. Open Automator and choose the Folder Action item. This will create a blank Folder Action[6] for you.

  2. Set the folder to the default upload folder you determined as part of your prep work. Mine happens to be ~/Pictures/ftp_upload/.

  3. Start adding the following Actions in order:
    • Replace Text
    • Add Date or Time
    • Get Folder Contents
    • Copy to Clipboard
    • Upload Files (using FTP client action)
    • Run Applescript (first script)
    • Run Applescript (second script)
  4. Fill in each of the Actions with the following. Note that on the Replace Text action, the 'Find:' box has a space in it. The idea is rename the image as one continuous string to avoid the unicode %20 filename fillers. [7]

  5. In the first Run Applescript action, the 2nd line, set trimFilename to ((characters 40 thru -1 of theFilename) as string) is used to strip the folder path from the file name. You will need to adjust the first numeric value (40 in my example) so only the name of the file is left in the string. The idea is to take the first 40 characters from:

    /Users/mygeekdaddy/Pictures/ftp_upload2/IMG_7819-Edit.jpg
    
    And leave just:
    IMG_7819-Edit.jpg
    
    Use the following Applescript to test your string trim setting.[8]
    set theFilename to the clipboard
    set trimFilename to ((characters 40 thru -1 of theFilename) as string)
    return trimFilename
    
  6. In the second Run Applescript action, to easily get the correct path syntax, do the following sub-tasks:[9]
    1. Open the Applescript editor on one side of your screen and Finder on another.

    2. In the Applescript editor, click on Record button.

    3. In Finder window, under Devices, click on your Mac computer name and then click through the folders until you open the default ftp folder where you'll copy the images to on your Mac.

    4. When you get your ftp upload folder in Finder, click stop in the Applescript editor.

    5. Now you will have an Applescript method to access your ftp upload folder.

    6. Copy the last set target statement and use that as the syntax inside the second Run Applescript action for the Action Folder to set the folder in which the image files should be sent to the Trash.

Now you can test your Action Folder by pasting an image into your selected ftp upload folder. If you keep Finder open to your ftp upload folder you can literally watch as the magic happens. After the Automator action runs, you can just hit paste in your favorite editor and get the URL of the image you just uploaded to your site:

http://share.mygeekdaddy.me/some_random_image_2013-07-06.png

Got any questions? Feel free to hit me up on Twitter at @MyGeekDaddy.

  1. All my posts are written MD and converted to HTML.  ↩
  2. Working != most efficient. Improvements are very likely to be made.  ↩
  3. From my research, this WF should work as far back as Leopard (v10.5.x).  ↩
  4. I'd recommend Fetch of Transmit, both are known to play nicely with Automator.  ↩
  5. Make a mental note that updating or changing your FTP account credentials will break this Automator action.  ↩
  6. Folder Actions will be saved in ~/Library/Workflows/Applications/Folder Actions, unless changed when first created.  ↩
  7. The idea is to change My Image Name.png to My_Image_Name.png, instead of My%20Image%20Name%20.png.  ↩
  8. Copy the full path and file name (e.g. /Users/mygeekdaddy/Pictures/ftp_upload2/IMG_7819-Edit.jpg) and test the code in the Applescript editor to get it right.  ↩

Comments

Top