Last active
June 5, 2018 11:08
-
-
Save s-oravec/b87ca1824cc2b7f0608ab3b99ebc70b3 to your computer and use it in GitHub Desktop.
Calendar events to timesheet AppleScript
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 newNumbersFromEventInfos(calendarNames, selectedYearMonth, theEventInfos) | |
| -- get max col count | |
| set colCount to 0 | |
| repeat with rowIndex from 1 to length of theEventInfos | |
| if colCount < length of item rowIndex of theEventInfos then | |
| set colCount to length of item rowIndex of theEventInfos | |
| end if | |
| end repeat | |
| -- | |
| tell application "Numbers" | |
| set theDocument to make new document | |
| -- with new document | |
| tell theDocument | |
| tell sheet 1 | |
| set name to "data" | |
| delete every table | |
| set thisTable to make new table with properties ¬ | |
| {column count:colCount, row count:length of theEventInfos} | |
| set name of thisTable to "timeSheetData" | |
| tell thisTable | |
| repeat with rowIndex from 1 to length of theEventInfos | |
| repeat with colIndex from 1 to colCount | |
| tell cell colIndex of row rowIndex | |
| -- meh, set format of theEventDuration to duration | |
| if colIndex = colCount then | |
| set value to (item colIndex of item rowIndex of theEventInfos) | |
| set format to duration | |
| else | |
| set value to item colIndex of item rowIndex of theEventInfos | |
| end if | |
| end tell | |
| end repeat | |
| end repeat | |
| end tell | |
| end tell | |
| end tell | |
| end tell | |
| end newNumbersFromEventInfos | |
| on parseEventInfo(theEventInfo) | |
| set oldDelimiters to AppleScript's text item delimiters | |
| set AppleScript's text item delimiters to ";" | |
| set theItems to every text item of theEventSummary of theEventInfo | |
| set AppleScript's text item delimiters to oldDelimiters | |
| if (length of theItems) = 1 then | |
| -- no identifier, just description | |
| return {theEventStartDate of theEventInfo, "", item 1 of theItems, (theEventDuration of theEventInfo) / 3600} | |
| else | |
| -- TODO: fix more then 2 items = more then one ; delimiters | |
| return {theEventStartDate of theEventInfo, item 1 of theItems, item 2 of theItems, (theEventDuration of theEventInfo) / 3600} | |
| end if | |
| end parseEventInfo | |
| on processCalendars(calendarNames, theStartDate, theEndDate, selectedYearMonth) | |
| -- collection to store events in [theStartDate, theEndDate] interval in calendNames calendars | |
| set theEventInfos to {{"theEventStartDate", "theEventIdentifier", "theEventActivity", "theEventDuration"}} | |
| tell application "Calendar" | |
| repeat with calendarName in calendarNames | |
| tell calendar calendarName | |
| -- get those items | |
| set theEvents to (every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate) | |
| repeat with theEvent in theEvents | |
| -- parse event info in summary of event and collect them into collection | |
| set end of theEventInfos to parseEventInfo({theEventSummary:summary of theEvent as string, theEventDuration:((end date of theEvent) - (start date of theEvent)) / 24, theEventStartDate:start date of theEvent}) of me | |
| end repeat | |
| end tell | |
| end repeat | |
| end tell | |
| -- create new numbers document from this collection | |
| newNumbersFromEventInfos(calendarNames, selectedYearMonth, theEventInfos) of me | |
| end processCalendars | |
| tell application "Calendar" | |
| -- date | |
| set defaultDate to do shell script "date +'%Y-%m'" | |
| set {text returned:selectedYearMonth} to display dialog "Please eneter month in format YYYY-MM" default answer defaultDate | |
| -- start date | |
| set theStartDate to the current date | |
| set the year of theStartDate to (text 1 thru 4 of selectedYearMonth) | |
| set the month of theStartDate to (text 6 thru 7 of selectedYearMonth) | |
| set the day of theStartDate to 1 as integer | |
| set the time of theStartDate to 0 as integer | |
| -- end date | |
| copy theStartDate to theEndDate | |
| set the month of theEndDate to (month of theEndDate) + 1 | |
| -- pick calendars | |
| set theCalendars to every calendar | |
| set listOfCalendarNames to {} | |
| repeat with theCalendar in theCalendars | |
| set listOfCalendarNames to listOfCalendarNames & (name of theCalendar as string) | |
| end repeat | |
| set selectedCalendarNames to choose from list listOfCalendarNames with title "Calendar Picker" with prompt "Choose calendars with work log" with multiple selections allowed | |
| -- process calendars | |
| processCalendars(selectedCalendarNames, theStartDate, theEndDate, selectedYearMonth) of me | |
| end tell |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
modify theEventDuration to duration format