Is there a way to have Applescript set the system clock?

I have several Macs on a network that are using a database connected to a server. Each Mac client may have a different time on the system clock. These Macs are not connected to the internet so the clocks cannot be updated that way.

I want to know if there is a script (maybe a shell script) that can set the system clock. I can have the database client on each Mac supply the correct time and activate the script, I think.

Maybe there is a better way, but I think Apple has deactivated the ability to have System 10 coordinate the clock times on a network using Unix commands. Thanks.

When run as root, the date command can set the date and time on the machine.

The following script works well for setting the time using hours and minutes.

set boomerang to "perl -Ue '$< = $>;system (@ARGV)' "
set the_script to "date 536"
do shell script boomerang & the_script password "test" with administrator privileges

Is there a way to also set the seconds? I haven’t figured out the correct syntax for the date command.

The manpage for date(1) indicates that you could use 536.07 to set the time to 5 hours, 36 minutes and 7 seconds after midnight.

OK, here is some more information…

I used the following script to get the date in the correct format:

set the_script to "date \"+%Y%m%d%H%M.%S\""
set thisTime to do shell script the_script
log thisTime

I can set the date and time by using the following script, but the seconds are set to zero:

set boomerang to "perl -Ue '$< = $>;system (@ARGV)' "
set the_script to "date 200701170512.12"
do shell script boomerang & the_script password "test" with administrator privileges

I still have a problem with the setting the seconds. Any ideas?

The format you are using (year month day hours minutes . seconds) is only listed in the “LEGACY SYNOPSIS” section of the date(1) manpage. A few lines down, at the end of the “LEGACY DIAGNOSTICS” section, it says “For more information about legacy mode, see compat(5).” The compat(5) manpage tells me that you need to set the “COMMAND_MODE” environment variable to the value “legacy” to enable this “legacy mode” for the date command.

Or you could just stick the digits after the digits (before the dot and the digits) and forget about trying to use legacy mode and the odd return code that it will probably force you to deal with:

--set cmdToSetDate to "COMMAND_MODE=legacy date 200907020155.55" -- Will likely exit with code 2, which will cause an error to be thrown by <do shell script>.
set cmdToSetDate to "COMMAND_MODE=legacy date 200907020155.55 || { ec=$?; [ $ec != 2 ] && exit $ec; :; }" -- Exit codes 0 and 2 are OK, pass along anything else.
set cmdToSetDate to "date 070201552009.55" -- Just deal with the "odd" ordering.
do shell script cmdToSetDate with administrator privileges

All of those command variations successfully set the date to 2009-07-02 1:55:55 on my 10.4.11 system. The first throws an error even though it sets the date, see the “LEGACY DIAGNOSTICS” section of the manpage.

Unless you are running 10.4.0 or 10.4.1 you probably do not need your “boomerang”. Both the real and effective UIDs should already be 0 (root) for the commands run by do shell script . with administrator privileges (see AppleScript release notes for 10.4.2).

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4 Public Beta (4528.17)
Operating System: Mac OS X (10.4)

Thanks for your help. My computers are using OSX 10.3.9, so I may still need to continue with the boomerang segment, but I will check it without the boomerang. I think the easiest thing to do would be changing how I gather the initial time. So my initial script will be

set the_script to "date \"+%m%d%H%M%Y.%S\""
set thisTime to do shell script the_script
log thisTime

I will check this today and see if it works. Thanks again.

Perhaps not. The release notes segment I to which I linked indicates that the changes in 10.4.2 make sute that the EUID/RUID behavior of with administrator priviledges “matches the behavior in Mac OS X version 10.3.x”.

Maybe, but I have a doubt. The manpage from my system (10.4) and that from Apple’s website (covering 10.5) say that is the format used in “legacy mode”. The manpage that describes “legacy mode” says that it makes commands work (mostly) like they did in 10.3. Together, they imply that the format that 10.3’s date commands uses is not If so, you were already using the right format for your system. I would think it unlikely, but maybe 10.3’s date just does not support setting the seconds.

You should check your local [i]date/i manpage (man date in a Terminal window) to see what format it says to use”look on the first page under “SYNOPSIS”.

Are you trying to synchronize clocks between machines that can reach one another over a network? If you are unable to get date to work, there are other means to synchronize the clocks on one or more machines. NTP is the standard way to do this. Most people use NTP to get the actual current time (ultimately) from time standards (GPS, and/or other atomic clocks), but you could setup your own NTP server that offers whatever time you want. For strictly local networks there is also timed. Of course neither will be useful if you are just rolling back the clock on a single machine for testing or other purposes.