renaming finder items introduced a couple of ways to batch-rename files in the finder — using automator or applescript — by replacing characters or removing parts of the file name. renaming finder items II showed how to map a bunch of files to a completely different set of file names.
this post will show another way to rename files — based on their creation date — using applescript. this workflow came about due to the diabolical way that digital cameras name their files. here’s a screen grab of a typical bunch of images :

for this workflow, we’d like the order of the filenames to match the date the photo was taken — to make sorting, selecting, archiving, etc. just a little easier. the current naming is made more problematic because files are coming from more than one camera. well, we don’t have to put up with that crap — let’s just rename them.
set mgFolder to choose folder
tell application "Finder"
set mgFiles to items of mgFolder
end tell
repeat with mgFile in mgFiles
set mgPath to POSIX path of (mgFile as string)
set mgCreation to creation date of mgFile
set mgDate to short date string of mgCreation
set mgTime to time string of mgCreation
-- rest of script here
end repeat
first we get the user to select a folder of images to process, then we get references to the files in that folder, then we start a repeat loop to process each file in turn. we capture the following information :
mgCreation – for example “Thursday, 4 October 2012 17:13:18 ” – from which we extract :
mgDate – “04/10/12” (that’s dd/mm/yy) and
mgTime – “17:13:18 ” (that’s hh/mm/ss)
if you find that mgDate or mgTime give you the data in a different format, you will need to update the next part of the script accordingly or, if you prefer, change your language and text system preferences :

we’re going to use mgDate and mgTime to create a file name like this “yymmdd_hhmmss” — so, we need to strip out the slashes and colons, reverse the order of the date, and introduce an underscore :
set text item delimiters of AppleScript to ":"
set mgTime to text items of mgTime --gives {"17", "13", "18 "}
set text item delimiters of AppleScript to "/"
set mgDate to reverse of text items of mgDate --gives {"12", "10", "04"}
set text item delimiters of AppleScript to ""
set mgDate to mgDate as string --gives "121004"
set mgTime to mgTime as string --gives "171318 "
set mgTime to text items 1 thru 6 of mgTime as string
set mgNewName to mgDate & "_" & mgTime
this uses the handy “reverse of” command — taking a list of items and rearranging them in reverse order. also notice the second last line — we need that because mgTime has a trailing space we need to get rid of. this method works no matter how many trailing spaces there are because we’re saying “just give me the first six characters and ditch the rest”.
now, that’s the bulk of the script done — we also need to capture the extension of the file (mgExtension), create a filepath for the renamed file (mgFinalPath), and then do the renaming with a shell script. the shell script basically says — move this file, without any prompts, from this filepath to that filepath (you could also use this method to move the file to a completely different location — but we’re just renaming the file and leaving it in the same location) :
danger : do not run this next script — it is for demonstration purposes only – you’ve been warned!
set mgFolder to choose folder
tell application "Finder"
set mgFiles to items of mgFolder
end tell
repeat with mgFile in mgFiles
set mgPath to POSIX path of (mgFile as string)
set mgCreation to creation date of mgFile
set mgDate to short date string of mgCreation
set mgTime to time string of mgCreation
set text item delimiters of AppleScript to ":"
set mgTime to text items of mgTime
set text item delimiters of AppleScript to "/"
set mgDate to reverse of text items of mgDate
set text item delimiters of AppleScript to "."
set mgExtension to text item -1 of (mgFile as string)
set text item delimiters of AppleScript to ""
set mgDate to mgDate as string
set mgTime to mgTime as string
set mgTime to text items 1 thru 6 of mgTime as string
set mgNewName to mgDate & "_" & mgTime
set mgFinalPath to mgFolder & mgNewName & "." & mgExtension as string
do shell script "mv -f " & quoted form of POSIX path of mgPath & space & quoted form of POSIX path of mgFinalPath
end repeat
if we run this script on that original folder of images we’ll get this :

well, that looks pretty good at first glance — all the files have been renamed successfully … except … the very observant will notice that this folder has only 81 items, whereas the original folder had 103. wtf?
the problem is that it’s quite possible to have more than one file with a creation date of “04/10/12 17:13:18” — digital cameras can flick off quite a few images in a second. the script in its current form will simply write over a file with a duplicate filename. so we need to build in some method to check if a file with a particular name already exists and, if so, create a different name. here’s one way :
set mgFinalPath to mgFolder & mgNewName & "_1" & "." & mgExtension as string
tell application "Finder"
set x to 2
repeat
set mgExists to exists file mgFinalPath
if mgExists is true then
set mgFinalPath to mgFolder & mgNewName & "_" & x & "." & mgExtension as string
set x to x + 1
else
exit repeat
end if
end repeat
end tell
this time we’re appending “_1” to each filename. then the script enters a repeat loop to check if a file with that name already exists. if so, it will change the end to “_2” and then check that, incrementing by one on each pass through the loop. once it reaches a name that has not already been used it exits the repeat loop. this way we don’t lose any files :

this is the final form of the script. it’s always a good idea to test new scripts on duplicate files before implementing them into your workflow — just to make sure they do what you expect (and don’t do what you don’t expect) — you’ve been warned again :
set mgFolder to choose folder
tell application "Finder"
set mgFiles to items of mgFolder
end tell
repeat with mgFile in mgFiles
set mgPath to POSIX path of (mgFile as string)
set mgCreation to creation date of mgFile
set mgDate to short date string of mgCreation
set mgTime to time string of mgCreation
set text item delimiters of AppleScript to ":"
set mgTime to text items of mgTime
set text item delimiters of AppleScript to "/"
set mgDate to reverse of text items of mgDate
set text item delimiters of AppleScript to "."
set mgExtension to text item -1 of (mgFile as string)
set text item delimiters of AppleScript to ""
set mgDate to mgDate as string
set mgTime to mgTime as string
set mgTime to text items 1 thru 6 of mgTime as string
set mgNewName to mgDate & "_" & mgTime
set mgFinalPath to mgFolder & mgNewName & "_1" & "." & mgExtension as string
tell application "Finder"
set x to 2
repeat
set mgExists to exists file mgFinalPath
if mgExists is true then
set mgFinalPath to mgFolder & mgNewName & "_" & x & "." & mgExtension as string
set x to x + 1
else
exit repeat
end if
end repeat
end tell
do shell script "mv -f " & quoted form of POSIX path of mgPath & space & quoted form of POSIX path of mgFinalPath
end repeat
now, of course, you can run this script for any kind of file, not just images. but in its current form it has no error handling for if it encounters a folder. we’ve fixed it so it won’t have a hassle with duplicate files, but if it hits a folder you’ll get something like this :

dammit you say. but the fix is actually very simple — just change the line “set mgFiles to items of mgFolder” to “set mgFiles to files of mgFolder” and the folders will be skipped — no drama.
but that’s not all …
just to be safe, or because your workflow calls for it — you could change the script so that it makes a copy of the file — giving the new name to the copy and leaving the original untouched.
and again, the solution is very simple — change the shell script from move (mv) to copy (cp) :
do shell script "cp -f " & quoted form of POSIX path of mgPath & space & quoted form of POSIX path of mgFinalPath
you can get a copy of the RenameByDate app here
keep grunting
• related post : renaming finder items : renaming using automator or applescript.
• related post : renaming finder items II : renaming by list.
• related post : renaming finder items IV : easy renaming.