-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdi-alfred.sh
executable file
·172 lines (109 loc) · 3.74 KB
/
di-alfred.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env zsh -f
# Purpose: Updated for Alfred 4
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2019-05-29
NAME="$0:t:r"
[[ -e "$HOME/.path" ]] && source "$HOME/.path"
[[ -e "$HOME/.config/di/defaults.sh" ]] && source "$HOME/.config/di/defaults.sh"
INSTALL_TO="${INSTALL_DIR_ALTERNATE-/Applications}/Alfred 4.app"
## Regular Releases
# XML_FEED='https://www.alfredapp.com/app/update4/general.xml'
## Beta Releases
XML_FEED='https://www.alfredapp.com/app/update4/prerelease.xml'
PLIST="${TMPDIR-/tmp}/${NAME}.$$.$RANDOM.plist"
rm -f "$PLIST"
curl -sfLS "$XML_FEED" > "$PLIST"
if [[ ! -s "$PLIST" ]]
then
echo "$NAME: '$PLIST' is empty."
exit 1
fi
RELEASE_NOTES=$(defaults read "${PLIST}" changelogdata | awk '/^## /{i++}i==1')
LATEST_BUILD=$(defaults read "${PLIST}" build)
URL=$(defaults read "${PLIST}" location)
LATEST_VERSION=$(defaults read "${PLIST}" version)
# If any of these are blank, we cannot continue
if [ "$LATEST_BUILD" = "" -o "$URL" = "" -o "$LATEST_VERSION" = "" ]
then
echo "$NAME: Error: bad data received:
LATEST_VERSION: $LATEST_VERSION
LATEST_BUILD: $LATEST_BUILD
URL: $URL
"
exit 1
fi
if [[ -e "$INSTALL_TO" ]]
then
INSTALLED_VERSION=$(defaults read "${INSTALL_TO}/Contents/Info" CFBundleShortVersionString)
INSTALLED_BUILD=$(defaults read "${INSTALL_TO}/Contents/Info" CFBundleVersion)
autoload is-at-least
is-at-least "$LATEST_VERSION" "$INSTALLED_VERSION"
VERSION_COMPARE="$?"
is-at-least "$LATEST_BUILD" "$INSTALLED_BUILD"
BUILD_COMPARE="$?"
if [ "$VERSION_COMPARE" = "0" -a "$BUILD_COMPARE" = "0" ]
then
echo "$NAME: Up-To-Date ($INSTALLED_VERSION/$INSTALLED_BUILD)"
exit 0
fi
echo "$NAME: Outdated: $INSTALLED_VERSION/$INSTALLED_BUILD vs $LATEST_VERSION/$LATEST_BUILD"
FIRST_INSTALL='no'
else
FIRST_INSTALL='yes'
fi
# <string>https://cachefly.alfredapp.com/Alfred_4.0_1076.tar.gz</string>
FILENAME="$HOME/Downloads/Alfred-${LATEST_VERSION}_${LATEST_BUILD}.tgz"
if (( $+commands[lynx] ))
then
(echo "Alfred ${LATEST_VERSION} / ${LATEST_BUILD} \nURL: ${URL}\n\n$RELEASE_NOTES") | tee "$FILENAME:r.txt"
fi
## Downloading
echo "$NAME: Downloading '$URL' to '$FILENAME':"
curl --continue-at - --fail --location --output "$FILENAME" "$URL"
EXIT="$?"
## exit 22 means 'the file was already fully downloaded'
[ "$EXIT" != "0" -a "$EXIT" != "22" ] && echo "$NAME: Download of $URL failed (EXIT = $EXIT)" && exit 0
[[ ! -e "$FILENAME" ]] && echo "$NAME: $FILENAME does not exist." && exit 0
[[ ! -s "$FILENAME" ]] && echo "$NAME: $FILENAME is zero bytes." && rm -f "$FILENAME" && exit 0
## Un-Archiving
TEMPDIR=$(mktemp -d "${TMPDIR-/tmp}/$NAME.XXXXXX")
echo "$NAME: Extracting '$FILENAME' to '$TEMPDIR':"
tar -C "$TEMPDIR" -z -x -f "$FILENAME"
TEMPAPP="$TEMPDIR/Alfred 4.app"
if [[ ! -d "$TEMPAPP" ]]
then
echo "$NAME: Did not find '$TEMPAPP'. Giving up."
exit 1
fi
## Move old version
if [[ -e "$INSTALL_TO" ]]
then
## the release notes for 4.0 suggests that the AppleScript syntax should be
## osascript -e 'tell application "com.runningwithcrayons.Alfred" to quit'
## but that does not actually cause Alfred 4 to quit.
## However
## osascript -e 'tell application "Alfred 4" to quit'
## does work, so we use that
##
## Also `pgrep -x "Alfred 4"` does not work but `pgrep -x "Alfred"` does
# Quit app, if running
pgrep -xq "Alfred" \
&& LAUNCH='yes' \
&& osascript -e 'tell application "Alfred 4" to quit'
# move installed version to trash
mv -vf "$INSTALL_TO" "$HOME/.Trash/$INSTALL_TO:t.$INSTALLED_VERSION.app"
fi
mv -vn "$TEMPAPP" "$INSTALL_TO"
EXIT="$?"
if [ "$EXIT" = "0" ]
then
echo "$NAME: Installed new version to '$INSTALL_TO'."
[[ "$LAUNCH" = "yes" ]] && open -g -j -a "$INSTALL_TO"
else
echo "$NAME: failed (\$EXIT = $EXIT)"
exit 1
fi
exit 0
#EOF