6

I need to check programmatically whether Set Date and Time automatically is checked or not on a Mac System Preferences.

I didn't find any .plist file that contained this information, can anybody tell me where I can find this information?

5
  • 1
    I suspect that there may be a better programmer-oriented answer to this question. Could I recommend that you flag this post and request it to be migrated to StackOverflow? Commented Dec 4, 2011 at 23:44
  • @JasonSalaz This would be off-topic at Stack Overflow.
    – Lri
    Commented Dec 5, 2011 at 4:49
  • Programming/API/SDK questions are not off topic at Stack Overflow. "I need to check programmatically" says the OP. Commented Dec 5, 2011 at 10:08
  • I suppose the question I would ask first is... What method you want to use to hold of the information, and in what format. I can think of at least two ways of getting the info using AppleScript, one using the file and plist read functions, and the second using the System Preferences AppleScript interface itself. The second method could also provide a method to set the option if it is not set, however I do not believe that is possible if the security on the pane is activated. It shouldn't take long to rig up a script to do it either.
    – Stu Wilson
    Commented Jan 11, 2012 at 23:20
  • I'm very tempted to edit this to make it more on-scope, but much of the comments would then seem out of place. Perhaps I can answer it to help clarify what is on-topic here and what is not.
    – bmike
    Commented Jul 2, 2012 at 14:57

3 Answers 3

1

The end result of the "Set Date and Time Automatically" checkbox is that the ntpd daemon gets started by launchd.

So you could check for the presence of the file /var/run/ntpd.pid or presumably check if ntpd is running as a process. This is all good and fine for the site, but your second part to the question on how to programmatically retrieve this status is basically off-topic here.

We do allow limited AppleScript, Automator, and shell scripting programming questions. The full scope of developer questions (especially with new OSX restrictions such as sandboxing) or code level Q&A on how to program are best asked on https://stackoverflow.com/ rather than here.


With that out of the way - here's the back story about why this actually a fairly complicated question. Launchd is responsible for starting and stopping ntpd time keeping daemon when you toggle that switch and rather than load or unload the configuration file for that "job", the tool instead has an internal override plist file that has a true/false status for certain jobs to be disabled even though they should normally run. When you toggle the System Preference checkbox in Date & Time, this file changes Disabled key value at the end of the file to be or when automatic timekeeping is off or on (respectively).:

From /private/var/db/launchd.db/com.apple.launchd/overrides.plist

    <key>org.ntp.ntpd</key>
    <dict>
            <key>Disabled</key>
            <false/>
    </dict>
1

I don't know of a purely programmatic way of getting it, but the systemsetup command can do it:

$ systemsetup -getusingnetworktime
Network Time: On
1
  • This will set the NTP server for your Mac "systemsetup -setnetworktimeserver <ntpserver>" and "systemsetup -getnetworktimeserver" to get the current time server.
    – grantc
    Commented Apr 3, 2013 at 6:33
0

I found this discussion on the discussions fora of Apple. It describes how to get the time in the menu bar, but with it also how to get to the plist programmaticly.

4
  • Can you please add more information on your answer? And add the website as a source? Commented Oct 5, 2011 at 14:08
  • sure, no problem.
    – Michiel
    Commented Oct 5, 2011 at 14:17
  • Thanks for your reply,but com.apple.GlobalPreferences.plist doesnot exists on my box in any Library/Preferences locations
    – Akbar
    Commented Oct 7, 2011 at 6:55
  • 1
    That's probably because the file is called /Library/Preferences/.GlobalPreferences.plist
    – nohillside
    Commented Nov 4, 2011 at 17:46

You must log in to answer this question.

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