applescriptautomator

How to add hours to time in Automator script using AppleScript?


I put together an AppleScript on Automator that has the goal of

  1. taking text (in a pre-determined format)
  2. extracting the dates/times
  3. creating macOs Calendar events with these dates/times

The script works but fails when I try to add hours to the appointment date/time. I'd need the events to be +6 hours ahead of the times in the text due to my timezone.

Sample data:

4/29/24 - 2:30pm-3:30pm - XYASHDK
4/29/24 - 6:30pm-9:30pm - DLHEOSH

AppleScript:

{On Automator, "Get Specified Text" with above data is followed by "Extract dates from Text" followed by this script}

on run {input, parameters}
    set apptList to input
    repeat with i from 1 to count of apptList
        set appt to item i of apptList
        set AppleScript's text item delimiters to " - "
        set stringItems to text items of appt
        set apptDate to item 1 of stringItems
        set AppleScript's text item delimiters to ""
        
        set apptTimes to item 2 of stringItems
        set AppleScript's text item delimiters to "-"
        set hoursItems to text items of apptTimes
        set apptBegTime to item 1 of hoursItems
        set apptEndTime to item 2 of hoursItems
        set apptBegin to date (apptDate & " " & apptBegTime)
        set apptEnd to date (apptDate & " " & apptEndTime)
        set apptBeginTZ to ((hours of apptBegin) + 6)
        set apptEndTZ to ((hours of apptEnd) + 6)
        tell application "Calendar"
            tell calendar "ABC"
                make new event with properties {summary:"busy", start date:apptBeginTZ, end date:apptEndTZ}
            end tell
        end tell
    end repeat
end run

As above, I've tried adding N hours to the final appointment time but get the following error:

The action “Run AppleScript” encountered an error: “Calendar got an error: Can’t make 8 into type date.”

Would you have any suggestions on how to accomplish adding N hours to the appointment times?


Solution

  • The least unit of an AppleScript date is second and you can do dath math with seconds or even hours, so the syntax is almost "natural human"

    set apptBeginTZ to apptBegin + 6 * hours
    set apptEndTZ to apptEnd + 6 * hours
    

    Your syntax extracts the hour value of a date and adds 6. The result is an integer rather than a date. This is what the error message says.

    Edit:

    A more reliable way to parse the date is Regular Expression and a date formatter. Vanilla AppleScript can't do it but the Foundation framework can:

    use AppleScript version "2.5"
    use framework "Foundation"
    use scripting additions
    
    set theDate to "4/29/24 - 2:30pm-3:30pm - XYASHDK"
    
    set nsTheDate to current application's NSString's stringWithString:theDate
    set regexPattern to "(\\d{1,2}/\\d{1,2}/\\d{2}) - (\\d{1,2}:\\d{2}[pa]m)-(\\d{1,2}:\\d{2}[pa]m)"
    
    set regex to current application's NSRegularExpression's regularExpressionWithPattern:regexPattern options:0 |error|:(missing value)
    set theMatch to regex's firstMatchInString:theDate options:0 range:{0, theDate's length}
    set dateRange to theMatch's rangeAtIndex:1
    set startTimeRange to theMatch's rangeAtIndex:2
    set endTimeRange to theMatch's rangeAtIndex:3
    set datePart to (nsTheDate's substringWithRange:dateRange) as text
    set startDateString to datePart & (nsTheDate's substringWithRange:startTimeRange) as text
    set endDateString to datePart & (nsTheDate's substringWithRange:endTimeRange) as text
    set dateFormatter to current application's NSDateFormatter's alloc's init()
    dateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
    dateFormatter's setDateFormat:"M/d/yyh:mma"
    set startDate to (dateFormatter's dateFromString:startDateString) as date
    set endDate to (dateFormatter's dateFromString:endDateString) as date
    

    startDate and endDate are AppleScript dates.