Adding a time picker & doing math on the result

I’m trying to adapt a script to schedule tasks from chosed start time. I’m struggling with time formatting.

When the script passes the date/time into the calendar section of the script, I keep getting the error: Can’t make {“8:30am”} into type number.

Does anyone know how to script a time picker which can then have math done on the result?

I’d suggest reading up on the basic AppleScript types.

That said, dates are not very intuitive. AppleScript dates are mutable. Something like this will allow you to set a time with the current date:

set list_choice to choose from list {"8:00", "8:30", "9:00", "9:30"} with prompt "Choose start time:" with title (my name as string)
if list_choice is false then error "User canceled." number -128
set list_choice to item 1 of list_choice

set time_separator_offset to offset of ":" in list_choice
set choice_hours to text 1 thru (time_separator_offset - 1) of list_choice
set choice_minutes to text (time_separator_offset + 1) thru (time_separator_offset + 2) of list_choice

set start_time to current date
set hours of start_time to choice_hours as integer
set minutes of start_time to choice_minutes as integer
set seconds of start_time to 0

return start_time

In future, you will have more success on the forum if you post the code with proper code formatting, and not a screenshot.

Thanks - that’s really helpful. Can I check where you learned about the basic Applescript Types? I’ve read parts of Sal’s book, but there’s very little on time which was applicable.

Thanks

The official AppleScript Reference Guide is useful, but somewhat terse.

I find the most useful reference to be Matt Neuberg’s AppleScript: The Definitive Guide (2e). It’s very old at this point, but it almost all applies to present-day AppleScript.

1 Like

You can also use the Variable Viewer in ScriptDebugger and the Dictionary Explorer.