how do I tell when 1 hour has elapsed between running scripts

G’day

I’m automating a warning email that sends a message to the relevant IT manager if my script finds a problem with the server, that it has to send copies of emails to.

Error trapping is easy, but if i send a message with every email that arrives, he’ll be pissed!

I’d like to send one message per hour, and have come up with the attached script.

However, the way that it works, if there is a fault at, say, 2.05 on day 1, and it send messages every hour for 3 hours (till 5.05), then gets fixed, then gets a breakdown at 5.10 then next day, it will wait until another hour from 5.05 goes past, before sending the first email for that day.

Is there anything such as getting total time in seconds from a certain date, or SOMETHING that will give me a better way of keeping the time?

I’ve tried saving the current time, but it saves as gibberish unless saved as a string or text, and as a string I can’t do time comparisons.

The script does not stay open, and cannot be made to stay open, it’s a folder action generated one.

Regards

Santa

PS Sorry if I’m full of questions in this forum, but I’m learning heaps from you guys!


my seeifhourhaselapsed()

on seeifhourhaselapsed()
	tell application "Finder"
		set CheckLastTime to 0
		set TheFileName to (path to desktop folder as string) & "DataTimeSave.dat"
		set theEntireSeconds to time of (current date) -- the day time in seconds
		try
			set CheckLastTime to (read file (TheFileName) using delimiter ",")
		end try
		try
			set TimeElapsed to theEntireSeconds - CheckLastTime
		on error
			set TimeElapsed to 3601
		end try
		if TimeElapsed > 3600 or (TimeElapsed > -23 * 3600 and TimeElapsed < 0) then
			set fRef to (open for access file TheFileName with write permission)
			set eof fRef to 0
			try
				write (theEntireSeconds as string) & "," to fRef
				close access fRef
			end try
			return true
		else
			return false
		end if
	end tell
	
end seeifhourhaselapsed

There’s an osax called getMilliSec that give the time in milliseconds from the UNIX epoch, i.e. it always has the same base. Its one and only command is set T to getmillisec. Returns something like 3.93831026E+8. Very little screwing around required to get an hour.

I’d recommend to use a launchd agent which triggers a script periodically

Or simpler, Cronnix

There’s also this Python script for time from the epoch:

set epoch to (do shell script "echo 'puts [clock seconds]' | tclsh")

There’s also this script by Nigel Garvey for chiming the hours (which I use to do exactly that) that you could modify:

on idle
	local h
	
	tell (time of (current date))
		set h to ((it div hours + 11) mod 12) + 1
		tell (it mod hours)
			if (it < 10) then
				-- If it's now less than ten seconds after an hour, chime.
				my chime(h)
			else if (it < 300) then
				-- Otherwise, if it's within the first five minutes, make a belated announcement.
				my waffle(h)
			end if
		end tell
		
		-- Idle until the next five-minute boundary.
		return (86399 - it) mod 300 + 1 -- ie. (days - (time of (current date)) - 1) mod (5 * minutes) + 1
	end tell
end idle

to chime(h)
	tell application "Play Sound"
		play (("" & (path to "dlib" from system domain) & "Sounds:" & "Glass.aiff") as alias) repeat (h - 1)
		quit
	end tell
end chime

on waffle(h)
	say "Oops! It's a little past " & h & " o'clock."
end waffle

I’d use the entire date, rather than just the time, like this:

my seeifhourhaselapsed()

on seeifhourhaselapsed()
	set TheFileName to (path to desktop folder as string) & "DataTimeSave.dat"
	set now to (current date)
	try
		set CheckLastTime to (read file (TheFileName) as date)
		set TimeElapsed to now - CheckLastTime
	on error
		set TimeElapsed to hours + 1
	end try
	if (TimeElapsed > hours) then
		set fRef to (open for access file TheFileName with write permission)
		try
			set eof fRef to 0
			write now to fRef
		end try
		close access fRef
		return true
	else
		return false
	end if
	
end seeifhourhaselapsed

Ooooh! I hate nitpicking people’s posts! :mad: getMilliSec gives the time in milliseconds since the computer was switched on or restarted. :slight_smile:

You do not! :lol:, and as you know well, I don’t mind a bit; every nit picked is a learning experience for both me and others who follow the thread.

Thankg Nigel

The bit that eluded me was (read file (TheFileName) as date)

Regards

Santa

You’ve stored the previous time as an unreadable date code in the file with this line: “write now to fRef”. To do the comparison you need a date again:

set Now to (current date)
set F to open for access ((path to desktop folder as text) & "myDat.txt") with write permission
write Now to F  -- you won't be able to read the result in the file
close access F
set oldNow to read file ((path to desktop folder as text) & "myDat.txt") as date
-- the as date assures recovering the data as a date.