Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create timestamped duplicates of files via AppleScript Desktop
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.]
    •    
  • Currently 3.33 / 5
  You rated: 5 / 5 (6 votes cast)
 
[11,250 views]  

Create timestamped duplicates of files via AppleScript | 9 comments | Create New Account
Click here to return to the 'Create timestamped duplicates of files via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create timestamped duplicates of files via AppleScript
Authored by: jbirk on Sep 29, '08 07:59:24AM

I was unclear about "select some files... then run the saved AppleScript" so I added this after the "property" line:
<code>
on open the selection
</code>

don't forget the "end open" at the end... now you've got a droplet!



[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: Andreas. on Sep 29, '08 09:22:28AM

If you don't enclose the whole script between "on open" and "end open" but just add

on open
    run
end open

you will have the best of both worlds!



[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: PizzaCake on Sep 29, '08 10:19:09AM

Thanks for pointing that out jbirk, Andreas. Now you can either select the files or drop them on the app and they will be duplicated.



[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: Black on Sep 29, '08 08:10:29AM

Of course, if this is really something that comes up often for you, you should look into using version control software - like say, Subversion. It doesn't matter is you aren't writing code, Subversion won't care. I use it all the time to manage the papers I write. Using proper version control will mean that you not only have timestamped old copies, but they can be accompanied by comments that describe the state of the document when it was committed. In addition, the overhead is smaller as it stores changes rather than copies, and if they are text files you can easily diff versions to see changes and a whole host of other cool tricks.

There is also Time Machine's hourly backups. Not quite as much control, but it is still a way to manage old copies. Basically, what I'm saying is that you have made the first step on the version control path (differentiating old versions intelligibly), but you can go quite a bit farther.



[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: drudus on Sep 29, '08 08:21:11AM

Bear in mind if you do go down Black's route of SVN that it has limitations. SVN does allow you to move & delete files, but only if you tell it so first. Otherwise you can end up ruining the repository with simple mistakes.
It also litters your directories with hidden .svn folders that need to be left alone.

GIT or Mercurial are some alternatives, but keeping it as simple as possible is always the best way.



[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: PizzaCake on Sep 29, '08 10:27:02AM

Nicely put Black and drudus. A succinct overview of the pros and cons of svn.



[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: Andreas. on Sep 29, '08 10:55:36AM
If you want to make the dateStamp a bit 'friendlier' you could append the filename with the output of

on createDateString()
    set theDate to current date
    set the_month to characters 1 thru 3 of (month of theDate as text) as string
    set {the_hour, the_minutes, ampm} to {hours of theDate, minutes of theDate, word -1 of (theDate as text)}
    if the_minutes < 10 then set the_minutes to "0" & the_minutes
    return "" & the_month & " " & day of theDate & "  " & the_hour & "." & the_minutes & ampm
end createDateString
or, say,
set dateStamp to do shell script "/bin/date '+%a %b %e  %H:%M:%S'"


[ Reply to This | # ]
Create timestamped duplicates of files via AppleScript
Authored by: osxpounder on Sep 29, '08 11:30:57AM

I do a similar thing, using TextExpander, that's quick and simple for me. I might try your more automated solution. I like the idea!

I do this:

1. I alt-drag* the file to copy it.
2. Press Enter to edit filename.
3. Press down-arrow once to put cursor at end of filename
4. Type "ddatt"
5. Press Enter. Done.

TextExpander replaces "ddatt" with, for example: "09-29-08"

I also use TE to type time in file names, with "ttim-", which auto-expands to "2-28 PM".

*I hate how Leopard's Finder doesn't let me simply do "Cmd-c, then Cmd-v" to paste an automatically-named copy of a file into the same folder. I used to do that. Much quicker and no need to reach for mouse.



[ Reply to This | # ]
Enhanced version
Authored by: Tschuess on Oct 02, '08 01:45:36PM
Thank you for this nice Script, this is a slightly enhanced Version with: Code cleanup; not dot at the end of filename; no extension in the middle of filename; clear TimeStamp like "2008-10-02 21_37_25"

(*
Create timestamped duplicates
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.

  Submitted by PizzaCake  Sep 29 2008 at 7:30AM PDT 
 enhanced by B.Bippus 2008-10-02: Code cleanup; not dot at the end of filename; no extension in the middle of filename; clear TimeStamp like "2008-10-02 21_37_25"
 *)
on open
	run -- Now you can either select the files or drop them on the app  
end open

tell application "Finder"
	set theselection to selection
	repeat with i from 1 to (count theselection)
		set aFileOrFolder to item i of theselection
		
		set ParentFolder to folder of (aFileOrFolder) as alias
		
		set theFileName_with_extension to name of (aFileOrFolder) as text
		set theFileExtension to my getFileExtension(theFileName_with_extension)
		set theFileName to my StripFileExtension(theFileName_with_extension)
		set thedate to my TimeStamp("")
		set theNewName to theFileName & " " & thedate & theFileExtension as text
		
		set theDuplicate to duplicate (aFileOrFolder) to ParentFolder replacing no
		set theDuplicateAlias to theDuplicate as alias
		set name of theDuplicateAlias to theNewName
		
	end repeat
	select (aFileOrFolder) -- select original item in finder 
end tell


--* Version 21.08.2007 (02.10.2008)
on TimeStamp(aTimeDate) --> returns: "2007-08-12"
	-- aTimeDate: full AS date like:  "Sonntag, 12. August 2007 13:46:43 Uhr"
	-- or aTimeDate = "" for current date time
	if aTimeDate is "" then
		copy (current date) to aTimeDate
	end if
	set totalSeconds to (time of (aTimeDate))
	set theHour to totalSeconds div 3600
	set theMinutes to (totalSeconds mod 3600) div 60
	set theSeconds to totalSeconds mod 60
	copy (theHour as string) & "_" & theMinutes & "_" & theSeconds to aTime
	
	copy short date string of (aTimeDate) to aDate
	
	set as_txt_del to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	copy text item 3 of aDate & "-" & text item 2 of aDate & "-" & text item 1 of aDate to thedateStamp
	set AppleScript's text item delimiters to as_txt_del
	
	--return thedateStamp
	copy thedateStamp & " " & aTime to aTimeStamp
	return aTimeStamp
end TimeStamp

on StripFileExtension(t_fileName) -- 02.10.2008/ 29.08.2007
	set AppleScript's text item delimiters to ""
	set t_fileName to "_" & t_fileName -- ".fh8"  empty filename
	if "." is in t_fileName then
		set myOffSet to (offset of "." in (reverse of characters of t_fileName as string))
		set t_fileName to characters 1 thru -(myOffSet + 1) of t_fileName as text
	end if
	if length of t_fileName > 1 then -- empty filename
		set t_fileName to characters 2 thru -1 of t_fileName as string
	else
		set t_fileName to ""
	end if
	return t_fileName
end StripFileExtension

on getFileExtension(t_fileName) -- 02.10.2008/ 29.08.2007
	set AppleScript's text item delimiters to ""
	set t_extension to "_" & t_fileName -- eg  empty filename = ".txt" 
	if "." is in t_fileName then
		set myOffSet to offset of "." in t_fileName as string
		set myOffSet to (offset of "." in (reverse of characters of t_fileName as string))
		if (myOffSet > 1) then
			set t_extension to characters (-1 * (myOffSet - 1)) thru -1 of t_fileName as text
		else
			set t_extension to ""
		end if
	else
		set t_extension to ""
	end if
	if length of t_extension = 0 then --  filename  empty
		set t_extension to ""
	end if
	return t_extension
end getFileExtension
Paste into Script Editor, and save as application. Select some file/s in the Finder, then run the saved AppleScript.



[ Reply to This | # ]