Startup script to set date and time

I have an old tangarine iBook running 10.3.9 which I’ve passed on to someone to use just for emailing. Unfortunately, the battery is dead and not worth replacing so the date and time is not retained. Is there a simple script that can change the date to say, December 2005 at startup? Something I can change regularly to the correct month?

Do you have “Set date and time automatically” checked in the Date & Time preference panel? Doesn’t that do it?

There isn’t an always-on connection to the Net. In order to get it to work, they’d have to dial up solely to correct the date and time.

You can set the date & time (together) using a shell script, but that will soon be wrong as the months go by. See man date in the terminal. In addition, though, you can’t get or send e-mail without a connection and when you connect, the time will get set.

Thanks Adam, but I’m not at all familiar with AppleScript so I would need a ready-made script and an explanation of how to set it up to work at startup. Can you point me in the right direction?

You could try something like this:

do shell script "date 12`date +%d%H%M`05" with administrator privileges

12 is the month, and 05 is the year. Change as needed.

Note that using the date command to change the system date/time requires administrator privileges. In it’s current form, you’ll be asked for an admin name and password. If you want to avoid that dialog, you’ll have to supply a name and password inside the script.

do shell script "date 12`date +%d%H%M`05" user name "your username" password "your password" with administrator privileges

Thanks Bruce. I’ll give it a try. Would I just add the resulting script to the startup items to get it to work automatically? The person who has the iBook now is not at all Mac-savvy.

Yes, if you save it as an application.

Hi again Bruce. I’m getting syntax erron on the second scriptlet:
A property can’t go after this “”"

Assume you mean this

do shell script "date 12`date +%d%H%M`05" user name "your username" password "your password" with administrator privileges

To make sure you’ve got it typed right, open the script editor and then click on the line
" > Open this scriptlet in you editor" and the scriptlet will appear in the Script editor window. Edit out “your username” and replace it with your actual user name, same for password. Try that.

I tried both clicking and copying but I’m still getting the syntax error when I compile:
A property can’t go after this “”".
with the following highlighted:
" user name

Anyone got any other ideas about this?

I recently had the same problem Trash Man describes (using 10.3.9).
I found that it went away when I omitted the user info and just had the password
so…
do shell script “command” password “mypassword” with administrator privileges

(no escaping of quotes was necessary)

I realise this might not help if you need the script to login as a different user but it works otherwise.

Hello, I’m just beginning scripting with OS X and I’m searching a way to set date with a script. The script in this topic helped me but I would like to change day month and year in the same time. Do you know the syntax to do it with this script ?

Hello, and welcome to MacScripter!

You’ll have to open a Terminal window and enter “man date” to see the format, then you’ll see that you must pass a number string.

As a matter of fact, do try it out in the Terminal, and don’t try to write the script, using the snippets above before you before you have managed to do it from there.

Then you’ll need a dialog of some kind to enter the values you have chosen for the dates.

Those numeric values, doesn’t have to be regarded as numbers, but a check that they are within a valid range is a nice touch.

Then you concatenate the full number string, create a do shell script as above, and execute it.

You will find most of the things you are searching for here.

Good Luck!

on newer system versions (≥ 10.5) I’d prefer

systemsetup with -setdate and -settime flags