Casting a dialog string as a date for a script

I’m still trying to figure out Applescript, and am not quite there yet (Perl is my toy of choice). I’m trying to write a variant of the example app “Countdown Timer”. My main change is that I want to use user-defaults to manage the default amount of time to put on the counter via a preference panel. I’m really having two problems, one is understanding what the heck the countdown timer thinks its doing with all the data manipulation in the first place, the second is trying to get the string I enter and store for a time “01:45:00” to cast as a date that I can then feed into this bizzarre script.

I enclose the Countdown Timer source for grins:

(* ===== Properties ===== *)

property countdown : false
property currentDate : 0
property startDate : 0
property endDate : 0

(* ===== Event Handlers ===== *)

on launched theObject
– Show the window
set visible of window “main” to true

-- Display an alert (as a sheet) asking for the amount of time in the HH:MM:SS format
display dialog "Enter the amount of time for the countdown timer:" default answer "00:00:05" attached to window "main"

end launched

on dialog ended theObject with reply withReply
– See if the “OK” button has been clicked
if button returned of withReply is “OK” then
– Save the current date for display purposes

	set currentDate to date (text returned of withReply)
	
	-- Save the start date
	set startDate to current date
	
	-- And determine the end date (start date + the countdown timer)
	set endDate to startDate + (time of currentDate)
	
	-- Update the contents of the text field
	set contents of text field "display" of window "main" to currentDate
	
	-- And let the processing in the idle event handler begin
	set countdown to true
end if

end dialog ended

on idle theObject
– See if we are ready to start counting down
if countdown then
– If the required amount of time has elapsed then display our dialog
if (current date) is greater than endDate then
set countdown to false
display alert “Time’s Up!”
else
– Otherwise determine how much time has elapsed (for display purposes)
set elapsedTime to (current date) - startDate

		-- Update the display
		set contents of text field "display" of window "main" to currentDate - elapsedTime
	end if
end if

-- We want to update the idle event every second, so we return 1
return 1

end idle

Sorry, should have added that this is an AppleScript Studio application.

This thread should be in the AppleScript Studio forum.

You can take a look at a demo app I put together a few months ago:

http://homepage.mac.com/jonn8/as/dist/Countdown.hqx

Jon

If you just want an incredibly basic timer where you tell AppleScript to wait x number of hours, minutes, & seconds before doing something, try this:

set the_time to text returned of (display dialog "Enter a time in HH:MM:SS" & return & return & "(The timer will begin as soon as you hit 'OK'. Make sure you only enter numbers and colons, no spaces.)" default answer "00:00:10" buttons {"Cancel", "OK"} default button 2 with icon 1)
set old_delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set {the_hours, the_minutes, the_seconds} to text items of the_time
set AppleScript's text item delimiters to old_delim
set {the_hours, the_minutes, the_seconds} to {(the_hours as integer) * hours, (the_minutes as integer) * minutes, (the_seconds as integer)}
set end_time to (current date) + the_hours + the_minutes + the_seconds
repeat
	if (current date) is greater than or equal to end_time then exit repeat
	do shell script "sleep  1"
end repeat
display dialog "Time's up!" buttons {"OK"} default button 1 with icon 1 giving up after 10

Jon

Thanks for your help. For some reason that I just don’t understand, I’m no longer having the problem casting data today. The sad thing is that I really don’t understand what the problem was.

Essentially I’m using the following code snippet:

set tempVar to “00:00:10” – tempVar represents 10 seconds
set currentDate to date tempVar - text cast of currentDate will now be “Thursday March 11th, 2004 12:00:10am”

Yesterday when I tried this I kept getting Internal script errors, or can;t create command errors. What do these mean, why did I get them, and why don’t I get them now? I can’t think of anything substantive that I’ve c hanged, although there must be something.

Thanks,

Maddog