Renaming files based on difference between creation date and a specific date

Hi! I’m having some success with KM so far, and find it pairs well with TextExpander, Hazel, etc. I wrote on the Noodlesoft forums and hit a wall there, so I figured I’d ask here.

What I’d like to do is set up KM to rename images based on the time between the Date Created and a specific date. The purpose is to drop “growth” photos of my dog into the “growth” folder and have them automatically renamed something like “Arlo at X.X weeks” where X.X is the number of weeks between the date I took the photo and 8/2/15, his birthday.

Can this be done in KM, or with KM + AppleScript etc.?

Thank you :smile:

Hey Christian,

This sort of thing is possible.

Here’s an AppleScript snippet to get you started.

set birthDate to "3/1/2016"
set birthDate to date birthDate

set cDate to current date -- Substituting for creation date of file.
set time of cDate to 0

set weeksElapsed to ((cDate - birthDate) / weeks)


Use the For Each action with the Folder Contents collection to iterate over the contents of a folder.

Use the If Then Else action with the Variable condition to test that the file is an image (.jpeg or whatever).

Use the Get File Attribute action to get the creation date of the file.

The date is in seconds since 1970 (unixtime).

Use the TIME function to get the birth time (ie, TIME(2015,2,8) or (2015,8,2) depending on which you mean February 8 or August 2).

Use the Set Variable to Calculation action to get the time in weeks, formatted as 0.0 something like:

Then use the Move or Rename a File action to rename the file.

Thank you. I think I'm getting there. Does anything here stick out to you? It was working at first, but now misfires with either the wrong number of weeks, or the same number of weeks for each photo, or sometimes a negative number. I check the dates created and they are correct. Also, once it finishes it tries to do them all over again and I get "destination already exists" errors. Any way to prevent that?

Thank you!

An incidental problem is that you are renaming files from png to jpg, which you probably don’t want to do.

The actual problem is likely that you are modifying the directory while iterating through the directory. So as the system lists through the directory, you are moving files around in the directory, and so the listing gets confused.

There are two solutions:

  • Make your macro actions idempotent, and restart the macro each time you make any change.
  • Read the list of files into a variable, then iterate through the list of files.

The former solution would go something like this:

  • Set variable “Did Something” to 0
  • Until variable “Did Something” is 0
    • For Each variable “image” in folder
      • if variable image does not contain " weeks"
        • if variable image is an image
          • Rename file
          • Set variable “Did Something” to 1
          • Break from Loop

So basically you are avoiding redoing files that you have already done, and restarting the loop each time you make a change so the directory iteration wont get confused.

The second method looks something like:

  • Set variable “List” to “”
  • For each variable “image” in folder
    • Set variable “List” to “%Variable%List%$Variable%image%\n”
  • For each variable “image” in lines in variable “List”
    • do stuff

@peternlewis - I'm having trouble implementing both suggested methods without getting an "Action Failed' notification.

Here are the referenced macros for any tinkering:
MoveFiles.kmmacros (9.9 KB)

My use case involves copying to another directory. Sometimes the files are moved over and sometimes they are not. Whether I get an error message or not appears to be independent.

Restart loop solution:

List solution:

Hey Greg,

Try something like this.


Folder Trigger – Move Screen Shots.kmmacros (2.9 KB)

Thanks @ccstone - It worked after I made these modifications

  • Removed the hot key trigger
  • adjusted folder locations for my setup
  • used contains “Screen Shot” instead of lower case “screen shot”

Hey Greg,

The text contains condition isn't case sensitive.


Good to know. I am paranoid :slight_smile:

Trust – but verify...  :smile: