Created
October 13, 2013 09:25
-
-
Save bjpirt/6960179 to your computer and use it in GitHub Desktop.
An applescript to export the events for a specific calendar for the month you choose in order to use Calendar.app as a timesheet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on monthsAgo(m) -- returns a date formatted for the select box | |
set m to -m | |
set newDate to current date | |
-- Convert the month-offset parameter to years and months | |
set {y, m} to {m div 12, m mod 12} | |
-- If the month offset is negative (-1 to -11), make it positive from a year previously | |
if m < 0 then set {y, m} to {y - 1, m + 12} | |
-- Add the year offset into the new date's year | |
set newDate's year to (newDate's year) + y | |
-- Add the odd months (at 32 days per month) and set the day | |
repeat m times | |
if m is not 0 then tell newDate to set {day, day} to {32, day} | |
end repeat | |
return (the month of newDate & " " & the year of newDate) as text | |
end monthsAgo | |
on replace_chars(this_text, search_string, replacement_string) | |
set AppleScript's text item delimiters to the search_string | |
set the item_list to every text item of this_text | |
set AppleScript's text item delimiters to the replacement_string | |
set this_text to the item_list as string | |
set AppleScript's text item delimiters to "" | |
return this_text | |
end replace_chars | |
-- Choose the month to export | |
set monthList to {monthsAgo(0), monthsAgo(1), monthsAgo(2), monthsAgo(3), monthsAgo(4), monthsAgo(5)} | |
set selected_month to choose from list monthList with prompt "Select the month to export: " OK button name "Continue" cancel button name "Cancel" | |
if selected_month is false then return | |
set startDate to date ("1 " & selected_month) | |
copy startDate to endDate | |
tell endDate to set {day, day} to {32, day} | |
set fileName to (the year of startDate & "-" & (the month of startDate as integer) & "_timesheet.csv") as text | |
-- Choose the calendar | |
tell application "Calendar" to set calendarNames to the name of every calendar | |
set calendar_name to choose from list calendarNames with prompt "Select the timesheet calendar: " OK button name "Continue" cancel button name "Cancel" | |
if calendar_name is false then return | |
set calendar_name to the first item of calendar_name | |
-- Collect the calendar events as CSV | |
tell application "Calendar" | |
set cal to first calendar whose name is calendar_name | |
set selectedEvents to every event of cal whose start date ≥ startDate and end date < endDate | |
set output to {} | |
repeat with myEvent in selectedEvents | |
set duration to (((end date of myEvent) - (start date of myEvent)) / 60) as integer | |
set hours to duration div 60 | |
set days to hours div 24 | |
set hours to hours mod 24 | |
set minutes to duration mod 60 | |
set {year:y, month:m, day:d, hours:h, minutes:min} to the start date of myEvent | |
tell (100000000 + (d * 1000000 + m * 10000 + y)) as string to text 2 thru 3 & "/" & text 4 thru 5 & "/" & text 6 thru 9 | |
set outputDate to the result | |
tell (10000 + (h * 100 + min)) as string to text 2 thru 3 & ":" & text 4 thru 5 & ":00" | |
set outputTime to the result | |
tell (1000000 + (days * 10000 + hours * 100 + minutes)) as string to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7 | |
set outputDuration to the result | |
set summaryText to my replace_chars(the summary of myEvent, "\"", "\"\"") | |
set outputLine to (outputDate) & " " & outputTime & "," & outputDuration & ",\"" & (summaryText) & "\"" | |
set end of output to outputLine as text | |
end repeat | |
end tell | |
-- Write the CSV out to a file | |
set AppleScript's text item delimiters to {ASCII character 10} | |
set saveFile to choose file name with prompt "Save timesheet as:" default name fileName | |
set the open_target_file to open for access saveFile with write permission | |
write (output as text) to the open_target_file starting at 0 | |
close access the open_target_file | |
-- Sort the file so the events are in the correct order | |
do shell script "sort -o " & quoted form of POSIX path of saveFile & " " & quoted form of POSIX path of saveFile | |
I'm getting the same error:
error "Invalid date and time date 1 March 2015 of «script»." number -30720
Any ideas to resolve this would be awesome!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the following error:
error "Invalid date and time date 1 September 2014 of «script»." number -30720
. I have a hunch that it's because I've set all my region format to US. Any other ideas?