Finding the latest screenshot in macOS Mojave

One of the things that changed in macOS Mojave was the format of screenshot filenames. On older versions of macOS, the filename would be something like:

Screen Shot 2016-10-10 at 18.34.18.png

On Mojave, the first two words got collapsed into one:

Screenshot 2019-03-08 at 18.38.41.png

I have a handful of scripts for doing something with screenshots – and in particular, a shortcut that grabs the newest screenshot. When I started updating to Mojave, I had to update the shell snippet that powers that shortcut. Because I couldn’t update to Mojave on every machine immediately, it had to work with both naming schemes.

This is what I’ve been using for the last few months (bound to last_screenshot in my shell config):

find ~/Desktop -name 'Screen Shot*' -print0 -o -name 'Screenshot*' -print0
  | xargs -0 stat -f '%m %N'
  | sort --numeric-sort --reverse
  | head -1
  | cut -f "2-" -d " "

Let’s break it down:

It’s certainly possible to do this with a higher-level language like Python or Ruby, but I like the elegance of chaining together tiny utilities like this. For non-critical code, I enjoy the brevity.