-
Notifications
You must be signed in to change notification settings - Fork 2
/
Intellidock.sh
executable file
·174 lines (153 loc) · 4.68 KB
/
Intellidock.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
173
174
#!/bin/bash
# MIT License
#
# Copyright (c) 2018 Joni Van Roost
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
log()
{
local formatted_date=$(date -u +"%Y-%m-%dT%H:%M:%S")
echo "$formatted_date: $1" # output to cli
if [ $ENABLE_LOGGING -eq 1 ]; then
local logfile="$LOG_PATH/Intellidock.log"
echo "$formatted_date: $1" >> $logfile 2>&1&
fi
}
notification()
{
if [ $ENABLE_NOTIFICATIONS -eq 1 ]; then
osascript -e "display notification \"$1\" with title \"Intellidock\""
fi
}
check_clamshell_state()
{
local state=$(ioreg -r -k AppleClamshellState -d 4 | grep AppleClamshellState | head -1 | cut -f2 -d"=")
echo $state
}
set_dock_mode()
{
osascript -e "tell application \"System Events\" to set the autohide of the dock preferences to \"$1\""
}
usage()
{
echo ""
echo "usage: $0 [OPTIONS]" 1>&2;
echo ""
echo "OPTIONS:"
echo "-n to enable notifications"
echo "-l to enable logging. You can also provide a path for the log file: $0 -l ~/logfiles/"
echo "Default logfile location is the current directory."
exit 1;
}
format_path()
{
# removes possible / at the end
str=${1%/}
echo "$str"
}
LOG_PATH="$(pwd)"
FIRST_TIME=1
ENABLE_LOGGING=0
ENABLE_NOTIFICATIONS=0
# Previous value of the clammshell mode command. values: Yes or No.
PREVIOUS_CLAMSHELL_STATE=$(check_clamshell_state)
# Check if Intellidock is already running
for pid in $(pgrep -f Intellidock.sh); do
if [ $pid != $$ ]; then
log "info: Intellidock is already running with PID $pid"
exit 1
fi
done
log "info: Running with PID $$"
# parse arguments
while getopts ":nl:" o; do
case "${o}" in
n)
ENABLE_NOTIFICATIONS=1
;;
l)
ENABLE_LOGGING=1
LOG_PATH=$OPTARG
# fix for optional path parameter taking -n option as "parameter"
if [ "$LOG_PATH" == "-n" ]; then
ENABLE_NOTIFICATIONS=1
LOG_PATH=$(pwd)
log "info: no log path supplied. Will use current directory for log file."
elif ! [ -d $LOG_PATH ]; then
ENABLE_LOGGING=0
log "error: $LOG_PATH is not a valid directory"
usage
fi
;;
:)
# getopts doesn't support optional arguments,
# when an optional argument is left out, it returns :,
# we check the $OPTARG here if it matches.
if [ $OPTARG == "l" ]; then
ENABLE_LOGGING=1
log "info: no log path supplied. Will use current directory for log file."
fi
;;
\?)
log "error: invalid option -$OPTARG"
usage
;;
esac
done
shift $((OPTIND-1))
if [ $ENABLE_LOGGING -eq 1 ]; then
LOG_PATH=$(format_path $LOG_PATH)
# check for write permissions
if ! [ -w "$LOG_PATH/" ]; then
ENABLE_LOGGING=0
log "error: unable to write to $LOG_PATH/"
exit 3
fi
log "info: logging to $LOG_PATH/Intellidock.log"
else
log "info: logging: off"
fi
if [ $ENABLE_NOTIFICATIONS -eq 1 ]; then
log "info: notifications: on"
else
log "info: notifications: off"
fi
# Sleep always returns true, so we can use it as a loop condition.
while sleep 3; do
# Command for checking if Device is in "clamshell mode"
clamshell_state=$(check_clamshell_state)
# First we check if clamshell state is different.
if [ $PREVIOUS_CLAMSHELL_STATE != $clamshell_state ] || [ $FIRST_TIME -eq 1 ]; then
# If clamshell state was different, we update the PREVIOUS_CLAMSHELL_STATE for next check
PREVIOUS_CLAMSHELL_STATE=$clamshell_state
FIRST_TIME=0
# If Macbook is in clamshell mode, disable autohide dock
if [ $clamshell_state == "Yes" ]; then
log "info: Clammshellmode: yes, Autohide: off"
set_dock_mode "false"
notification "Device: open, Autohide: off"
# If not in clamshell mode, enable autohide dock
else
log "info: Clamshellmode: no, Autohide: on"
set_dock_mode "true"
notification "Device: closed, Autohide: on"
fi
fi
done
exit 0