Automatic detection of reset OS X clock

I have a couple over 70 year old relatives using old Powerbooks which work fine, except for an annoying fluke: the computer’s clock battery seems to have died, so whenever the laptop’s battery dies, the clock resets to Jan 1st, 1970. This in turn causes OS X’s DHCP client to fail, so the computer cannot connect to the network anymore, so the NTP based network time reset fails as well.

This wouldn’t be such a problem, except I get the support calls for the network failing, and the relatives in question are old enough not to always remember to watch out the battery dying, nor checking the clock being reset.

So I created the following script today when fixing the “dead Internet”. It’s an ugly Applescript that detects if the clock has reset (year is less than 2011) and if so. resets the date to the last known one, and asks to reboot the computer to fix the network. I’d have assumed an OS X would be smart enough to do something like this on it’s own, but nope, you have to do this yourself.

If you want to use the following, save the script using Applescript Editor as an Application (without Startup Screen and Stay Open options) and add the app to the Startup Items of the user account.

if ((year of (current date)) is less than 2011) then

do shell script “systemsetup -setusingnetworktime off”

do shell script “mdls ~/Library/Preferences/RepairTime.txt | grep ‘kMDItemFSContentChangeDate’ | awk ‘/ = / {print $3}'”

if length of the result > 0 then

tell the result to set modDate to text 6 & text 7 & “:” & text 9 & text 10 & “:” & text 1 & text 2 & text 3 & text 4

else

set modDate to “01:20:2011”

end if

do shell script “systemsetup setdate ” & modDate

do shell script “systemsetup settime 13:00”

do shell script “systemsetup -setusingnetworktime on”

set answer to the button returned of (display dialog “Your computer clock seems to have reset. Press OK to restart so your network will work correctly.” buttons {“Cancel”, “Reboot”} default button 2)

if answer is “Reboot” then

tell application “Finder” to restart

end if

else

do shell script “touch ~/Library/Preferences/RepairTime.txt”

end if


Leave a Reply