-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetCovers
More file actions
executable file
·121 lines (102 loc) · 3.49 KB
/
setCovers
File metadata and controls
executable file
·121 lines (102 loc) · 3.49 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# For each immediate subdirectory, attempts to set poster.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="realpath basename dirname"
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
# ImageMagick v7 uses the `magick` command; v6 uses `identify`/`convert` directly.
if command -v magick >/dev/null ; then
IM_IDENTIFY=(magick identify)
IM_CONVERT=(magick)
else
for i in identify convert; do
command -v "$i" >/dev/null && continue || { echo "command '$i' not found."; exit 1; }
done
IM_IDENTIFY=(identify)
IM_CONVERT=(convert)
fi
# 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"
# Prefer a local virtualenv interpreter when present.
PYTHON_CMD=(python3)
if [ -x "$SCRIPTFOLDER/.venv/bin/python" ] ; then
PYTHON_CMD=("$SCRIPTFOLDER/.venv/bin/python")
fi
if ! "${PYTHON_CMD[@]}" -c "import AppKit" >/dev/null 2>&1 ; then
echo "Python module 'AppKit' not found for: ${PYTHON_CMD[0]}" 1>&2
echo "Install it with: ${PYTHON_CMD[0]} -m pip install pyobjc" 1>&2
exit 1
fi
# 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=`"${IM_IDENTIFY[@]}" -format "%w" "$folderJPG"`
oldH=`"${IM_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=`"${IM_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
"${IM_CONVERT[@]}" "$folderJPG" -resize ${p2}x${p2} -background transparent \
-gravity center -extent ${p2}x${p2} "$squarePNG"
# Convert square png to square icns
"${IM_CONVERT[@]}" "$squarePNG" "$squareICNS"
echo -n " -> folder.icns [${p2}x${p2}]..." | tee -a "$LOG"
# Call python3 script setIcon
"${PYTHON_CMD[@]}" "$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"