Hello!
I’m writing an AppleScript to get time values out of text documents in BBEdit. It takes a list of time ranges, totals each line in decimal hours, then adds a grand total.
My times are input in ranges like:
09:30-09:45
11:30-12:00
13:00-14:45
15:15-17:15
The script converts the hours and minutes to a single seconds value, then does the subtraction to get the amount of time indicated by the range. I get these times to a decimal hours result by dividing by 3600. An example looks like this:
set TimeTotalLineCurrent to (TicketTextTimeLineStop - TicketTextTimeLineStart) / 3600
I always want the number of hours to have two decimal places, even with a trailing zero, but when I add the totals to the lines I get:
09:30-09:45 0.25
11:30-12:00 0.5
13:00-14:45 1.75
15:15-17:15 2.0 4.5
You’ll notice I also total all the lines and add it to the last line like the “4.5” in the example above, and I’d like that to be two decimal as well, or 4.50.
What I want this to look like is:
09:30-09:45 0.25
11:30-12:00 0.50
13:00-14:45 1.75
15:15-17:15 2.00 4.50
the way the script turns:
09:30-09:45
into
09:30-09:45 0.25
is with
# Add total time to end of current line in decimal hours
set BBLineNumber to startLine of selection
select insertion point after last character of line BBLineNumber of vDocument
set selection to " " & TimeTotalLineCurrent as text
Is there a way to make the text of TimeTotalLineCurrent to always have two decimals?
I’m curious as to why in my sample above the 2.0 gets its zero, but the 0.5 does not get a trailing zero. I would kind of expect it to just print “2”.
I’m also wondering why the 0.25 is getting its leading 0. - but this is exactly how I want it so I’m not complaining.
Thanks for any help!
- eric