MacOS – AppleScript to set clock

applescriptclockmacmacostime

I need to set the Mac clock to a specific date and time, to perform some testing.

I'm new to AppleScript.

  • Is it possible to write a double-clickable script to set the clock?
    ( By “double-clickable”, I mean a script that appears to be an app, double-click from the Finder to launch, run, and quit. No need to open the AppleScript environment/tools. )
  • Does anyone care to post some example code?

Best Answer

The follow example AppleScript code uses the state of the NTP Daemon to toggle the Date/Time settings between two Date/Time states. By default, ntpd is running and needs to be turned off if a changed Date/Time is to stay and move forwards normally from that point. Otherwise, with ntpd running, the Date/Time will be reset to the correct values rather quickly.

You can save this AppleScript code as an application and when run, does one of two things after entering the proper credentials. It either sets the Date/Time to the values specified, or turns the NTP Daemon back on so the system will update the Date/Time to the correct values.

set ntpdPID to do shell script "pgrep ntpd; exit 0"
if ntpdPID is not "" then
    do shell script "systemsetup -setusingnetworktime Off -setdate '01:24:1984' -settime '03:00:00'" with administrator privileges
else
    do shell script "systemsetup -setusingnetworktime On" with administrator privileges
end if

This is the no fills version and the code can be modified as needed/wanted, however, since those needs/wants have not been defined, this is all I can offer.