Create timestamped duplicates of files via AppleScript

Sep 29, '08 07:30:00AM

Contributed by: PizzaCake

I sometimes modify and save a file, only then to realize I need to undo something, but it's too late. So I've created an AppleScript which duplicates any files selected in the Finder, and adds the date and time to their names. This makes it easy to go back by looking at the date and time embedded in the filename. I suggest you work with the original file, and make versions of that one before modifying the file, so you can always go back to your starting point.

Here's the AppleScript (note that some date and time code was sourced via a Google search):

property extensionhidden : true

tell application "Finder"
    set theselection to selection
    repeat with i from 1 to (count theselection)
        set folderCheck to item i of theselection
        if folder (folderCheck as text) exists then
        else
            set extensionhidden to extension hidden of (item i of theselection)
            set extension hidden of (item i of theselection) to true
        end if
        set theItemName to displayed name of (item i of theselection)
        if folder (folderCheck as text) exists then
        else
            set theItemExtension to name extension of (item i of theselection)
        end if
        set upOneLevelFolder to folder of (item i of theselection) as alias
        
        set {year:y, month:m, day:d, time string:t} to (current date)
        set date_format to (y * 10000 + m * 100 + d) as string
        set time_format to (t) as string
        set t to (do shell script "echo " & "'" & t & "'" & " | sed  's/://g' ")
        set thedate to date_format & " " & t as text
        if folder (folderCheck as text) exists then
            set theNewName to theItemName & " " & thedate as text
        else
            set theNewName to theItemName & " " & thedate & "." & theItemExtension as text
        end if
        log theNewName
        set theDuplicate to duplicate (item i of theselection) to upOneLevelFolder replacing no
        set theDuplicateAlias to theDuplicate as alias
        set name of theDuplicateAlias to theNewName
        if folder (folderCheck as text) exists then
        else
            if extensionhidden then
                set extension hidden of theDuplicateAlias to true
                set extension hidden of (item i of theselection) to true
            else
                set extension hidden of theDuplicateAlias to false
                set extension hidden of (item i of theselection) to false
            end if
        end if
    end repeat
end tell
Paste into Script Editor, and save as application. Select some file/s in the Finder, then run the saved AppleScript.

[robg adds: This worked as described.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080917034034587