Hello Kel.
I liked your applet, but shouldn’t you have written that it must be exported as a “stay open applet”?
This is a different take on the problem of recording time. I use a script instead of an Applet. It works after the “finite state machine” principle, as it acts accordingly to which state it is currently in, much like your applet, but still not so, since it isn’t running at all times.
The script tracks the time in a property, so you can’t lock the script against modification I believe. You can start, pause and stop the “Stop Watch”.
I haven’t decided, if I should subtract the slack off the end time, or if I should leave a slack column.
How it cycles through the different states:
It starts off from the state “Stopped”, then it can only bekomme “Running”. From “Running”, it can become “Stopped” or “Paused”. From “Paused” it can become “Running” or “Stopped”. The code really doesn’t look very good, but it should work as expected.
property scriptTitle : "Stop Watch"
property state : "Stopped"
property t0 : 0
property elapsed : 0
property slack : 0
property observation : "Enter what to time:"
global clockIcon
# Copyright © 2015 McUsr, you may not post this as a work of your own on some webpage, or in a book.
-- http://macscripter.net/viewtopic.php?pid=179972#p179972
(*
Now considers daylightsavings time regardless of locale
This works as follows: if the time to gmt has increased, from when we started timing, then we must
subtract the difference, if the time has decreased, then we increase the time of the current lap
likewise.
*)
on run
set clockIcon to (path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:Clock.icns"
if state = "Stopped" then
tell application (path to frontmost application as text)
display dialog "Start StopWatch" default answer observation with title scriptTitle buttons {"Cancel", "Start"} cancel button 1 default button 2 with icon file clockIcon
end tell
-- We never pass this point if the user hits "Cancel"
set observation to text returned of result
set {state, t0, t0_ToGMT} to {"Running", (current date), (time to GMT)}
-- Changes the state to running so we don't enter this block before the script is stopped.
display notification "Clocking: " & observation with title scriptTitle
else if state = "Running" then
tell application (path to frontmost application as text)
display dialog "Pause or Stop: " & observation with title scriptTitle buttons {"Cancel", "Pause", "Stop"} default button 2 with icon file clockIcon
end tell
set btn to button returned of result
if btn is not "Cancel" then
-- Recording "lap"
set Te_ToGMT to (time to GMT)
set elapsed to elapsed + ((current date) - t0) + (t0_ToGMT - Te_ToGMT)
if btn = "Pause" then
-- starting to "slack"
set {state, slackStart, t0_ToGMT} to {"Paused", (current date), (time to GMT)}
display notification "Paused after " & my formatTime(elapsed) & "." with title scriptTitle subtitle observation
else if btn = "Stop" then
set state to "Stopped"
stopDialog(observation, elapsed, slack, scriptTitle)
end if
end if
-- We can come back to the state "running" from the state "Paused", or when we have started afresh again from the state "Stopped"
else if state is "Paused" then
tell application (path to frontmost application as text)
display dialog "Start or Stop: " & observation & "?" with title scriptTitle buttons {"Cancel", "Start", "Stop"} default button 2 with icon file clockIcon
end tell
set btn to button returned of result
if btn is not "Cancel" then
set Te_ToGMT to (time to GMT)
tell (current date)
set {slack, t0} to {slack + (it - slackStart) + (t0_ToGMT - Te_ToGMT), it}
end tell
if btn = "Start" then
set state to "Running"
set t0_ToGMT to (time to GMT)
display notification "Clocking continued at " & t0's time string with title scriptTitle subtitle observation
else if btn = "Stop" then
stopDialog(observation, elapsed, slack, scriptTitle)
set state to "Stopped"
set observation to "Enter what to time:"
end if
end if
-- we can only come back to this state from the state "Running", that is, if we don't stop the script from this "Paused" state.
end if
end run
on stopDialog(observation, obsTime, slack, scriptTitle)
set clockedTime to formatTime(obsTime)
set slackedTime to formatTime(slack)
set resultString to "Final time for:
" & observation & " was: " & clockedTime & "
Slack: " & slackedTime
tell application (path to frontmost application as text)
display dialog resultString with title scriptTitle buttons {"Clipboard", "Ok"} default button 2 with icon file clockIcon
end tell
if button returned of result is "Clipboard" then set the clipboard to resultString
resetVars()
-- we do wipe out the observation here
end stopDialog
on resetVars()
global observation, t0, elapsed, slack
set {observation, t0, elapsed, slack} to {"Enter what to time:", 0, 0, 0}
end resetVars
on formatTime(someSecs)
if someSecs ≥ 3600 then -- we have to consider hours
set tHours to someSecs div 3600
if tHours < 10 then
set tHours to "0" & tHours & ":"
else
set tHours to "" & tHours & ":"
end if
set someSecs to someSecs mod 3600
else
set tHours to ""
end if
set tMinutes to (text -2 thru -1 of ("0" & (someSecs div 60))) & ":"
set someSecs to someSecs mod 60
set tSecs to text -2 thru -1 of ("0" & someSecs)
return tHours & tMinutes & tSecs
end formatTime