Problem with creating folders based on file creation dates

The software I’ve been using for transferring files from camera cards to my hard drive just won’t work in Lion. I’ve been using it for 4 years, so I don’t really want to change my filing system. Essentially what I use it for (it does other things too, but I only use it for one thing) is to take files on a card and sort them into folders on my hard drive in a specific format that is easy to find things in based on file creation dates. An example folder name would be 2011_08_22, which would have all the photos shot on August 22, 2011 in it. I recently upgraded my camera to one that shoots video as well, and would like to sort the videos into a separate set of folders that is excluded from my Time Machine backups.

I’ve gotten most of the way in creating a script that replicates that functionality, but I’m hitting a snag. Every time I run it, I get part way through and it stops with an error “Can’t make “2011_8_27” into type integer.”

It errors out on the “if not (exists folder folderDate of toPath) then” line.

Any ideas? I can’t for the life of me find out where or why it’s trying to convert my perfectly good string into an integer anyway.


set chooseString to "Select a folder containing images to be sorted"
set FilePath to (choose folder with prompt chooseString)
set toPath to "Drobo:Photos" as string
set moviesPath to "Drobo:Movies" as string
set photoList to {"CR2", "JPG"}
set movieList to {"MOV", "MP4"}

tell application "Finder"
	set FileList to every file of folder FilePath as alias list
	repeat with aFile in FileList
		set myDate to creation date of aFile
		set folderDate to (my dateFormat(myDate))
		if name extension of aFile is in photoList then
			tell application "Finder"
				if not (exists folder folderDate of toPath) then
					make folder of toPath with properties {name:folderDate}
					set photoPath to ((toPath & folderDate as string) as alias)
				end if
				duplicate aFile to photoPath
			end tell
		else if name extension of aFile is in movieList then
			tell application Finder
				if not (exists folder folderDate of toPath) then
					make folder of moviesPath with properties {name:folderDate}
					set moviePath to ((moviesPath & folderDate as string) as alias)
				end if
				duplicate aFile to moviePath
			end tell
		end if
	end repeat
end tell

on dateFormat(theDate)
	set stringDate to theDate as string
	set myMonth to month of theDate as number
	set myYear to year of theDate as number
	set myDay to day of theDate as number
	set newDate to ((myYear as string) & "_" & (myMonth as string) & "_" & (myDay as string))
	return newDate
end dateFormat

Hi,

I’m wondering that the script works at all.
¢ The alias FilePath is double referenced with an additional folder specifier in this line


 set FileList to every file of folder FilePath as alias list

¢ the quotes around Finder are missing in this line


 tell application Finder

¢ In all lines which refer to folder toPath (or moviesPath) the specifier folder is missing
¢ In the movie part of the script the movie will be copied also to the photo folder (toPath).

Not big problems, but these issues can cause unexpected behavior
¢ The correct syntax to create a folder in Finder is actually make new folder at.
¢ Nested application tell blocks should be avoided.

This is a shorter version which uses the ditto shell command. It copies and creates intermediate directories automatically if necessary




set chooseString to "Select a folder containing images to be sorted"
set folderPath to (choose folder with prompt chooseString)
set toPath to "Drobo:Photos:"
set moviesPath to "Drobo:Movies:"
set photoList to {"CR2", "JPG"}
set movieList to {"MOV", "MP4"}

tell application "Finder" to set FileList to every file of folderPath
repeat with aFile in FileList
	tell aFile to set {nameExtension, creationDate, FilePath} to {name extension, creation date, it as text}
	tell creationDate to set folderDate to (year as text) & "_" & (its month as integer) & "_" & day & "/" -- AppleScript coerces to text implcitly
	if nameExtension is in photoList then
		do shell script "ditto " & quoted form of POSIX path of FilePath & space & quoted form of (POSIX path of toPath & folderDate)
	else if nameExtension is in movieList then
		do shell script "ditto " & quoted form of POSIX path of FilePath & space & quoted form of (POSIX path of moviePath & folderDate)
	end if
end repeat

Thanks for the reply, Stefan. I knew there was a more efficient way to do it with a shell script, but I just haven’t been able to wrap my head around them quite yet. Your script seems to work well, other than a misidentified variable (you wrote moviePath instead of the defined moviesPath, which was easy enough to correct.)

It’s probably not going to be an issue for me, since I download all my pictures after every shoot, or if I’m on vacation, have at most 10 days of folders to create, but would it also be more efficient to use a shell script to create the folders?

Another inconsistency I’ve noticed in both of our scripts vs. the Canon software is that the date format Canon uses would be something like 2011_08_22, vs. 2011_8_22. It’s not a big deal as everything still sorts the same, but is there an easy (ie, less than 3 lines of code) fix for that?

This was really just kind of a sloppy first draft. I had intended to clean up variable names and organize it better once I got basic functionality working. You keep telling me not to nest my application Tell blocks, and one of these days I’m going to listen :stuck_out_tongue: I still can’t figure out why my original script would be trying to coerce to an integer though.

One more thing. Does the “ditto” command replace existing files? One of the things I had intended to build in later was a “without replacing” on the duplicate line. I sometimes add metadata into my original files before my normal editing/save as procedure, and it would be slightly annoying to have to do it more than once.

I tested only the photo part :wink:

this version adds the leading zeros



set chooseString to "Select a folder containing images to be sorted"
set folderPath to (choose folder with prompt chooseString)
set toPath to "Drobo:Photos:"
set moviesPath to "Drobo:Movies:"
set photoList to {"CR2", "JPG"}
set movieList to {"MOV", "MP4"}

tell application "Finder" to set FileList to every file of folderPath
repeat with aFile in FileList
	tell aFile to set {nameExtension, creationDate, FilePath} to {name extension, creation date, it as text}
	tell creationDate to set folderDate to (year as text) & "_" & my pad(its month as integer) & "_" & my pad(day) & "/" -- AppleScript coerces to text implcitly
	if nameExtension is in photoList then
		do shell script "ditto " & quoted form of POSIX path of FilePath & space & quoted form of (POSIX path of toPath & folderDate)
	else if nameExtension is in movieList then
		do shell script "ditto " & quoted form of POSIX path of FilePath & space & quoted form of (POSIX path of moviesPath & folderDate)
	end if
end repeat

on pad(integerValue)
	return text -2 thru -1 of ("00" & integerValue as text)
end pad

ditto always overwrites existing files.
To have an option you better use a combination of mkdir (make directory) and cp