forked from eprev/locationchanger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocationchanger.sh
More file actions
executable file
·95 lines (77 loc) · 2.52 KB
/
Copy pathlocationchanger.sh
File metadata and controls
executable file
·95 lines (77 loc) · 2.52 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
#!/bin/bash
INSTALL_DIR=/usr/local/bin
SCRIPT_NAME=$INSTALL_DIR/locationchanger
PLIST_NAME=$HOME/Library/LaunchAgents/LocationChanger.plist
sudo -v
mkdir -p $INSTALL_DIR
cat > $SCRIPT_NAME << "EOT"
#!/bin/bash
# This script changes network location based on the name of Wi-Fi network.
exec 2>&1 >> $HOME/Library/Logs/LocationChanger.log
sleep 3
ts() {
date +"[%Y-%m-%d %H:%M] $*"
}
notify() {
osascript -e "display notification \"$*\" with title $SCRIPT_NAME"
}
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep ' SSID' | cut -d : -f 2- | sed 's/^[ \t]*//'`
LOCATION_NAMES=`scselect | tail -n +2 | cut -d \( -f 2- | sed 's/)$//'`
CURRENT_LOCATION=`scselect | tail -n +2 | grep '^\ +\*' | cut -d \( -f 2- | sed 's/)$//'`
ts "Connected to '$SSID'"
CONFIG_FILE=$HOME/.locations/locations.conf
if [ -f $CONFIG_FILE ]; then
NEW_SSID=`grep "^$SSID=" $CONFIG_FILE | cut -d = -f 2`
if [ "$NEW_SSID" != "" ]; then
ts "Will switch the location to '$NEW_SSID' (configuration file)"
SSID=$NEW_SSID
else
ts "Will switch the location to '$SSID'"
fi
fi
if echo "$LOCATION_NAMES" | grep -q "^$SSID$"; then
NEW_LOCATION="$SSID"
else
if echo "$LOCATION_NAMES" | grep -q "^Automatic$"; then
NEW_LOCATION=Automatic
ts "Location '$SSID' was not found. Will default to 'Automatic'"
else
ts "Location '$SSID' was not found. The following locations are available: $LOCATION_NAMES"
exit 1
fi
fi
if [ "$NEW_LOCATION" != "" ]; then
if [ "$NEW_LOCATION" != "$CURRENT_LOCATION" ]; then
ts "Changing the location to '$NEW_LOCATION'"
notify("Changing the location to '$NEW_LOCATION'")
scselect "$NEW_LOCATION"
SCRIPT="$HOME/.locations/$NEW_LOCATION"
if [ -f $SCRIPT ]; then
ts "Running '$SCRIPT'"
$SCRIPT
fi
else
ts "Already at '$NEW_LOCATION'"
fi
fi
EOT
chmod +x $SCRIPT_NAME
cat > $PLIST_NAME << EOT
<?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>locationchanger</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/locationchanger</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
</dict>
</plist>
EOT
launchctl load $PLIST_NAME