Automating Date

Discussion in 'Mac Apps and Mac App Store' started by phaedo5, Jul 8, 2009.

  1. phaedo5 macrumors newbie

    Joined:
    Jan 13, 2009
    #1
    I want to use Automator to insert a date in front of a file name. However, the date options for Finder in Automator are limited.

    I want this:
    filename.ext

    To be this:
    2009.07.08_filename.ext

    They have options for doing something close to this, but I need it exactly as I I have it with periods as separators and two digit day and month.

    Is there anything I can do to make that happen in Automator?

    I know I can use something like A Better Finder Rename - but that costs I believe.
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    It doesn't appear that Automator can do exactly what you want. Here's an Applescript that I believe will:

    Code:
    on twodigits(num) -- turns "7/8/9" to '07/08/09"
    	if num > 9 then
    		return (num as string)
    	else
    		return "0" & (num as string)
    	end if
    end twodigits
    
    on datePeriods() -- fetches date string
    	set theYear to the year of the (current date) as string
    	set theMonth to my twodigits((the month of the (current date)) as integer)
    	set theDay to my twodigits(the (the day of the (current date)) as integer)
    	return theYear & "." & theMonth & "." & theDay
    end datePeriods
    
    on rename(anAlias) -- renames the file
    	
    	tell application "Finder"
    		set theFileName to (the displayed name of anAlias)
    		
    		set newFileString to my datePeriods() & "_" & theFileName
    		try
    			set the name of anAlias to newFileString
    		on error
    			display dialog "Couldn't rename " & theFileName
    		end try
    	end tell
    	
    end rename
    
    
    on open fileNames -- these files have been dropped on droplet
    	
    	repeat with theFileAlias in fileNames
    		
    		rename(theFileAlias)
    		
    	end repeat
    	
    end open
    
    
    on run -- this runs if you doubleclick on file
    	set theFileAlias to (choose file)
    	rename(theFileAlias)
    end run
    Open script editor, paste this in the window and save it as an application.

    It creates a droplet that will let you drop files onto it, and rename those files. The "on run" handler lets you double click on the icon and you'll get a Finder open window to select a file.

    This worked on my machine. Holler if you have any problems.

    BTW, I haven't been able to test the error handler. If you see it in action, write back on whether it works as its supposed to.

    mt
     
  3. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #3
    Just use Name Mangler. It is the best file renaming utility I've found.
     

Share This Page