I am making a simple alarm clock. I would like a dialog to display something like: “You have” xamountoftime " left to sleep"
and I would like that dialog to refresh the xamountoftime every 30 seconds or so.
Is that possible? Please help. This is very much appreciated.
Thanks.
You cannot change the text of a dialog window once it is displayed.
You could do something like this though using ‘giving up after’ in the display dialog command.
repeat with x from 1 to 60
set theText to "The current time is: " & (time string of (current date))
display dialog theText giving up after 1 -- second
end repeat
The above displays the current time, refreshing every second for 60 seconds.
with plain vanilla AppleScript it’s not possible to just refresh an existing dialog, but
you can occasionally display the dialog, which disappears after a certain amount of time.
This is a very primitive example:
set xamountoftime to 60
repeat while xamountoftime > 0
display dialog ("You have " & xamountoftime & " seconds left to sleep") giving up after 2
delay 3
set xamountoftime to xamountoftime - 5
end repeat
beep
Here’s another rough example in an idle handler. You save idle handlers as stay open applications.
global target_date
--
on run
display dialog "Enter number of minutes:" default answer "5"
set m to (text returned of result) as integer
set target_date to (current date) + m * minutes
end run
--
on idle
set cur_date to (current date)
if cur_date > target_date then
beep 10
quit
else
set secs_left to (target_date - cur_date)
set h to secs_left div hours
set secs_left to secs_left mod hours
set m to secs_left div minutes
set secs_left to secs_left mod minutes
display dialog "You have " & h & ":" & m & ":" & secs_left & " time to sleep." giving up after 30
end if
return 1
end idle
Adding leading zeros to the time migh make it nicer. You might want to get into AppleScript Studio where it’s more fun making timers, alarms, clocks, etc.