Skip to content

Instantly share code, notes, and snippets.

@jmcurran
Last active May 19, 2021 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcurran/bc600b9e05606d3ffbb688e4194017e2 to your computer and use it in GitHub Desktop.
Save jmcurran/bc600b9e05606d3ffbb688e4194017e2 to your computer and use it in GitHub Desktop.
Python Code for Automatically Renaming and Filing Screenshots on the Mac
import os
import re
root = '/Users/jcur002/Dropbox/Screenshots'
Files = os.listdir(root)
pat1 = re.compile('Screenshot (?P<year>[0-9]{4})\-(?P<month>[0-9]{2})\-(?P<day>[0-9]{2}) (?P<hh>[0-9]{2})\.(?P<mm>[0-9]{2})\.(?P<ss>[0-9]{2})(\.png)?')
pat2 = re.compile('Screen Shot (?P<year>[0-9]{4})\-(?P<month>[0-9]{2})\-(?P<day>[0-9]{2}) at (?P<hh>[0-9]{1,2})\.(?P<mm>[0-9]{2})\.(?P<ss>[0-9]{2}) *(?P<am>AM|PM)*(\.png)?')
for f in Files:
m1 = pat1.match(f)
m2 = pat2.match(f)
if m1 or m2:
if m1:
m = m1
else:
m = m2
year = m.group('year')
month = m.group('month')
day = m.group('day')
hh = m.group('hh')
mm = m.group('mm')
ss = m.group('ss')
if m2:
am = True if m.group('am') == 'AM' else False
if not am:
hh = int(hh) + 12
hh = str(hh)
newname = '{year}-{month}-{day} {hh}.{mm}.{ss}.png'.format(year = year,
month = month,
day = day,
hh = hh,
mm = mm,
ss = ss)
dirPath = os.path.join(root, year)
if not os.path.isdir(dirPath):
os.mkdir(dirPath)
oldPath = os.path.join(root, f)
newPath = os.path.join(root, year, newname)
os.rename(oldPath, newPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment