Skip to content

Instantly share code, notes, and snippets.

@damc-dev
Created November 30, 2022 16:11
Show Gist options
  • Save damc-dev/451f3b2c70481d8ea7c5118ad32f85ea to your computer and use it in GitHub Desktop.
Save damc-dev/451f3b2c70481d8ea7c5118ad32f85ea to your computer and use it in GitHub Desktop.
Partially working applescript to retrieve todays calendar entries and add to clipboard (Note doesn't capture reoccurring entries)
-- NOTE Problem with this is that because of the way reocurring events are handled they aren't captured by this script
-- See this thread for some workarounds to project reocurring events: https://macscripter.net/viewtopic.php?id=29516
on format24h(provided_date)
try
set oldDelims to AppleScript's text item delimiters
set the_time to time string of provided_date -- something like "4:20:56 PM"
set am_pm to characters -2 thru -1 of the_time as string -- is it AM or PM
set AppleScript's text item delimiters to ":"
set the_hour to text item 1 of the_time as string -- in this example, "4"
set the_minute to text item 2 of the_time as string -- in this example, "20"
if (the_hour as string is not "12") and (am_pm is "PM") then
set the_hour to the_hour + 12
end if
if the_hour as string is "24" then
set the_hour to "12"
end if
-- that could have been more graceful but it's nice and obvious this way
if (count of (the_hour as string)) is 1 then -- padding with a zero if necessary
set the_hour to "0" & the_hour
end if
if (count of (the_minute as string)) is 1 then -- padding with a zero if necessary
set the_minute to "0" & the_minute
end if
set AppleScript's text item delimiters to ""
set time_string to (the_hour as string) & ":" & the_minute as string -- putting the pieces together
--log "Converted " & provided_date as string & " to " & time_string as string
set AppleScript's text item delimiters to oldDelims -- cleaning up
return time_string as string
on error
log "error converting " & provided_date & " to 24hr time"
return ""
end try
end format24h
on getEventString(an_event)
return summary of an_event as string
end getEventString
on changeCaseOfText(theText, theCaseToSwitchTo)
if theCaseToSwitchTo contains "lower" then
set theComparisonCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set theSourceCharacters to "abcdefghijklmnopqrstuvwxyz"
else if theCaseToSwitchTo contains "upper" then
set theComparisonCharacters to "abcdefghijklmnopqrstuvwxyz"
set theSourceCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
else
return theText
end if
set theAlteredText to ""
repeat with aCharacter in theText
set theOffset to offset of aCharacter in theComparisonCharacters
if theOffset is not 0 then
set theAlteredText to (theAlteredText & character theOffset of theSourceCharacters) as string
else
set theAlteredText to (theAlteredText & aCharacter) as string
end if
end repeat
return theAlteredText
end changeCaseOfText
on findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
on trimText(theText, theCharactersToTrim, theTrimDirection)
set theTrimLength to length of theCharactersToTrim
if theTrimDirection is in {"beginning", "both"} then
repeat while theText begins with theCharactersToTrim
try
set theText to characters (theTrimLength + 1) thru -1 of theText as string
on error
-- text contains nothing but trim characters
return ""
end try
end repeat
end if
if theTrimDirection is in {"end", "both"} then
repeat while theText ends with theCharactersToTrim
try
set theText to characters 1 thru -(theTrimLength + 1) of theText as string
on error
-- text contains nothing but trim characters
return ""
end try
end repeat
end if
return theText
end trimText
on cleanString(subjectTxt, prefixesToRemove)
set cleanedSubject to changeCaseOfText(subjectTxt, "lower")
repeat with prefix in prefixesToRemove
set cleanedSubject to findAndReplaceInText(cleanedSubject, prefix, "")
end repeat
set cleanedSubject to trimText(cleanedSubject, " ", "both")
return cleanedSubject
end cleanString
-- prefixes should be lowercase
set prefixList to {"re:", "fw:", "[external]", "recall:"}
set theStartDate to current date
set hours of theStartDate to 0
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (1 * days) - 1
display notification ("Checking for calendar entries between " & theStartDate as string) & " - " & theEndDate as string
set myResult to ""
tell application "Calendar"
tell calendar "REPLACE WITH YOUR CALENDAR NAME HERE"
set todaysEvents to (every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate)
display notification "Found " & ((count of todaysEvents) as string) & " event(s) for calendar entries between " & (theStartDate as string) & " - " & theEndDate as string
repeat with myEvent in todaysEvents
set body to summary of myEvent as string
set startTime to start date of myEvent
set endTime to end date of myEvent
set startStr to my format24h(startTime as date)
set endStr to my format24h(endTime as date)
set timeRange to startStr & "-" & endStr
set eventStr to timeRange & " " & body
set myResult to myResult & "
" & eventStr
end repeat
set clipboard_text to myResult
set the clipboard to clipboard_text
display notification "Copied to clipboard: " & myResult
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment