G’day.
The following script, which I posted some time ago, no longer works under Leopard.
When I deliberately cancelled the dialog box in the second version of the script, and left the file open, I couldn’t close the ‘write’ being open, in this line.
close access TheFileName2
If I enclosed the file closing line in a try statement, the script declared that the file was still open
try
close access TheFileName2
try
Anybody else having problems?
FIRST example (used to work under Tiger)
-- Timer
-- Not bloody copyright
-- Just fooling around.
-- Some examples of how time strings can be manipulated,
-- cause I found examples hard to find.
-- by Santa
my Timer()
my displayInfo()
on Timer()
copy (current date) - (time to GMT) to tempDate -- For universal time
copy tempDate to KeepTheTime
-- For an invisible file, put a full stop in front of the name ie ".Timer"
set t to my ReadTheTime("Timer", KeepTheTime)
display dialog "elapsed : " & my setTheDaysHoursMinutesSeconds(KeepTheTime - t) buttons {"OK", "See elapsed time", "Reset Timer"}
set temp to button returned of the result
if temp = "Reset Timer" then
copy (current date) - (time to GMT) to tempDate -- For universal time
my SaveTheTime("Timer", tempDate)
my Timer()
else
if temp = "See elapsed time" then my Timer()
end if
end Timer
on displayInfo()
copy (current date) - (time to GMT) to tempDate -- For universal time
copy tempDate to KeepTheTime
set time of tempDate to 0
set dayelapsedtime to KeepTheTime - tempDate
set day of tempDate to 1
set monthelapsedtime to KeepTheTime - tempDate
set month of tempDate to 1
set yearlyelapsedtime to KeepTheTime - tempDate
display dialog "Universal time" & return & return & ¬
"Since midnight, " & dayelapsedtime & " seconds have elapsed." & return & my setTheDaysHoursMinutesSeconds(dayelapsedtime) & return & return & ¬
"Since 12.00 am on the first of the month, " & monthelapsedtime & " seconds have elapsed." & return & my setTheDaysHoursMinutesSeconds(monthelapsedtime) & return & return & ¬
"Since 12.00 am January 1, " & yearlyelapsedtime & " seconds have elapsed." & return & my setTheDaysHoursMinutesSeconds(yearlyelapsedtime) buttons {"OK", "Refresh information"}
if button returned of the result = "Refresh information" then my displayInfo()
end displayInfo
on ReadTheTime(Filename, KeepTheTime)
tell application "Finder"
set TheFileName to (path to desktop folder as string) & Filename
try
set theSavedTime to (read file (TheFileName) as date)
return theSavedTime
on error
my SaveTheTime(Filename, KeepTheTime)
return KeepTheTime
end try
end tell
end ReadTheTime
on SaveTheTime(Filename, KeepTheTime)
tell application "Finder"
set TheFileName to (path to desktop folder as string) & Filename
end tell
try
set fRef to (open for access file TheFileName with write permission)
end try
set eof fRef to 0
--try
display dialog TheFileName & KeepTheTime
write KeepTheTime to fRef
--end try
close access fRef
end SaveTheTime
on setTheDaysHoursMinutesSeconds(theSeconds)
set TheTotalDays to theSeconds div days
set TheTotalHours to (theSeconds - (TheTotalDays * days)) div hours
set thetotalminutes to (theSeconds - ((TheTotalDays * days) + (TheTotalHours * hours))) div minutes
set theTotalSeconds to theSeconds - ((TheTotalDays * days) + (TheTotalHours * hours) + (thetotalminutes * minutes))
set the AnswerString to ""
if TheTotalDays > 0 then set the AnswerString to the AnswerString & TheTotalDays & " Days "
if TheTotalHours > 0 then set the AnswerString to the AnswerString & TheTotalHours & " Hours "
if thetotalminutes > 0 then set the AnswerString to the AnswerString & thetotalminutes & " Minutes "
set the AnswerString to the AnswerString & theTotalSeconds & " Seconds."
return the AnswerString
end setTheDaysHoursMinutesSeconds
SECOND example
-- Timer
-- Not bloody copyright
-- Just fooling around.
-- Some examples of how time strings can be manipulated,
-- cause I found examples hard to find.
-- by Santa
global TimerFile
set TimerFile to "Time3"
my Timer()
my displayInfo()
on Timer()
copy (current date) - (time to GMT) to tempDate -- For universal time
copy tempDate to KeepTheTime
-- For an invisible file, put a full stop in front of the name ie ".Timer"
set t to my ReadTheTime(TimerFile, KeepTheTime)
display dialog "elapsed : " & my setTheDaysHoursMinutesSeconds(KeepTheTime - t) buttons {"OK", "See elapsed time", "Reset Timer"}
set temp to button returned of the result
if temp = "Reset Timer" then
copy (current date) - (time to GMT) to tempDate -- For universal time
my SaveTheTime(TimerFile, tempDate)
my Timer()
else
if temp = "See elapsed time" then my Timer()
end if
end Timer
on displayInfo()
copy (current date) - (time to GMT) to tempDate -- For universal time
copy tempDate to KeepTheTime
set time of tempDate to 0
set dayelapsedtime to KeepTheTime - tempDate
set day of tempDate to 1
set monthelapsedtime to KeepTheTime - tempDate
set month of tempDate to 1
set yearlyelapsedtime to KeepTheTime - tempDate
display dialog "Universal time" & return & return & ¬
"Since midnight, " & dayelapsedtime & " seconds have elapsed." & return & my setTheDaysHoursMinutesSeconds(dayelapsedtime) & return & return & ¬
"Since 12.00 am on the first of the month, " & monthelapsedtime & " seconds have elapsed." & return & my setTheDaysHoursMinutesSeconds(monthelapsedtime) & return & return & ¬
"Since 12.00 am January 1, " & yearlyelapsedtime & " seconds have elapsed." & return & my setTheDaysHoursMinutesSeconds(yearlyelapsedtime) buttons {"OK", "Refresh information"}
if button returned of the result = "Refresh information" then my displayInfo()
end displayInfo
on ReadTheTime(Filename, KeepTheTime)
tell application "Finder"
set TheFileName to (path to desktop folder as string) & Filename
try
set theSavedTime to (read file (TheFileName) as date)
return theSavedTime
on error
my SaveTheTime(Filename, KeepTheTime)
return KeepTheTime
end try
end tell
end ReadTheTime
on SaveTheTime(Filename, KeepTheTime)
set TheFileName to ((path to desktop as string) & Filename)
set TheFileName2 to alias ((path to desktop as string) & Filename)
close access TheFileName2
set fRef to (open for access file TheFileName with write permission)
set eof fRef to 0
display dialog KeepTheTime as string
write KeepTheTime to fRef
try
close access fRef
end try
end SaveTheTime
on setTheDaysHoursMinutesSeconds(theSeconds)
set TheTotalDays to theSeconds div days
set TheTotalHours to (theSeconds - (TheTotalDays * days)) div hours
set thetotalminutes to (theSeconds - ((TheTotalDays * days) + (TheTotalHours * hours))) div minutes
set theTotalSeconds to theSeconds - ((TheTotalDays * days) + (TheTotalHours * hours) + (thetotalminutes * minutes))
set the AnswerString to ""
if TheTotalDays > 0 then set the AnswerString to the AnswerString & TheTotalDays & " Days "
if TheTotalHours > 0 then set the AnswerString to the AnswerString & TheTotalHours & " Hours "
if thetotalminutes > 0 then set the AnswerString to the AnswerString & thetotalminutes & " Minutes "
set the AnswerString to the AnswerString & theTotalSeconds & " Seconds."
return the AnswerString
end setTheDaysHoursMinutesSeconds