# Renaming Files with Keyboard Maestro

I use several file naming schemes on my Mac. For screenshots I like to include a timestamp in the file name. If I am making several screenshots, I may want to prepend them with a sequence as well.

Keyboard Maestro makes this process very easy, with the new "For Each" conditional action. The macros shown all have the same core components with a few variations for formatting the new file name.

The "For Each" conditional action grabs each selection in the Finder and passes along the path to the file. I then use a couple of Shell scripts to get the file's base name and the parent directory. This could be accomplished with AppleScript string parsing or some kind of grep, but these Shell commands work on OS X and they are very concise and easy to read.

The middle portion of each macro just manipulates the filename as I need. The crucial bit, as well as the riskiest, is the last Shell script. This relatively short command performs the rename function. It uses the Shell "mv" command.

The "mv" command is very basic, but can be very destructive. Use at your own risk. If you don't feel comfortable using the MV command in the Terminal, then I don't recommend you use these macros either. I take no responsibility for anyone else's stupidity other than mine. Even that's debatable.

### Items of Note

Renaming the files does not change modification timestamp. That's good, but just be aware.

This only works on files, not directories. That's how I want it.

I think it's more predictable to have one macro for each type of name change. I'd be concerned that performing a bunch of changes at in one macro could generate file names that are very awkward and not at all what I had in mind.

The "Trim Results" check-box is crucial. Without that, the path and file name both get a newline character at the end. That's very bad for the "MV" command. Most of the time that will cause an error, but I suppose you could accidentally rename a directory instead of the file.

### The Macros

#### Make URL Safe File Names

This macro uses the built-in Keyboard Maestro string formatting to make URL safe file names. This is damn useful.

#### Prepend with Current Time Stamp

Prefixes the file name with the current time in the format "2012_04_05_11400029_MyAwesomeFile.txt"

#### Prepend with Sequence

Select a series of files and this macro will prepend the file names with a series of integers, for example:

1_MyAwesomeFile.txt

2_MyMoreAwesomeFile.txt

3_SomeJunk.txt



#### Prepend with File Modification Date

This macro looks up the modification date of the file using AppleScript and prepends it in the format "2012_4_3_77131". I'm not a fan of AppleScript date formatting but the code is relatively efficient. I'll probably end up changing this to use a Python shell script instead.

Here's the AppleScript:

tell application "Keyboard Maestro Engine"

set curFile to (process tokens "%Variable%filePath%")

set posixFilePath to POSIX path of curFile

set posixFile to POSIX file posixFilePath as string

end tell

set fileInfo to info for posixFilePath

set modDate to the modification date of fileInfo as string

set timeStamp to date modDate

set fmtTimeStamp to ((year of timeStamp) & "_" ¬

& (month of timeStamp as integer) & "_" ¬

& (day of timeStamp) & "_" ¬

& time of timeStamp) as string

return fmtTimeStamp



#### Prepend with Arbitrary Text

This macro asks for a user specified prefix which can be just about anything.