How do i save a variable as a time?

how do i save a variable as a time
i tried

display dialog “when do you want to quit?”
set time_to_quit to the text returned of result as (time)
if time_to_quit = (time) then exit repeat
end repeat
tell app “finder”
shut down
end tell


Something tells this neophyte scripter (me) that you need to coerce a data type.
Try browsing the articles in’s “unScripted” section:

I’ve found them to be very useful.
I hope this helps.


Variable as the shade


I’m not positive but I don’t believe you can coerce data to a time. The code below will convert a string, entered in time format (12:00:00 PM), to a time. It has the benefit of not limiting how the time is entered. What I mean is that you can enter the time as “12:00 PM”, “12:00:00 PM”, military time “23:45:15” or just “12”.

global theTimeToShutDown

set {theHour, theMins, theSecs} to {0, 0, 0}

tell application "Finder"
	set theTimeToShutDown to (text returned of (display dialog "What time do you want to Quit?" default answer "12:00:00 PM") as string)
end tell

set {theTimeToShutDown, theMeridian} to getMeridian(theTimeToShutDown)
set {theHour, theMins, theSecs} to convertTime(theTimeToShutDown)

-- ==| Developer Note |==========
-- ==| 43200 is the number of seconds from 00:00:00 (midnight) to noon
-- ==| If the time entered is not entered as military time, you'll
-- ==| want to add 12 hours to the time.

if theMeridian = "PM" and theHour < 43200 then
	set theHour to theHour + 43200
end if

set theTimeToShutDown to theHour + theMins + theSecs

-- ==| Developer Note |==========
-- ==| If you are going to use this as a "stay open" app,
-- ==| uncomment the "on idle" handler and the "return 1" below

--on idle
tell application "Finder"
	if (time of (current date)) = theTimeToShutDown then
		-- replace the "beep 3" with your command to shut down
		beep 3
	end if
end tell
--return 1
--end idle

-- ==| Subroutines |========================================

on getMeridian(theTime)
	-- ==| Developer Note |==========
	-- ==| Here we're finding out whether the user intended
	-- ==| the time entered to be "AM" or "PM"
	-- ==| if the time entered does not contain "AM" or "PM", 
	-- ==| we will interpret the time as "PM"
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to (ASCII character 32)
	set theTime to (text items of theTime) as list
	set AppleScript's text item delimiters to oldDelims
	if "AM" is in theTime then
		return {(item 1 of theTime), "AM"}
		return {(item 1 of theTime), "PM"}
	end if
end getMeridian

on convertTime(theTime)
	-- ==| Developer Note |==========
	-- ==| Here we are making sure that we have a value 
	-- ==| for hours, minutes and seconds
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set theTime to (text items of theTime) as list
	set AppleScript's text item delimiters to oldDelims
	if (length of theTime) < 3 then
		repeat until (length of theTime) = 3
			set the end of theTime to 0
		end repeat
	end if
	set {theHour, theMins, theSecs} to theTime
	set {theHour, theMins, theSecs} to {(theHour as integer), (theMins as integer), (theSecs as integer)}
	set theMins to theMins * 60
	set theHour to theHour * 3600
	return {theHour, theMins, theSecs}
end convertTime

Hope this helps out.

Scott Lewis
unScripted | MacScripter Staff


‘time’ gives the seconds after midnight of a date.


set cur_date to (current date)
set seconds_after_midnight to (time of cur_date)
set hours_after_midnight to (seconds_after_midnight div hours)
– etc.

Coerce to date first:

set date_string to “2/1/04”
set as_date to date date_string – coerce to applescript date

this gives you the applescript date at midnight. If you now do:

time of as_date

you’ll get 0. Zero seconds after midnight. You should read the AppleScriptLanguageGuide.pdf and search for date.


As kel mentions, the date class of AppleScript is really time (in seconds) so you can do something like this:

property time_to_quit_string : "1:15 AM"

set {the_error, the_icon} to {"", 1}
	set time_to_quit_string to text returned of (display dialog the_error & "When do you want to quit?" default answer time_to_quit_string buttons {"Cancel", "OK"} default button 2 with icon the_icon)
		set time_to_quit to (date time_to_quit_string) --make sure the entry is a time string
		exit repeat
	on error
		set {the_error, the_icon} to {"Your entry was not a time string. Please try again." & return & return, 2}
	end try
end repeat
set delay_time to (time_to_quit - (current date))
if delay_time < 0 then set delay_time to (delay_time + (1 * days)) --in case the time has already passed today, set it to tomorrow
do shell script "sleep " & delay_time --uses fewer CPU cycles than delay or a repeat loop
display dialog "It's " & time_to_quit_string & " & time's up!" buttons {"OK"} default button 1 giving up after 10 with icon 1



Very nice. More elegant and simple. You learn something every day. I didn’t realize that “date” and “time” were essentially the same thing.

Thanks for teaching me something new.

Scott Lewis
unScripted | MacScripter Staff