Automatically prepending dates to the names of saved files

So a Default Folder X user just emailed and asked this:

I have been a Mac user for 30 years and would love to find a tool that allows me to click a button (or make this the default filename) while in the “Save…” dialog box that will prepend a formatted date to the beginning of the filename. like so:

2020-09-08 Filename.ext

Now, you can set up an AppleScript to do this using Default Folder X’s GetSaveName and SetSaveName verbs. However, that would require that you run the AppleScript whenever you want the date prepended, which is a bit of a pain if you want all of your filenames formatted this way. But I realized as I was replying that you can actually automate this by using (or rather, slightly abusing) an existing feature in Default Folder X.

Default Folder X has the ability to run an AppleScript to determine the location of an application’s default folder. The script will be run whenever a new file dialog is displayed by an application, which is the perfect time to do our little filename modification. So I wrote an AppleScript that looks like this:

on getDefaultFolder(appName, dialogType, firstTime)

  -- only do this for save dialogs
  if dialogType is "save" then

    -- get the current date
    set dateObj to (current date)

    -- then format it as YYYY-MM-DD
    set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
    set theDay to text -1 thru -2 of ("0" & day of dateObj)
    set theYear to year of dateObj
    set dateStamp to "" & theYear & "-" & theMonth & "-" & theDay

    -- then prepend that to the name in the save dialog
    tell application "Default Folder X"
      set theName to GetSaveName
      set theName to dateStamp & " " & theName
      SetSaveName theName
    end tell
  end if

  -- finally, don't give Default Folder X a default
  -- folder, so it just continues on normally 
  return ""
end getDefaultFolder

If you save this script in a file named “GetDefaultFolder.scpt” in this location:

~/Library/Application Support/com.stclairsoft.DefaultFolderX5/Scripts/

It will magically prepend the date in the format ‘2020-09-15’ to the beginning of all of your filenames in Save As dialogs. Note that you can still edit the name afterwards if the default filename (like “Untitled 4”) needs to be modified.

6 Responses to “Automatically prepending dates to the names of saved files”

  1. A 1000 thank you’s for the this piece of automagic that also for me fulfilled a desire that I didn’t realise I have had for 30 years.

    • Jon says:

      You’re welcome! Also note that you can do this on an application-by-application basis if you only want dates on filenames when you save them from Safari, for example. Just put the “GetDefaultFolder.scpt” file in a subfolder of the Scripts folder, with the folder named with the application’s name (meaning that it’ll only be used for Safari if it’s in a subfolder named ‘Safari’).

  2. Mogens Thyregod says:

    Wow, it’s just keeps getting better and better. I guess this this means that I can have any numbers of application-named folder with a copy of the “GetDefaultFolder.scpt” inside and thus control the auto-renaming feature.

  3. Tom Rohde says:

    This is wonderful, thank you!

    Cordial greetings from this long-time user in Germany 😊

Leave a Reply