0

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?
  • You said, "Is it possible to write a double-clickable script to set the clock?", what's a "double-clickable script"? – user3439894 Aug 12 '17 at 17:07
  • 1
    @user3439894 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. – Basil Bourque Aug 12 '17 at 17:28
2

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.

0

@user3439894 I had to modify your code to work on Mojave. ntpd is no longer being used, and apparently disabling the networktime and setting the time have to be done as 2 separate commands.

set ntpdPID to do shell script "systemsetup -getusingnetworktime; exit 0" with administrator privileges
if ntpdPID is "Network Time: On" then
    do shell script "systemsetup -setusingnetworktime off" with administrator privileges
    do shell script "systemsetup -setdate '01:24:1984' -settime '03:00:00'" with administrator privileges
else
    do shell script "systemsetup -setusingnetworktime on" with administrator privileges
end if

Hope this helps someone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .