-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIconGrabber.sh
executable file
·149 lines (119 loc) · 3.37 KB
/
IconGrabber.sh
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/zsh --no-rcs
# shellcheck shell=bash
#set -x
#useDialog=false
# IconGrabber.sh by: Trevor Sysock
# 2024-05-29
# Programmatically Find icns Files and Convert Them to PNG
# Provide one argument to this script: An app or directory containing apps/icns files
# Script will search the provided path for .icns files and convert them to png.
#
# Default directory to scan is /Applications
#
# Output will be saved in /var/tmp/ and the folder will be opened when complete.
#################
# Functions #
#################
# Check if we're using Dialog, setup our functions
if [ -x /usr/local/bin/dialog ] && [[ $useDialog != false ]]; then
useDialog=true
# execute a dialog command
function dialog_command(){
/bin/echo "$@" >> "$dialogCommandFile"
#log_message "$@"
sleep .1
}
function increment_dialog(){
count=0
while [ "$count" -lt 90 ]; do
dialog_command "progress: increment"
sleep .5
count=$(( count + 1 ))
done
}
else
# Not using dialog, make dummy functions
useDialog=false
function dialog_command(){
true
}
function increment_dialog(){
true
}
fi
##########################
# Script Starts Here #
##########################
echo "Initiated IconGrabber.sh: $(date)"
# Requires 1 arument.
if [ -z "$1" ]; then
searchDir="/Applications"
else
# Set our search dir based on input
searchDir="${1}"
fi
echo "Search Directory set: $searchDir"
# Set the tmp output dir as a unique folder
dirName="$(mktemp -d /var/tmp/IconGrabber.XXXXXXX)"
# Make the tmp output dir
if ! mkdir -p "${dirName}"; then
echo "Could not create ${dirName}"
exit 1
else
echo "Temporary Directory: $dirName"
fi
# Initiate an empty array
fileList=()
# Start a dialog window
if "$useDialog"; then
echo "Creating dialog window"
dialogCommandFile=$(mktemp /var/tmp/IconGrabberDialog.XXXXXX)
/usr/local/bin/dialog \
--title none \
--message " " \
--icon "SF=rectangle.and.text.magnifyingglass" \
--progress \
--moveable \
--ontop \
--width 420 \
--height 150 \
--centericon \
--button1disabled \
--commandfile "$dialogCommandFile" &
sleep 1
else
echo "Skipping Dialog"
fi
echo "Finding .icns files"
# read/find commands put each found .icns file into an array so we can iterate through later
fileCount=0
while IFS= read -r -d $'\0'; do
#$REPLY is our relative file path, this is part of the `read` command
currentFile="${REPLY}"
fileList+=("$currentFile")
fileCount=$(( fileCount + 1 ))
done < <(find "$searchDir" -name "*.icns" -print0 )
echo "Converting $fileCount files"
# Update dialog window
dialog_command "progresstext: Converting all .icns to .png..."
dialog_command "height: 200"
# This is a fake progress bar, gotcha
increment_dialog & killPID=$!
# Iterate through our list of icons and do the conversion
count=1
for icon in "${fileList[@]}"; do
iconAppParent="$(echo ${icon} | sed 's/.app.*/.app/')"
iconBaseName="$(basename ${iconAppParent})"
outputName="${dirName}/${iconBaseName:r}-$count.png"
sips -s format png "${icon}" --out "${outputName}" > /dev/null 2>&1
echo "icon: ${outputName}" >> "$dialogCommandFile"
count=$(( count + 1 ))
done
if [ -n "$killPID" ]; then
kill "$killPID"
fi
dialog_command "progress: complete"
sleep 2
dialog_command "quit:"
rm "${dialogCommandFile}"
open "${dirName}"