Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Created July 8, 2015 16:04
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 DinoChiesa/113a5b30c05f125a3c3d to your computer and use it in GitHub Desktop.
Save DinoChiesa/113a5b30c05f125a3c3d to your computer and use it in GitHub Desktop.
Rename screenshots on Mac OSX to something sane
#!/bin/bash
# -*- mode:shell-script; coding:utf-8; -*-
#
# fixupScreenshotName.sh
#
# The MIT License (MIT)
#
# Copyright (c) 2014 by Dino Chiesa
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Created: <Wed Dec 4 10:24:15 2013>
# Last Updated: <2015-July-08 09:03:30>
#
IFS=$'\n'
targdir=/Users/YOURSELF/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