I’m about three months into AppleScripting and I’d like some advice about using timecodes in AppleScript.
I have an AppleScript where I want to add a duration time to an existing timecode value.
Let’s say value x is the initial timecode value: 01:00:10:10 (Hr.Min.Sec.Frames)
I want to add a duration y of 50 seconds and 20 frames (00:00:50:20) to this value.
The result z should come to 01:01:01:00 (30 frames per second).
This should get you going. The script below contains two handlers for converting a time sting back and forth to an integer.
property fps : 30
set initial_value to "01:00:10:10"
set additional_value to "00:00:50:20"
set total_value to ((my convert_frame_string_to_number(initial_value)) + (my convert_frame_string_to_number(additional_value)))
set total_value to my convert_number_to_frame_string(total_value)
-->"01:01:01:00"
on convert_frame_string_to_number(the_string)
tell (a reference to my text item delimiters)
set {old_delim, contents} to {contents, ":"}
set {{h, m, s, f}, contents} to {the_string's text items, old_delim}
end tell
set h to (h as integer) * fps * hours
set m to (m as integer) * fps * minutes
set s to (s as integer) * fps
return h + m + s + f
end convert_frame_string_to_number
on convert_number_to_frame_string(the_number)
set h to ("0" & (the_number div (fps * hours)))'s text -2 thru -1
set m to ("0" & ((the_number mod (fps * hours)) div (fps * minutes)))'s text -2 thru -1
set s to ("0" & (the_number mod (fps * minutes)) div fps)'s text -2 thru -1
set f to ("0" & (the_number mod fps))'s text -2 thru -1
return "" & {h, ":", m, ":", s, ":", f}
end convert_number_to_frame_string