Somewhere in 2016, maybe a bit later, but coinciding with a new Mac, I discovered that Apple had decided it would change the way it named screenshots. Specifically, the format switched from:
Screenshot YYYY-MM-DD HH.mm.ss.png
Screen Shot YYYY-MM-DD at hh.mm.ss AM
Screen Shot YYYY-MM-DD at hh.mm.ss PM
Why does this matter? Well, it means the ordering of screenshots is no longer in chronological order, and when you have a lot, this really is a nuisance. I am not completely clear if this is a conscious choice by Apple, but you can make your Mac do the same thing by opening up the Screenshot application (⌘-shift-5) and changing the mode from ‘Screenshots’ to ‘Clipboard’ and back.
After many years of searching old internet posts for a solution to the problem I decided to change tack. There are some internal settings that you can change at the command line, but none would let me switch the file name back the format I preferred. At the same time I decided to automatically file the snapshots in folders organised by year, just to make finding my newest screenshots easier. You might ask why I keep them. Good question. I do not know, and I may very well delete them all at some point.
The solution is two-fold. I wrote a Python script to automate the changing of filenames, creation of folders, and moving of files, and used
cron to automatically run the script once a minute, which is usually enough time for the file to be renamed and moved before I get around to including it in the email, or presentation, or document that I am working on. Why Python and not R—? After all I am more proficient in the latter than the former. I think Python’s handling of regular expressions is nicer and easier to work with. I also find the way it handles files, paths, and dealing with the operating system a little more logical and robust. Shout about this if you want—I promise I will not be listening.
I have written two scripts—one of which deals with the screenshots, and one of which effectively does the same thing for camera uploads to Dropbox. The second script is particular to the way Dropbox imports images, but it is not very complex and so could be adapted to another system. I also toyed with extracting information from the EXIF tags rather than manipulating file names, however this seems a bit clunky in Python (the date information appears to only be accessible by a numeric, but not named, tag). The scripts are given below, but are also hosted on github as gists here: jmcurran/tidyScreenshots.py and here: jmcurran/cleanCamUploads.py.
cron is a software utility that is a time based job scheduler for Unix-like operating systems. Users can use
cron to schedule commands or scripts to run at fixed times, dates, or intervals. My solution depends on
cron being available, and hence it is useable on both a Mac and on Linux systems. It is unclear to me, although I will investigate, as to whether one can use the Linux Subsystem for Windows (WSL) to use
cron. I have two entries in the
crontab file, one for each of my scripts. Each entry says that the scripts should be executed every minute of every day. Although this sounds rather intense, it has negligible impact on the performance of my system. The
crontab file can be edited by opening up a terminal window (open Spotlight and type
terminal, and hit enter) and typing
crontab -e at the command line. This will bring up a text file which can be edited using the
vi editor. If you are not familiar with
vi, this may be somewhat painful. Some useful commands:
Keystroke Action --------- ------ x delete character dd delete line i enter insert mode ESC exit insert mode shift-zz save file and exit
As noted I set my scripts to run every minute. My crontab file looks like this:
0-59 * * * * /Users/jcur002/opt/anaconda3/bin/python3 $HOME/Dropbox/Code/Python/tidyScreenshots/cleanScreenShots.py >> ~/cron.log 2>&1 0-59 * * * * /Users/jcur002/opt/anaconda3/bin/python3 $HOME/Dropbox/Code/Python/tidyCamUploads/tidyCamUploads.py >> ~/cron.log 2>&1
NB: You can scroll the text in the code box above.
The key elements here are:
0-59means run every minute.
- The four asterisks are used to specify hours (0-23), day of the month (0-31), month (1-12), and day of the week (0-6). As they are unspecified, this means my job is run every minute of every day.
- The path to the location of the python binary must be fully specified.
- The location of each script must also be unambigously specified, but can rely on shell variables like
I should note that there is a strong element of “monkey see, monkey do” here. I am not completely au fait with
cron but it appears to do what I want it to do. I hope you find this useful.