A stopwatch

Any ideas for a stopwatch-
click, time starts…is running…click, time elapsed is displayed? I was wondering if there were any quick solutions.
SC

Hi Sitcom,

Do you mean something like this?

display dialog "click OK to start timer"
set timeThen to current date
display dialog "click OK to stop timer" giving up after 86400
--giving up after 24 hours
set timeDiff to (current date) - timeThen
--timeDiff is seconds elapsed.  Set into Hrs, Mins & Secs
set hrDiff to timeDiff div 3600
set timeDiff to timeDiff mod 3600
set minDiff to timeDiff div 60
set secDiff to timeDiff mod 60
--display result
display dialog "Time elapsed" & return & hrDiff & " Hours " & minDiff & " Minutes " & secDiff & " Seconds"

Best wishes

John M

That’s what I’m talking about, with a few mods. I should have been more specific. Is there a way to have the script go idle or wait after the “Start timer” prompt, so the second dialog box (asking to stop the time) does not launch until you call it back to trigger it? I’ve come up with some ways around it, I would just like to fish for some ideas…
And thank you!
SC

Hi Sitcom,

You could make the saved time a property of the script and check to see if it’s set. There’s definitely room for bells & whistles in this script (no pun intended).

property timeThen : date
if timeThen is date then
	display dialog "click OK to start timer"
	set timeThen to current date
	return -- stop app running
else
	display dialog "click OK to stop timer" giving up after 86400
	--giving up after 24 hours 
	set timeDiff to (current date) - timeThen
	--timeDiff is seconds elapsed.  Set into Hrs, Mins & Secs 
	set hrDiff to timeDiff div 3600
	set timeDiff to timeDiff mod 3600
	set minDiff to timeDiff div 60
	set secDiff to timeDiff mod 60
	set timeThen to date
	--display result 
	display dialog "Time elapsed" & return & hrDiff & " Hours " & minDiff & " Minutes " & secDiff & " Seconds"
end if

Saved as an application this should do what you ask.

John M

Very nice!
Question: Is making ‘timeThen’ a property what saves its value the next time the script is ran?
I hear bells, I hear whistles…
SC

Hi Sitcom

Yes, properties of scripts should be saved between runs on a compiled AS app. Recompiling the app (editing in Script Editor) will loose the property’s settings.

Best wishes

John M

Hi,

You could use a stay open app with a reopen handler. Something like this:

property r : true
global temp_date
on run
set cur_date to current date
if r then
set temp_date to cur_date
else
set elapsed_secs to cur_date - temp_date
display dialog elapsed_secs
end if
set r to not r
end run
on reopen
run
end reopen

You know Kel, that was my original intention. The solution that John came up with continues to time even when the computer is shut down! I tested that earlier by setting the timer, and shutting down :rolleyes: . When I booted up hours later and clicked the script’s icon, sure enough, the “Stop Timer” prompt came up. Then elapsed time was a couple of hours. That was a feature I didn’t plan on, which is why I logged in to check for ideas. The power of the think tank!!!
I updated my intial dialog to include a note for the timer and the final display of

my note
date started
date finished
time elapsed:
00:00:00
SC


--copy and paste to new Script Editor window
property timeThen : date
if timeThen is date then
	
	set UI to display dialog "                       Ready to begin timing." & return & "                   Make a note for your timer?" default answer ""
	set Mynote to text returned of UI
	
	set timeThen to current date
	return -- stop app running
else
	display dialog "Stop timer" giving up after 86400
	--giving up after 24 hours
	set timeDiff to (current date) - timeThen
	set FirstT to timeThen
	--timeDiff is seconds elapsed.  Set into Hrs, Mins & Secs
	set hrDiff to timeDiff div 3600
	set timeDiff to timeDiff mod 3600
	set minDiff to timeDiff div 60
	set secDiff to timeDiff mod 60
	set timeThen to date
	--display result
	display dialog Mynote & ":" & return & (FirstT as string) & " - " & ((current date) as string) & return & "Time elapsed -" & return & hrDiff & " Hours " & minDiff & " Minutes " & secDiff & " Seconds"
end if

Thanks again John!

Hi Sitcom,

It’s a pleasure.

One thing about your script, you might want to declare your variable “Mynote” as a property of the script, otherwise it will be lost between runs.

John M

Ah, yes. “My note” is one of those ‘whistles’ we joked about… A little note of what I’m timing each time. It’s not forgotten between runs though…Any thoughts on that?
It works perfectly so far!
:!: SC :!: