I need some Mac help here, solving a situation I find myself in fairly often.
If I have a folder full of files --let’s say they were jpegs–with more-or-less random file names, how can I rename them in chronological order?
I’d want the first file created (“alkcbfkj.jpg”) to be renamed “file1.jpg” and the second file created to be “file2.jpg”, etc.
I’m slightly familiar with the file renaming scripts (Replace text in file names, Append text to file names) available in the Finder Scripts pulldown, but don’t see anything which would let me rename a bunch of files in chronological order.
In days of yore, I used FileBuddy to perform that function, and more. The info says that it will “rename files based on patterns, sequences and dates”, which sounds like what you want.
Someone who remembers more about awk will be along in a moment to show you a one-line command from Terminal that will do the job, but here’s a quick applescript:
on run
set myfilenameprefix to "File"
set thefolder to choose folder
tell application "Finder"
set allthenames to name of every file in folder thefolder
set fcount to count allthenames
if fcount > 0 then
repeat with i from 1 to fcount
set curname to item i of allthenames as text
set newname to my renamefile(curname, i)
try
set name of file curname in folder thefolder to newname
on error
beep
end try
end repeat
end if
end tell
say "the mighty groo gave me permission to rename my files"
end run
on renamefile(aname, anumber)
global myfilenameprefix
set theout to my getextension(aname)
set thename to item 1 of theout
set theextension to item 2 of theout
set newname to myfilenameprefix & my leadingzeroes(anumber) & "." & theextension
return newname
end renamefile
on leadingzeroes(anint)
if anint > 100 then
set anout to "-" & anint as text
else if anint > 10 then
set anout to "-0" & anint as text
else
set anout to "-00" & anint as text
end if
return anout
end leadingzeroes
on getextension(afilename)
set AppleScript's text item delimiters to "."
set text_item_list to every text item of afilename
set AppleScript's text item delimiters to ""
set icount to count text_item_list
if icount = 0 then
beep
set theoutput to {afilename, ""}
else
if icount = 1 then
set theoutput to {afilename, ""}
else if icount = 2 then
set theoutput to {item 1 of text_item_list, item 2 of text_item_list}
else
set theoutname to item 1 of text_item_list
repeat with i from 2 to icount - 1
set theoutname to theoutname & "." & (item i of text_item_list)
end repeat
set theoutput to {theoutname, item icount of text_item_list}
end if
end if
return theoutput
end getextension
Note: I had getextension just lying around; this was about three minutes’ effort.
Open up Applications/Applescript/Script Editor, paste the text above into a new window, then just hit the “play” button on the Applescript editor tool bar. It’ll prompt for a folder name; select your folder and hit “OK.” Note: Change the string “File” to whatever you want as your “base” filename.
I think I’ll finally get around to reading up on Automator; ISTR it was designed precisely for functions like this.
Automator is easily the most powerful and useful piece of “unknown” software that Apple has released. It’s a brilliant and easy to use tool, and comes with every machine.