How Stopwatch 1.0 works

I was looking at Stopwatch 1.0 and want to know how it stores info to either start or stop
http://scriptbuilders.net/files/stopwatch1.0.html

My test script:


property timeThen : date

if timeThen is date then
	display dialog timeThen
	
	set timeThen to (current date) as text
	
	display dialog timeThen
else
	set timeThen to date
	display dialog "Else " & timeThen
end if

It states in the ReadMe that it “Continues “timing” through shut-downs”

So how does it store timeThen variable

Thanks
darock

Almost certainly in a pref file.

See the bit about lifetime in the “Scope of Variables and Properties” sub-section of the “Variables and Properties” section of the AppleScript Language Guide.

Properties and global variables can be (are usually) saved between runs of a script by the program that runs the script (the applet runner in the case of a script saved as an application, the SystemUIServer program in the case of the built-in script menu, or other launchers (Quicksilver, FastScripts, etc.)).

Your example script does not make much sense to me. Here is a “timer” example that demonstrates property persistence.

-- In Script Editor, use Script > Compile to reset the starting time.
-- The first run records the starting time.
-- Subsequent runs report the starting time and the elapsed time.
-- Any recompilation (even the implicit ones before saving or running an editing script) will make the next run restart the timer.

property timeThen : missing value

if timeThen is missing value then
	set timeThen to current date
	display dialog "Starting at " & timeThen
else
	set elapsed to (current date) - timeThen
	set s to "s"
	if elapsed is 1 then set s to ""
	display dialog "Started at " & timeThen & return & "(" & elapsed & " second" & s & " ago)"
end if

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.4 (4531.21.10, r53456)
Operating System: Mac OS X (10.4)

OK, here is one that supports hours, minutes, and seconds.

property timeThen : missing value
repeat
	if timeThen is missing value then
		set timeThen to current date
		display dialog "Starting at " & timeThen buttons {"Cancel", "OK"} default button {"OK"} cancel button {"Cancel"}
	else
		set elapsed to ((current date) - timeThen) as integer
		set theminutes to 0
		set thehour to 0
		repeat
			if elapsed > 59 then
				set theminutes to theminutes + 1
				set elapsed to elapsed - 60
			else
				exit repeat
			end if
		end repeat
		repeat
			if theminutes > 59 then
				set thehour to thehour + 1
				set theminutes to theminutes - 60
			else
				exit repeat
			end if
		end repeat
		set sss to "s"
		if thehour is 1 then set sss to ""
		set ss to "s"
		if theminutes is 1 then set ss to ""
		set s to "s"
		if elapsed is 1 then set s to ""
		if thehour is 0 then
			set dialog to "Started at " & timeThen & return & "(" & theminutes & " minute" & ss & " and " & elapsed & " second" & s & " ago)"
		else
			set dialog to "Started at " & timeThen & return & "(" & thehour & " hour" & sss & " and " & theminutes & " minute" & ss & " and " & elapsed & " second" & s & " ago)"
		end if
		set button_returned to the button returned of (display dialog dialog buttons {"Clear", "Hide", "OK"} default button {"OK"}) as string
		if button_returned is "Clear" then
			set timeThen to missing value
		else if button_returned is "Hide" then
			exit repeat
		end if
	end if
end repeat

These loops would probably be more readable as repeat while foo > 59 . end repeat, but the math can be done directly with with div and mod.

-- hours and mintues are predefined to 60 and 3600, respectively
set thehour to elapsed div hours
set theminutes to elapsed mod hours div minutes
set elapsed to elapsed mod minutes

Though, I would also recommend using a new variable (theseconds?) instead of reusing elapsed. Also consistently using the plural or singular form in the variable names is a good idea. The duplication and irregularity in the string building code could also be reduced with something like this:

-- (place handler somewhere at the top level)
to quantityPhrase(num, noun)
	set phrase to "" & num & " " & noun
	if num is 1 then return phrase
	return phrase & "s"
end quantityPhrase

set phrases to {}
if thehours is not 0 then set end of phrases to quantityPhrase(thehours, "hour")
if theminutes is not 0 then set end of phrases to quantityPhrase(theminutes, "minute")
if theseconds is not 0 ¬
	or length of phrases is 0 then set end of phrases to quantityPhrase(theseconds, "second")

if length of phrases is greater than 1 then set last item of phrases to "and " & last item of phrases
set text item delimiters to {", "}
set dialog to "Started at " & timeThen & return & "(" & (phrases as text) & " ago)"

Ours both return the same thing, so lets not bother.

???
I’m curious, Are you saying lets not bother to help improve code?

No, you can improve the code, but it does basically the same thing, so I won’t bother.

Thanks for the Replies

Been busy and forgot about my ???

Thank’s I never was sure what properties do, See them in scripts but never new what for.

better play and read more on that.

My Script was just to see what timeThen was holding.

I have wrote several timer scripts but wrote data to AppleWorks or .txt file or .pref or something, not knowing that all I had to do was set a property for a variable

but this has some potential, I’ve written several scripts that I could use this.

What kind of data can you save using Properties ?

thanks
darock

My guess is that any of the basic ˜value’ types (integer, real, boolean, date, alias, text/string/Unicode text, alias, file, and composites (list, record) of those) that are built into AppleScript would work fine. You might even be able to successfully store and retrieve script objects.

Objects of classes defined in applications or OSAX are probably a bit riskier. They might work, but I would guess that any object that is much more than an application specific ˜value’ object would break over the longer run (perhaps if its host application quits and restarts between uses or the application’s internal state otherwise changes enough so that the object is no longer valid).