-
Notifications
You must be signed in to change notification settings - Fork 0
/
setCovers
executable file
·97 lines (81 loc) · 2.72 KB
/
setCovers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
# For each immediate subdirectory, attempts to set folder.jpg as folder icon
# Usage setSubdirIcons [-f] /path/to/folder
usage() { # print usage message & terminate
echo "Usage: ${0} [-f] directory" 1>&2
echo "Attemps to set each subdirectory's \"poster.jpg\" as its icon"
echo "Use -f flag to overwrite existing folder icons"
exit 1
}
CMDS="convert identify realpath basename dirname python3"
for i in $CMDS; do
# command -v will return >0 when the $i is not found
command -v $i >/dev/null && continue || { echo "command '$i' not found."; exit 1; }
done
# Process arguments
force=false
if [ $# -eq 0 ] ; then usage ; fi # no arguments ?
if [ "$1" == "-f" ] ; then # -f flag?
force=true
shift
fi
if [ $# -ne 1 ] ; then
echo "Wrong number of arguments" 1>&2
usage
fi
DIR="$1"
# Script constants
SCRIPTPATH=$(realpath "$0")
SCRIPTNAME=$(basename "$SCRIPTPATH")
SCRIPTFOLDER=$(dirname "$SCRIPTPATH")
# File output constants
SETICONPATH="$SCRIPTFOLDER/setIcon.py"
LOG="$SCRIPTPATH-$(date +%F_%Hh%M).log"
# Counters
success=0
prevsuccess=0
failure=0
echo "Firing up the flux capacitor..."
echo -e "$SCRIPTNAME $DIR \n" > "$LOG"
for subdir in "$DIR"/*/ ; do
folderJPG="$subdir""poster.jpg"
if [ -e "$folderJPG" ] ; then #not case-sensitive
echo $subdir | tee -a "$LOG"
squarePNG="${folderJPG%.*}.png"
squareICNS="${folderJPG%.*}.icns"
# Get height and width
oldW=`identify -format "%w" "$folderJPG"`
oldH=`identify -format "%h" "$folderJPG"`
echo -en "\tposter.jpg [${oldW}x${oldH}]" | tee -a "$LOG"
# Skip if icon is already set
if [ $force == "false" ] && [ -e "$subdir"/Icon$'\r' ] ; then
echo " -> icon previously set" ℹ️ | tee -a "$LOG"
prevsuccess=$((prevsuccess+1))
continue
fi
# Calculate new square size
p2=`identify -format "%[fx:2^(ceil(log(max(w,h))/log(2)))]" "$folderJPG"`
# Convert to square png
# Note: two-step conversion needed to avoid vertical black bars on either side
convert "$folderJPG" -resize ${p2}x${p2} -background transparent \
-gravity center -extent ${p2}x${p2} "$squarePNG"
# Convert square png to square icns
convert "$squarePNG" "$squareICNS"
echo -n " -> folder.icns [${p2}x${p2}]..." | tee -a "$LOG"
# Call python3 script setIcon
python3 "$SETICONPATH" "$squareICNS" "$subdir"
if [ ${?} -eq 0 ] ; then
echo "applied succesfully! ✅" | tee -a "$LOG"
success=$((success+1))
else
echo "unable to set file icon ❌" | tee -a "$LOG"
failure=$((failure+1))
fi
# Clean up
rm -f "$squarePNG" "$squareICNS"
else
echo "$folderJPG does not exist ❌" | tee -a "$LOG"
failure=$((failure+1))
fi
done
echo -e "\n ✅ $success \t ❌ $failure \t ℹ️ $prevsuccess \t\t Total: $((success+failure+prevsuccess))" | tee -a "$LOG"