Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to rename screen capture files on MacOSX to something cleaner. It's handy to attach this as a "folder action" so that files get renamed automagically. See http://apple.stackexchange.com/a/112747/27434
#!/bin/bash
# -*- mode:shell-script; coding:utf-8; -*-
#
# Rename screenshot files to cleaner names, names for which a
# lexicographic sort is equivalent to a time-based sort.
#
#
# Created: <Wed Dec 4 10:24:15 2013>
# Last Updated: <2013-December-04 13:57:46>
#
IFS=$'\n'
targdir=/Users/${USER}/Desktop
pattern=Screen\ Shot\ ????-??-??\ at\ *M.png
cd $targdir
files="$(find . -iname "$pattern" -type f -print)"
if [ -n "$files" ]; then
count=$(echo "$files" | wc -l)
else
count=0
fi
if [ $count -gt 0 ]; then
ix=1
for item in ${files} ; do
printf "%d. %-18s\n" $ix "$item"
# strip dot-slash
fname=${item:2}
IFS=$' '
parts=(${fname// / })
# for part in ${parts} ; do
# echo $part
# done
#echo parts count: ${#parts}
fdate=${parts[2]}
ftime=${parts[4]}
fdate=(${fdate//-/ })
ftime=(${ftime//./ })
suffix=${parts[5]}
suffix=${suffix:0:2}
#echo suffix: ${suffix}
if [[ $suffix =~ ^PM ]] ; then
if ! [[ $ftime[0] =~ 12 ]]; then
# echo adding 12
let "ftime[0]+=12"
fi
else
# prepend with a zero as necessary
if [[ $ftime[0] =~ 12 ]]; then
ftime[0]="00"
else
tvar="00${ftime[0]}"
ftime[0]=${tvar:(-2)}
fi
fi
newname="screenshot-${fdate[0]}${fdate[1]}${fdate[2]}-${ftime[0]}${ftime[1]}${ftime[2]}.png"
if ! [ -f ${newname} ]; then
echo new name: $newname
mv "${fname}" "${newname}"
fi
IFS=$'\n'
let "ix+=1"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.