forked from Marginal/QLVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resetmds
executable file
·67 lines (56 loc) · 1.62 KB
/
resetmds
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
#!/bin/sh
touch -c "$2"
if [ -z "$USER" ]; then exit 0; fi
# If upgrading then force re-indexing for the current user immediately
if sudo -u $USER /usr/bin/mdimport -L 2>&1 | grep -q "$2"; then
sudo -u $USER /usr/bin/mdimport -r "$2"
exit 0;
fi
# Otherwise schedule re-indexing for the next time the current user logs on
JOB=uk.org.marginal.qlvideo.mdimporter
AGENT="$HOME/Library/LaunchAgents/$JOB.plist"
SCRIPT="$HOME/Library/Application Support/uk.org.marginal.qlvideo/mdimporter"
sudo -u $USER mkdir -p "`dirname "$SCRIPT"`"
sudo -u $USER /usr/bin/tee "$SCRIPT" >/dev/null <<EOF
#!/bin/bash
#
# QLVideo helper script to kick Spotlight into (re)indexing movie files
#
# Wait for Spotlight to notice the new importer
TRY=300
while [ \$((TRY -= 10)) -ne 0 ]; do
if /usr/bin/mdimport -L 2>&1 | grep -q "$2"; then
break;
else
sleep 10;
fi;
done
# Force re-indexing
/usr/bin/mdimport -r "$2"
# Clean up
rm -f "$AGENT"
rm -f "$SCRIPT"
/bin/launchctl remove $JOB
EOF
chmod +x "$SCRIPT"
sudo -u $USER mkdir -p "`dirname "$AGENT"`"
sudo -u $USER /usr/bin/tee "$AGENT" >/dev/null <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
<key>Label</key>
<string>$JOB</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>$SCRIPT</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
# Launch immediately in case the user doesn't intend to log off any time soon
sudo -u $USER /bin/launchctl load "$AGENT"
true