How to add date/time stamp to screenshot file in shell script?


In the following shell script, I would like to add the date time stamp

screencapture -i /Users/Peter/Downloads/screenshot.png

and end up with something like screenshot 2020-1-1 8:00.png

thanks in advance for your time and help

you can use something like:

and then use it as:
screencapture -I ~/Downloads/%CreationDate% screenshot.png

1 Like

I think I stole than from a post from JMichael


Be careful. Your example includes an illegal character for file: The colon :
which, as you probably know, is used in the HFS as a folder separator.

And in Unix (POSIX) paths, the forward slash / is a folder separator.

So, best practice is to always avoid using either in file names.

@hello's suggestion avoids both, while including minutes in the file name.
BTW, if you are creating a lot of files very quickly using this approach, you should include time with tenths of seconds:



thank you both @hello @JMichaelTX
I read and re-read @JMichaelTX warnings about file naming, and am embarrassed to say that I can't get the file syntax right in the execute shell script.
thanks again

Shell scripts can be tricky unless you happen to be a shell script guru, and most of us are not, including me. :wink:

There are two problems with your shell script:

  1. The file path is NOT properly quoted
  2. The proper syntax for KM variables is not used.

For using paths, see Quoting Strings in a Shell Script action.

Basically, paths in shell scripts can NOT contain spaces, unless you take precautions.
In this case you need to enclose your path in double-quotes.

screencapture -i "/Users/YourUserName/Downloads/%Variable%Date TimeForFile% screenshot.png"

That takes care of issue #1, but NOT issue #2.

When you are having trouble with a shell script, one way to troubleshoot is to place "echo" at the front of the line, and output to a window.

So you would have:

and when you run this you see:

So clearly the KM Variable is NOT getting substituted with its value.

The problem is you are not using the correct syntax for using KM Variables in shell scripts:

Using Keyboard Maestro Variables in Shell Scripts

Using Keyboard Maestro Variables

Most Languages like bash, perl, ruby

In your script, use this format: $KMVAR_[variable_name]

where [variable_name] is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores ( ).

For example: File Name would be formatted as $KMVAR‗File‗Name .

If your variable already has underscores, you can use it as is.

So, where the correct syntax is used:

we get the correct result:



thank you for a beautiful explanation both in content and in terms of the didactic format.

You really should write a Kindle book on KBM.

Unfortunately, the echo is correct but the term snapshot does not appear in the file listing neither in Finder nor in Pathfinder.

1- macro
2- echo is fine
3- both PF and Finder: only the date time stamp appears.

I tried to add an underscore between File_screenshot but it did not help

thank you !


does using the full path work?
instead of ~/Downloads

also, tried saving in Desktop and not using as a variable?

~/Desktop/Screencapture %ICUDateTime%yyyy-MM-dd-HHmmssA%.jpg

1 Like

thank you for your suggestions. Neither option works. End result is the same

@hello @JMichaelTX
I thank you both for your help. I think that we have spent enough time on this issue. I will look for alternative solutions.
thanks again very much

1 Like

sure, good luck!
try the Screen Capture command

1 Like

thank you for the extra tip. It is very helpful. I did not know about it.
To pick up on a recent conversation, I tried a gazillion data management/notes apps.
I forgot to mention. Although I use evernote for data management, Bear is a very good small but powerful app when I jot down notes which I am working at my computer. it is powerful, has many export capabilities, handles images, has a good tagging system. Web Clipping is not good but that's not the objective.
Another point: if you are a student, do some research, want any kind of academic career, knowing and using Scrivener is a must.

hey cool!
Yeah, from the other thread, tried a lot, and settled on Scrivener. It has capability for extra metadata fields, and Outline view can be used for to-dos :slight_smile:

I found another problem:
By default, the screencapture command outputs in jpg format, and so when you give it a filename with .png it fails to save to the file.
This is in spite of the documentation:

-t<format>     image format to create, default is png (other options include pdf, jpg, tiff and other formats)

So, when I changed the file extension to .jpg it worked.
screencapture -i "/Users/YourUserName/Downloads/$KMVAR_DateTimeForFile screenshot.jpg"

It took a lot of testing and trial-and-error to discover this.

Based on your original macro and what I learned, I created and just posted this macro:

MACRO: ScreenCapture: Select Region & Save to File with Date/Time Stamp [Example]

It worked for using .jpg, .png, and .pdf formats.

Please let us know if it works for you.


I do not believe that is true. My tests show that it happily outputs a PNG file.

One thing to not here is that the 'echo' is expanding the quotes along with the KMVar. If you really want to see what your command would look like you need to escape the quotes:

 echo screencapture -i \"~/Downloads/$KMVAR_DateTimeForFile screenshot.png\"

Now... onto the solution.

You should omit the quotes, use your ~ for expanding your home directory and remove the space from the filename (either an underscore or another dash).

screencapture -i ~/Downloads/$KMVAR_DateTimeForFile-screenshot.png

do that and you'll be gold.

One other thing... if you really want the space, and you want to expand ~, you can wrap the part of the filename that has spaces with quotes... like this:

screencapture -i ~/Downloads/"$KMVAR_DateTimeForFile screenshot.png"

In my tests, when I specified an output file with a ".png" and did NOT specify the file format parameter, it failed.
But I seem to remember, but can't find now, that somewhere you can set the default output format. Perhaps I did this and set it to "jpg".

IAC, it is easily, and perhaps best, handled by including the file format using the -t parameter.

IMO, it is best practice to always enclose paths in double-quotes because you never know when a path might contain a space. Just about all of my file names have spaces.

It is also easy enough to use the KM Filter Action to "expand tilde" before passing the full path in a KM variable, so that becomes a non issue.

In case you didn't see my macro, it comes down to this which works quite well:

1 Like

Fair point, I was just showing the OP how to 'fix' the original command with ~ and why quotes would interfere with expansion.

To be clear, double quotes will NOT interfere with the path that does NOT have a tilde.

1 Like