View Full Version : Mac scripting problem seeks solution
ktinkel
05-20-2005, 04:48 AM
A friend uses the last beta version of Fontographer (as do I, so I have an interest in this). It runs only in Classic, and is time-bombed to stop working at a certain, long past, date. So to run it, the user needs to set the clock back, launch the application, then restore the accurate date.
Is there a way to do this with AppleScript (or another scripting language — JavaScript, maybe)?
Here is my friend’s question:Can someone tell me exactly how to do the simplest thing?
I want a script that when I press the button, it will
1. Set the date back to Jan 15, 1997
2. Execute Fog 5 beta 2
3. Set the date back to 'auto'. Is there any hope? If so, tips and advice welcome.
A friend uses the last beta version of Fontographer (as do I, so I have an interest in this). It runs only in Classic, and is time-bombed to stop working at a certain, long past, date. So to run it, the user needs to set the clock back, launch the application, then restore the accurate date.
Is there a way to do this with AppleScript (or another scripting language — JavaScript, maybe)?
I can think of a couple of approaches in Applescript, neither of them particularly graceful... Not that it matters in a script.
I'm kind of hoping Shane jumps in shortly, but if he doesn't I'll try and sketch out something.
Are they booted into OS X or OS 9? What version? Do they have administrator privileges on the Mac?
ktinkel
05-20-2005, 04:18 PM
I can think of a couple of approaches in Applescript, neither of them particularly graceful... Not that it matters in a script.
I'm kind of hoping Shane jumps in shortly, but if he doesn't I'll try and sketch out something.
Are they booted into OS X or OS 9? What version? Do they have administrator privileges on the Mac?It is just a guy — he has OS X and Classic (not sure whether he also has a machine running OS 9). He certainly has admin privileges (there’s no one else there!)
Thanks for any help you can provide.
What version of OS X? It's important to know. One of the options I can envision involves GUI scripting.
ktinkel
05-20-2005, 06:07 PM
What version of OS X? It's important to know. One of the options I can envision involves GUI scripting.I will have to ask. He may be way back on 10.3.4.
Thanks.
Shane Stanley
05-20-2005, 07:29 PM
This is pretty quick-and-dirty:
-- set these to whatever you want
set newDay to "01"
set newMonth to "01"
set newYear to "2001"
-- get time/date now
set timeNow to do shell script "date -u '+%m%d%H%M%C%y'"
-- set new date
set newdate to newMonth & newDay & (text 5 thru 8 of timeNow) & newYear
do shell script "date -u " & newdate with administrator privileges
-- open app
try
tell application "The App"
activate
end tell
end try
-- reset date
do shell script "date " & timeNow with administrator privileges
Put in the name of the real app where it says TheApp, paste it onto a Script Editor window, and save it as an application. The Mac will lose up to a minute each time it's run, but that shouldn't be a big deal.
Shane
This is pretty quick-and-dirty:
Ahhh... The master himself at work.
Nothing wrong with Q&D, sirrah.
ktinkel
05-21-2005, 03:37 AM
What version of OS X? It's important to know. One of the options I can envision involves GUI scripting.Okay. He is on 10.3.8, is thinking of moving to 10.3.9, and says he may shortly move to Tiger.
He is so interested in being able to control this clock-setback thing, though, that that might influence his decision (and I know he once started over, went all the way back to an early version of Panther — and I bet he’d be willing to do that again if it means he can have a script that works).
Thanks for your help.
ktinkel
05-21-2005, 04:08 AM
This is pretty quick-and-dirty:
-- set these to whatever you want
set newDay to "01"
set newMonth to "01"
set newYear to "2001"
-- get time/date now
set timeNow to do shell script "date -u '+%m%d%H%M%C%y'"
-- set new date
set newdate to newMonth & newDay & (text 5 thru 8 of timeNow) & newYear
do shell script "date -u " & newdate with administrator privileges
-- open app
try
tell application "The App"
activate
end tell
end try
-- reset date
do shell script "date " & timeNow with administrator privileges
Put in the name of the real app where it says TheApp, paste it onto a Script Editor window, and save it as an application. The Mac will lose up to a minute each time it's run, but that shouldn't be a big deal.
ShaneThanks, Shane! that’s fabulous!
I sent it to my friend. It looks a little confusing to me (assuming the lines that begin with -- are comments?) but let’s see how it goes for him. (Not sure if he has ever done any scripting, but he is generally smart about computer stuff.)
If he's on Pather, and looking to go to Tiger, he's fine. Let us know how Shane's script works.
Shane Stanley
05-21-2005, 10:53 PM
It looks a little confusing to me (assuming the lines that begin with -- are comments?) but let’s see how it goes for him.
It is pretty unfriendly. There's no built-in AppleScript command for changing the time, so it resorts to the Unix command, just using AppleScript to run it. The beauty of working on a Unix box.
Yes, the -- lines are comments.
Shane
ktinkel
05-22-2005, 04:39 AM
It is pretty unfriendly. There's no built-in AppleScript command for changing the time, so it resorts to the Unix command, just using AppleScript to run it. The beauty of working on a Unix box.
Yes, the -- lines are comments.Thanks. I haven’t heard from my friend. I’ll try to pester him later.
billtroop
05-23-2005, 11:18 AM
Shane, thanks for this - I have two problems: one is, the app asks for the admin password, well, no big deal, but the other is that I get the message "date: illegal time". What am I doing wrong? Many thanks, Bill
ktinkel
05-23-2005, 11:30 AM
Shane, thanks for this - I have two problems: one is, the app asks for the admin password, well, no big deal, but the other is that I get the message "date: illegal time". What am I doing wrong? Many thanks, BillHi, Bill — glad you could make it.
Shane, thanks for this - I have two problems: one is, the app asks for the admin password, well, no big deal, but the other is that I get the message "date: illegal time". What am I doing wrong? Many thanks, Bill
Bill,
If you open Terminal and type
date -u '+%m%d%H%M%C%y'
what result do you get?
Here (Canada) I get:
052319382005
Whch translates as: May 23, 1938 UT, 2005
So text 5 through 8 is 1938 (1:38 pm CST)
Shane Stanley
05-23-2005, 06:18 PM
Shane, thanks for this - I have two problems: one is, the app asks for the admin password
Right -- changing the time is considered a big enough issue to require authentication. You could hardcode your password if you're using Tiger by inserting 'user name "bill" password "xxx"' before 'with administrator privileges', but that's leaving your password exposed to anyone who has access to the script.
I get the message "date: illegal time". What am I doing wrong?
I'm not sure. There does seem to be an issue with time conversions to UTC, and I left out a "-u" in the last line. Try this version:
set newDay to "01"
set newMonth to "01"
set newYear to "2001"
-- get time/date now
set timeNow to do shell script "date -u '+%m%d%H%M%C%y'"
-- set new date
set newdate to newMonth & newDay & "0000" & newYear
do shell script "date -u " & newdate with administrator privileges
-- open app
try
tell application "Whatever"
activate
end tell
end try
-- reset date
do shell script "date -u " & timeNow with administrator privileges
Shane
Shane Stanley
05-23-2005, 06:20 PM
Whch translates as: May 23, 1938 UT, 2005
The result of the missing "-u " in the last line, I suspect.
Shane
billtroop
05-23-2005, 07:28 PM
Hi Shane, first, terminal gives 052403152005, so that should be OK, right?
In the first version of the program, execution stopped (if I read the signs aright) at the first 'do shell script' line, so Fog5 never executed.
In the second version of the program, the script still gives an error, but only on the last line.
I can tell that the program has executed (indeed, it seems to execute three times), but I don't think the date ever got set back, because it immediately closes.
Interestingly, after it has done that, which takes a few secs, I then get the authentication password request, and then immediately the message, 'date: illegal time'.
I don't mind if my login/password gets embedded into this script, if that would help debug it.
Sorry to give you all this trouble!
Is there any way of executing the script line by line? You can tell I know nothing about Applescript!!
billtroop
05-23-2005, 07:39 PM
Hi Shane, first, terminal gives 052403152005, so that should be OK, right?
In the first version of the program, execution stopped (if I read the signs aright) at the first 'do shell script' line, so Fog5 never executed.
In the second version of the program, the script still gives an error, but only on the last line.
I can tell that the program has executed (indeed, it seems to execute three times), but I don't think the date ever got set back, because it immediately closes.
Interestingly, after it has done that, which takes a few secs, I then get the authentication password request, and then immediately the message, 'date: illegal time'.
I don't mind if my login/password gets embedded into this script, if that would help debug it.
Sorry to give you all this trouble!
Is there any way of executing the script line by line? You can tell I know nothing about Applescript!!
billtroop
05-23-2005, 07:40 PM
Shane, one other thing, if I execute only the first seven lines of the script (v.2, including comment lines), the result is "020100001997" which is great, but the system date doesn't actually change.
Shane Stanley
05-23-2005, 09:17 PM
Is there any way of executing the script line by line? You can tell I know nothing about Applescript!!
It's kind of tricky because all AppleScript is doing is talking to the shell. But if you run it open in Script Editor with the Event Log showing in the bottom half of the window, it might give me more of a clue.
Shane
billtroop
05-24-2005, 07:26 AM
Shane, if I try to change the date from terminal in a simple manner, I always get "operation not permitted" so perhaps there is something more basic wrong. I understand that the date may only be changed by the superuser. Am I that? I am the only user, I am the administrator. Does that make me the wretched superuser?
Shane Stanley
05-24-2005, 04:09 PM
Shane, if I try to change the date from terminal in a simple manner, I always get "operation not permitted" so perhaps there is something more basic wrong. I understand that the date may only be changed by the superuser. Am I that? I am the only user, I am the administrator. Does that make me the wretched superuser?
You're an admin, but you still can't do it without authentication.
Did you get a chance to run the script in Script Editor and capture the Event Log?
Shane
billtroop
05-24-2005, 08:47 PM
Shane, one other thing, I got root established, so if I log on as root, I can execute one of those simple date commands - so that is one more thing out of the way, except that I have to be logged on as root before any of this is going to work, so it seems.
Shane Stanley
05-24-2005, 11:00 PM
except that I have to be logged on as root before any of this is going to work, so it seems.
...not if you use the script instead of going straight to Terminal.
Shane
billtroop
05-25-2005, 09:54 AM
OK. That is great info. Now I have to figure out how to do an event log properly!
Shane Stanley
05-25-2005, 03:49 PM
OK. That is great info. Now I have to figure out how to do an event log properly!
Click on the Event Log tab at the bottom of the script wondow, then click Run.
Shane
billtroop
05-26-2005, 09:34 PM
Hi Shane, many thanks for your continued efforts and patience! Here's my event log for 'v.2' of the script:
tell current application
do shell script "date -u '+%m%d%H%M%C%y'"
"052705302005"
do shell script "date -u 020100001997" with administrator privileges
"
date: illegal time"
In this script, I changed newmonth to 02, newyear to 1997 and application to offroad.ppc.opt, which is the actual name of the app in question. At one point I tried changing the order of %m etc. to go agree with something I tried researching, but it didn't work. At the moment, the script is exactly as you gave it to me, with the changes I noted. Many thanks, Bill
Shane Stanley
05-26-2005, 11:49 PM
I don't know why that's not working. There's a reference to needing to be a Unix superuser to run it, but the error message suggests a different problem.
Does running:
do shell script "date -u 0000" with administrator privileges
change the time?
Shane
billtroop
05-27-2005, 09:19 PM
Hi Shane, it actually DOES change the time!!! to 6 pm which I guess is 0000 GMT. I get this response: [I don't recall the previous scripts actually changing the time]
tell current application
do shell script "date -u 0000" with administrator privileges
"sudo: timestamp too far in the future: May 27 23:06:34 2005
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these two things:
#1) Respect the privacy of others.
#2) Think before you type.
"
billtroop
05-27-2005, 09:25 PM
Also, Shane, it runs differently each time you do it. For example, I now reset the time with sys preferences, using auto time. I run the one-line script you just got and get:
"AppleScript error Sat May 28 00:00:00: GMT 2005"
and this in the event log:
tell current application
do shell script "date -u 0000" with administrator privileges
"Sat May 28 00:00:00 GMT 2005"
HOWEVER, if I now run this script a second time,
I get the message I quoted before.
Something else, rather worrisome - it doesn't seem to matter what I type in the authentication box!!!!
billtroop
05-27-2005, 09:27 PM
I have tried it out some more and I was wrong - it does know when you type in the wrong password - though I really had the impression it missed it once.
Shane Stanley
05-27-2005, 09:30 PM
I have tried it out some more and I was wrong - it does know when you type in the wrong password - though I really had the impression it missed it once.
Sudo lets you do things as a superuser for a certain amount of time, which would explain this.
The rest, however, I'm at a loss to explain.
Shane
billtroop
05-27-2005, 09:35 PM
Shane, if I change 0000 to 00 in this line
set newdate to newMonth & newDay & "00" & newYear
I get a result which makes me think there is a different priority than expected in the string:
tell current application
do shell script "date -u '+%m%d%H%M%C%y'"
"052800042005"
do shell script "date -u 0201001997" with administrator privileges
"Mon Dec 31 20:37:00 GMT 2001"
and, remarkably, the date is set to December 31, 20:37 GMT 2001!!!
So I think we are very near solution? If I weren't so dense it would have occurred to me surely?
billtroop
05-27-2005, 10:28 PM
Shane, I'm a little closer to success. I changed two lines to make the year come first, so my script now reads:
set newDay to "07"
set newMonth to "02"
set newYear to "1997"
-- get time/date now
set timeNow to do shell script "date -u '+%C%y%m%d%H%M'"
-- set new date
set newdate to newYear & newMonth & newDay & "1234"
do shell script "date -u " & newdate with administrator privileges
-- open app
try
tell application "OffRoad.PPC.opt"
activate
end tell
end try
-- reset date
do shell script "date -u " & timeNow with administrator privileges
The result of this is that the date really does get changed to Feb 7, 1997, 5:34:00 am (MST)
However, it still gets hung up on that do shell script line. The results are:
tell current application
do shell script "date -u '+%C%y%m%d%H%M'"
"200505280624"
do shell script "date -u 199702071234" with administrator privileges
"Fri Feb 7 12:34:00 GMT 1997"
and the second time I run it the Event results are
tell current application
do shell script "date -u '+%C%y%m%d%H%M'"
"199702071235"
do shell script "date -u 199702071234" with administrator privileges
"sudo: timestamp too far in the future: May 28 00:24:49 2005
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these two things:
#1) Respect the privacy of others.
#2) Think before you type.
"
However, the first results are different:
tell current application
do shell script "date -u '+%C%y%m%d%H%M'"
"200505280627"
do shell script "date -u 199702071234" with administrator privileges
"Fri Feb 7 12:34:00 GMT 1997"
i.e., without all that other warning stuff. All this time, I have "set date and time automatically" and changing that doesn't seem to make a difference.
billtroop
05-27-2005, 10:30 PM
So, in sum, at least I have got it to change the date back to 1997, which in itself seems a miracle. What it won't now do is execute the program and set the date back. I have a feeling that the technique for saving the original date and time may be a problem too?
Shane Stanley
05-29-2005, 03:38 AM
So, in sum, at least I have got it to change the date back to 1997, which in itself seems a miracle. What it won't now do is execute the program and set the date back. I have a feeling that the technique for saving the original date and time may be a problem too?
It looks like you've cracked the format of time you need; now you just need to change the script to collect the current date and time in the same format.
But I think I have an idea about you're error, and it relates to the version of OS you're using. "with admin privileges" used to use a Unix command called "sudo", which would ask for authorisation and then give you carte blanche for 5 minutes. Because you're fiddling with the time itself, the 5-minute timer is getting confused.
Using sudo like this is a bit of a security hole, so it's since been changed so that the privileges are turned on for the single command in question, rather than for a time-frame.
I fear easiest solution might be an OS upgrade...
Shane
billtroop
06-02-2005, 09:14 AM
Shane, I am currently on 10.3.8, though I have a 10.3.2 system I could try out as well. But do you basically think I should upgrade to 10.4 for this? I have it, I just haven't installed it yet.
Shane Stanley
06-02-2005, 03:55 PM
Shane, I am currently on 10.3.8, though I have a 10.3.2 system I could try out as well. But do you basically think I should upgrade to 10.4 for this? I have it, I just haven't installed it yet.
I guess it depends how important it is to you. I've only tested it on 10.4 on my Mac, where it works fine.
Shane
billtroop
07-01-2005, 09:50 PM
Shane, it has taken me some time to reply as I had a very bad few weeks installing Tiger on my G4. (Check your memory with Tech Tool 2 or 3 before installing is a top tip; others are, make sure your system battery is replaced if the computer is more than two or three years old; use your original video card; remove all upgrades, PCI cards, peripherals.)
Anyway, your script works marvellously well now, as you predicted it would. My one remaining issue is that the time comes back to some completely different time zone. Simply running the date/time panel with 'automatic' checked fixes that, but it would be nice to get a more elegant solution one of these days.
In any case, it now all works. True, after struggling with Tiger so long, I did finally manage to destroy my data drive, but what is that? I didn't lose anything I didn't really need, but the moral is, backup frequently! I always tell people to do that, but never seem to be able to do it myself, except in the most desultory manner.
Many, many thanks, Shane! - Bill
Shane Stanley
07-02-2005, 10:27 PM
Bill,
It sounds like you had quite a ride! Glad you got where you wanted to (or close). I'm not sure what you can do about the zone business -- have you tried locking that setting in System Preferences?
Shane
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.