How Not To Have To Right-Click a Hundred Photos

First off, let me disclaim one thing: I’m not a developer at Redfin. I’m actually just a member of the product management and design team. I have been a dev in my past life, but I’m rusty to say the least.  Even though it’s not part of my job, I still love to code a little on the side when I can.  I’m always afraid my coding muscles will atrophy and I’ll end up being like this guy from the movie Beetlejuice.   And, well, that wouldn’t be a good look for me.

If you’re a Redfin fan, you can rest easy though.  My code is usually either for internal use or just some automation to complete a task.  It’s almost never something you’d interact with on the website or our mobile apps.  I’m not sure I’d let me do that and I’m definitely sure our developers would be rightfully concerned if I did.

The Problem

Just this past week, I had a task to accomplish:  I needed to download just over 2,000 photos from our website to a local directory with a very specific naming convention on the final file. This was for an internal project, so it wasn’t worth distracting a real developer with so I decided to tackle it myself.

Google turned up a few folks who had written single calls to download a file using CURL and Bash, but I didn’t find one source that had all the pieces.  So, using Applescript, I wrote a small Mac OS X application that accomplished what I needed.  I probably used a trick or two I found via all that Googling, so I figured I’d better pay it forward and post my solution in case someone else is in my boat.

The Solution

My app takes in a text file that has one row for each photo to get.  Each line contains just two things:  the photo source URL and the exact output file name for the local copy.  It then loops over that list and downloads that URL in the file name specified in the row.  If that local file already exists, it skips it and moves to the next one.  I had some issues with some of my output file names around row 500 that made the app exit.  But since it skips the ones it already has, you can just restart it from the beginning and it will get to where it left off super quickly.

And without further ado, here’s the full script for your downloading pleasure:

   on theSplit(theString, theDelimiter)
	-- save delimiters to restore old settings
	set oldDelimiters to AppleScript's text item delimiters
	-- set delimiters to delimiter to be used
	set AppleScript's text item delimiters to theDelimiter
	-- create the array
	set theArray to every text item of theString
	-- restore the old setting
	set AppleScript's text item delimiters to oldDelimiters
	-- return the result
	return theArray
  end theSplit

  on run
	tell application "Finder"
		set Names to paragraphs of (read (choose file with prompt ¬
   "Pick text file containing emails and output file names"))
		set myFilePath to path to me
		set myFolder to (container of myFilePath) as string

		set nTotalRows to count of Names
		set nCurrentRow to 0
		set nCount to 0

		repeat with nextLine in Names
			if length of nextLine is greater than 0 then
				--Do something with the next name, which is stored in "nextLine"
			end if
			set nCurrentRow to nCurrentRow + 1

			set myArray to my theSplit(nextLine, " ")
			if (count of myArray) is greater than 0 then
				set itemURL to item 1 of myArray
				set itemfileName to item 2 of myArray
				set itemFullPath to POSIX path of myFolder & itemfileName

				set msg to 0
				tell application "Finder" to if exists (itemFullPath as POSIX file) ¬ 
                                                  then set msg to 1
				if msg = 0 then
					set strCommand to "curl " & quoted form of itemURL ¬ 
                                                  & " -o " & quoted form of itemFullPath
					do shell script strCommand
					set nCount to nCount + 1
				end if
			end if
		end repeat

	end tell
  end run


I’m sure there’s probably some more elegant version that could have been done, but as I said, this isn’t my day job. This worked great for my 2,000+ photo download so I was satisfied, but I’m always up for learning how to be better. But if you have a tweak to the script that would make it better, please let us know!

Let us know if you found it useful as well!