-
Notifications
You must be signed in to change notification settings - Fork 2
/
setTimeZone.sh
184 lines (121 loc) · 6.73 KB
/
setTimeZone.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
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
<<ABOUT_THIS_SCRIPT
-----------------------------------------------------------------------
Written by: William Smith
Professional Services Engineer
Jamf
bill@talkingmoose.net
https://github.com/talkingmoose/Jamf-Scripts
Originally posted: January 1, 2017
Last updated: August 13, 2018
Purpose: When used with Self Service, enables a non-admin user
to change time zone on his or her Mac.
• The script will offer to set the time zone automatically
if it detects the time zone is set manually.
• Otherwise, the script will first present a short list
of commonly used time zones.
• If selected, the script will present a full set choices
based on UNIX time zones.
The script creates a log file in the user's ~/Library/Logs folder.
Except where otherwise noted, this work is licensed under
http://creativecommons.org/licenses/by/4.0/
"To script is admin.
To comment divine."
INSTRUCTIONS
1) Below, create a short list of common time zones. Include as many
as needed to make the script easier to use.
2) Numbers for each time zone in the short listmust be unique
and sequential beginning with "1".
3) Add this script to your Jamf Pro server.
4) Create an ongoing Self Service item with the script and
enabled for offline use.
-----------------------------------------------------------------------
ABOUT_THIS_SCRIPT
# short time zone list
name[1]="London (+0:00)"
zone[1]="Europe/London"
name[2]="US Atlantic (-5:00)"
zone[2]="America/New_York"
name[3]="US Central (-6:00)"
zone[3]="America/Chicago"
name[4]="US Mountain (-7:00)"
zone[4]="America/Denver"
name[5]="US Pacific (-8:00)"
zone[5]="America/Los_Angeles"
name[6]="Chennai (+5:30)"
zone[6]="Asia/Colombo"
# set up logging
# name of this script
currentScript=$( /usr/bin/basename -s .sh "$0" )
# get path to the current user's home folder
currentUserFolder=$( eval echo ~$( /usr/bin/logname ) )
# set log file path
logFile="$currentUserFolder/Library/Logs/$currentScript.log"
# functions
function logresult() {
if [ $? = 0 ] ; then
/bin/date "+%Y-%m-%d %H:%M:%S $1" >> "$logFile"
else
/bin/date "+%Y-%m-%d %H:%M:%S $2" >> "$logFile"
fi
}
# use this icon (Date & Time preference pane icon) in dialogs
dialogIcon="/System/Library/PreferencePanes/DateAndTime.prefPane/Contents/Resources/DateAndTime.icns"
# determine whether time zone is set automatically
setAutomatically=$( /usr/bin/defaults read /Library/Preferences/com.apple.timezone.auto Active )
if [ "$setAutomatically" = 0 ]; then # time zone is set manually, offer to set it automatically
logresult "Time zone is set manually." "Time zone is set automatically."
userResponse=$( /usr/bin/osascript -e "button returned of (display dialog \"Your Mac's time zone was manually set to $( date +%Z ). Would you like to try automatically setting it?\" buttons {\"Manual\", \"Automatic\"} default button {\"Automatic\"} with title \"Set Time Zone\" with icon file POSIX file \"$dialogIcon\")" )
else
logresult "Time zone is set manually." "Time zone is set automatically."
fi
if [ "$userResponse" = "Automatic" ]; then # set the time zone to update automatically
logresult "User has chosen to set time zone automatically."
# modify the time zone plist to update automatically
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool TRUE
logresult "Setting time zone automatically." "Failed setting time zone automatically."
# open the date & time system preference for the user to verify settings
# this is a workaround to force the Mac to update settings after changing the plist
/usr/bin/open "/System/Library/PreferencePanes/DateAndTime.prefPane"
/bin/sleep 2 # allow time to change settings
# notify the user time zone is set to update automatically
theCommand="display dialog \"Your system is now set to $( date +%Z ) timezone. Opening the Time Zone preference pane for you to verify settings.\" with title \"Set Time Zone\" with icon file POSIX file \"$dialogIcon\" buttons {\"OK\"} default button {\"OK\"}"
/usr/bin/osascript -e "$theCommand"
exit 0
fi
# if user chooses to manually set a time zone
logresult "User has chosen to set time zone manually."
# present the user the short list of time zones
listNames=$( /usr/bin/printf '%s\n' "${name[@]}" )
theCommand="choose from list (every paragraph of \"$listNames\") with prompt \"Choose your current time zone.\" OK button name \"OK\" cancel button name \"More Choices\" with title \"Set Time Zone\""
listChoice=$( /usr/bin/osascript -e "$theCommand" )
# determine which time zone the user selected from the list
choiceIndex=$( /usr/bin/sdiff <(/bin/echo "$listNames") <(/bin/echo "$listChoice") | /bin/cat -n | /usr/bin/grep -v "<" | /usr/bin/awk '{ print $1 }' )
if [[ "$listChoice" != false ]]; then # user has chosen a time zone from the short list
chosenTimeZone="${zone[$choiceIndex]}"
else # user has requested to see more time zones
# read system time zone list
timeZoneList=$( /usr/sbin/systemsetup -listtimezones )
# parse and list regions from time zone list
timeZoneRegion=$( /bin/echo "$timeZoneList" | /usr/bin/grep / | /usr/bin/awk -F '( |/)' '{ print $2 }' | /usr/bin/sort | /usr/bin/uniq )
theCommand="choose from list (every paragraph of \"$timeZoneRegion\") with prompt \"Choose your current world region.\" with title \"Set Time Zone\""
selectedRegion=$( /usr/bin/osascript -e "$theCommand" )
# parse and list cities for chosen region in time zone list
timeZoneCity=$( /bin/echo "$timeZoneList" | /usr/bin/grep "$selectedRegion" | /usr/bin/awk -F '(\/)' '{ print $2 }' | /usr/bin/sort | /usr/bin/uniq )
theCommand="choose from list (every paragraph of \"$timeZoneCity\") with prompt \"Choose the nearest city to you for world region $selectedRegion.\" with title \"Set Time Zone\""
selectedCity=$( /usr/bin/osascript -e "$theCommand" )
# set new time zone to Region/City format
chosenTimeZone="$selectedRegion/$selectedCity"
fi
# disable setting time zone automatically
/usr/bin/defaults write /Library/Preferences/com.apple.timezone.auto Active -bool False
# set the time zone manually to chosen time zone
/usr/sbin/systemsetup -settimezone "$chosenTimeZone"
logresult "Setting time zone to $chosenTimeZone." "Failed setting time zone to $chosenTime."
# open the date & time system preference for the user to verify settings
/usr/bin/open "/System/Library/PreferencePanes/DateAndTime.prefPane"
/bin/sleep 2 # allow time to change settings
# notify the user time zone has been set
theCommand="display dialog \"Your system is now set to $( date +%Z ) timezone. Opening the Time Zone preference pane for you to verify settings.\" with title \"Set Time Zone\" with icon file POSIX file \"$dialogIcon\" buttons {\"OK\"} default button {\"OK\"}"
/usr/bin/osascript -e "$theCommand"
exit 0