Last active
April 23, 2022 02:33
-
-
Save talkingmoose/629a0e827c358ae36b9fe6dbae588c44 to your computer and use it in GitHub Desktop.
Begins a new daily note for today in Bear. Adds a "Daily Notes" tag, events for today from Outlook 2016 for Mac calendar and a "Notes" header. Save this AppleScript to the Scripts folder for Bear at ~/Library/Scripts/Applications/Bear and call it using the system-wide AppleScript menu.
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
(* | |
---------------------------------------------------------------------------------- | |
ABOUT THIS SCRIPT | |
Written by:William Smith | |
Professional Services Engineer | |
Jamf | |
[email protected] | |
https://gist.github.com/629a0e827c358ae36b9fe6dbae588c44 | |
Originally posted: March 10, 2017 | |
Updated: January 24, 2021 | |
Bear URL scheme no longer seemed to work without URL-encoding strings. | |
May be a Big Sur issue. Found AppleScript handler online and | |
adjusted script. | |
Purpose: Begins a new daily note for today in Bear. Adds a | |
"Daily Notes" tag, events for today from Outlook 2016 | |
for Mac calendar and a "Notes" header. | |
Instructions: Add this script to Bear's AppleScript folder: | |
~/Library/Scripts/Applications/Bear/New Daily Note.scpt | |
Except where otherwise noted, this work is licensed under | |
http://creativecommons.org/licenses/by/4.0/ | |
"One, today is worth two tomorrows," as Poor Richard says. | |
---------------------------------------------------------------------------------- | |
*) | |
-- sourced from https://harvey.nu/applescript_url_encode_routine.html | |
on encodeCharacter(theCharacter) | |
set theASCIINumber to (the ASCII number theCharacter) | |
set theHexList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"} | |
set theFirstItem to item ((theASCIINumber div 16) + 1) of theHexList | |
set theSecondItem to item ((theASCIINumber mod 16) + 1) of theHexList | |
return ("%" & theFirstItem & theSecondItem) as string | |
end encodeCharacter | |
on encodeText(theText, encodeCommonSpecialCharacters, encodeExtendedSpecialCharacters) | |
set theStandardCharacters to "abcdefghijklmnopqrstuvwxyz0123456789" | |
set theCommonSpecialCharacterList to "$+!'/?;&@=#%><{}\"~`^\\|*" | |
set theExtendedSpecialCharacterList to ".-_:" | |
set theAcceptableCharacters to theStandardCharacters | |
if encodeCommonSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theCommonSpecialCharacterList | |
if encodeExtendedSpecialCharacters is false then set theAcceptableCharacters to theAcceptableCharacters & theExtendedSpecialCharacterList | |
set theEncodedText to "" | |
repeat with theCurrentCharacter in theText | |
if theCurrentCharacter is in theAcceptableCharacters then | |
set theEncodedText to (theEncodedText & theCurrentCharacter) | |
else | |
set theEncodedText to (theEncodedText & encodeCharacter(theCurrentCharacter)) as string | |
end if | |
end repeat | |
return theEncodedText | |
end encodeText | |
-- today's date is the title for today's Daily Note | |
set todaysDate to do shell script "date +\"%B %-d, %Y\"" -- e.g. "February 25, 2018" | |
set todaysDate to encodeText(todaysDate, false, false) | |
-- set tag for new note to "Daily Notes" | |
-- set bearText to "#Daily Notes#" & return & return as string | |
set bearText to "#Daily Notes# | |
" | |
set bearText to encodeText(bearText, true, false) | |
-- create the full URL to send to Bear | |
set bearURL to "open \"bear://x-callback-url/create?title=" & todaysDate & "&text=" & bearText & "\"" | |
-- open the URL, create the new note and bring note to front | |
do shell script bearURL | |
delay 1 | |
-- add "Appointments and Meetings" header to today's daily note | |
set bearText to "## Appointments and Meetings | |
" | |
set bearText to encodeText(bearText, true, false) | |
do shell script "open \"bear://x-callback-url/add-text?title=" & todaysDate & "&text=" & bearText & "\"" | |
-- define today's beginning and end times (midnight to 11:59 p.m.) | |
set startDay to date (date string of ((current date)) & "at 12:00:00 AM" as string) | |
set endDay to date (date string of ((current date)) & "at 11:59:59 PM" as string) | |
-- begin adding today's events to Bear Daily Notes | |
set eventCount to 0 | |
tell application "Microsoft Outlook" | |
-- get list of today's non-recurring events | |
set todaysEvents to ¬ | |
(every calendar event whose ¬ | |
(start time is greater than or equal to startDay and end time is less than endDay) or ¬ | |
(start time is less than or equal to startDay and end time is greater than endDay) or ¬ | |
(start time is greater than or equal to startDay and start time is less than endDay and end time is greater than or equal to endDay) or ¬ | |
(start time is greater than or equal to startDay and end time is greater than startDay and end time is less than or equal to endDay)) | |
-- sort events by time | |
-- reference: https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateListsofItems.html | |
set theIndexList to {} | |
set todaysEventsSorted to {} | |
repeat (length of todaysEvents) times | |
set theLowItem to "" | |
repeat with a from 1 to (length of todaysEvents) | |
if a is not in theIndexList then | |
set theCurrentItem to item a of todaysEvents | |
if theLowItem is "" then | |
set theLowItem to theCurrentItem | |
set theLowItemIndex to a | |
else if start time of theCurrentItem comes before start time of theLowItem then | |
set theLowItem to theCurrentItem | |
set theLowItemIndex to a | |
end if | |
end if | |
end repeat | |
set end of todaysEventsSorted to theLowItem | |
set end of theIndexList to theLowItemIndex | |
end repeat | |
-- add each calendar event to Bear Daily Notes | |
repeat with anEvent in todaysEventsSorted | |
set eventSubject to subject of anEvent | |
if eventSubject contains "\"" then | |
set theCommand to "echo '" & eventSubject & "' | sed \"s/\\\"/'/g\"" | |
set eventSubject to do shell script theCommand | |
end if | |
if eventSubject contains "&" then | |
set theCommand to "echo '" & eventSubject & "' | sed \"s/&/and/g\"" | |
set eventSubject to do shell script theCommand | |
end if | |
set startEvent to start time of anEvent | |
set startTime to (characters 1 through -7 of (time string of startEvent) as string) & " " & (characters -2 through -1 of (time string of startEvent) as string) | |
set endEvent to end time of anEvent | |
set endTime to (characters 1 through -7 of (time string of endEvent) as string) & " " & (characters -2 through -1 of (time string of endEvent) as string) | |
if startTime = endTime then | |
set bearText to "- " & eventSubject & " (All Day)" as string | |
tell me to set bearText to encodeText(bearText, true, false) | |
else | |
set bearText to "- " & eventSubject & " (" & startTime & " to " & endTime & ")" as string | |
tell me to set bearText to encodeText(bearText, true, false) | |
end if | |
do shell script "open \"bear://x-callback-url/add-text?title=" & todaysDate & "&text=" & bearText & "\"" | |
set eventCount to eventCount + 1 | |
end repeat | |
end tell | |
-- make note if no calendar events occur today | |
if eventCount = 0 then | |
-- create the full URL to send to Bear | |
set bearText to "No appointments or meetings for today" | |
set bearText to encodeText(bearText, true, false) | |
-- open the URL, create the new note and bring note to front | |
do shell script "open \"bear://x-callback-url/add-text?title=" & todaysDate & "&text=" & bearText & "\"" | |
end if | |
-- add space between sections | |
set bearText to return | |
set bearText to encodeText(bearText, true, false) | |
-- open the URL, create the new note and bring note to front | |
do shell script "open \"bear://x-callback-url/add-text?title=" & todaysDate & "&text=" & bearText & "\"" | |
-- add "Notes" header to Bear Daily Notes and open in a new window | |
set bearText to "## Notes | |
" | |
set bearText to encodeText(bearText, true, false) | |
do shell script "open \"bear://x-callback-url/add-text?title=" & todaysDate & "&text=" & bearText & "&new_window=yes\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment