Skip to content

Instantly share code, notes, and snippets.

@khanlou
Last active February 2, 2018 13:05
Show Gist options
  • Save khanlou/5341b366e68acf0049999e07991035f4 to your computer and use it in GitHub Desktop.
Save khanlou/5341b366e68acf0049999e07991035f4 to your computer and use it in GitHub Desktop.
Notes.applescripts
tell application "Notes"
move front note to folder "Archive"
end tell
on replaceChars(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 replaceChars
on splitOnNewlines(theString)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set theArray to every text item of theString
set AppleScript's text item delimiters to oldDelimiters
return theArray
end splitOnNewlines
tell application "Notes"
activate
set the oldClipboard to the clipboard
tell application "System Events"
keystroke "c" using command down
end tell
set theText to the clipboard as Unicode text
if theText is equal to oldClipboard then
display alert "No items selected to archive."
return
end if
set lineItems to splitOnNewlines(theText) of me
set adjustedText to "<ul>"
repeat with lineItem in lineItems
if length of lineItem is not 0 then
set adjustedText to adjustedText & "<li>" & lineItem & "</li>"
end if
end repeat
set adjustedText to adjustedText & "</ul>"
tell folder "Archive"
make new note with properties {name:" ", body:"<h2>Archived</h2><br>" & adjustedText}
end tell
set the clipboard to the oldClipboard
end tell
set today to current date
set hours of today to 0
set minutes of today to 0
set tomorrow to today + 1 * days
set yesterday to today - (1 * days)
set yesterdaysName to the date string of yesterday
set todaysName to the date string of today
on buildTimeString(theDate)
-- Get the "hour"
set timeStr to time string of theDate
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "AM or PM"
set Pos to offset of " " in timeStr
set theSfx to characters (Pos + 1) through end of timeStr as string
return (theHour & ":" & theMin & " " & theSfx) as string
end buildTimeString
on eventsForDay(theDay)
set theEvents to {}
tell application "Calendar"
repeat with c in (every calendar)
repeat with e in ((every event in c) whose (start date) is greater than or equal to theDay and (start date) is less than theDay + (1 * days) and (start date) is not in (excluded dates))
if (allday event of e) then
set end of theEvents to ("All Day: " & (summary of e))
else
set startDate to start date of e
set timeString to buildTimeString(startDate) of me
set end of theEvents to (timeString & ": " & (summary of e))
end if
end repeat
end repeat
end tell
return theEvents
end eventsForDay
tell application "Notes"
set todaysEvents to eventsForDay(today) of me
if length of todaysEvents is 0 then
set noteBody to "<h2>No Events</h2>"
else
set noteBody to "<h2>Events</h2><ul>"
repeat with theEvent in todaysEvents
set noteBody to noteBody & "<li>" & theEvent & "</li>"
end repeat
set noteBody to noteBody & "</ul>"
end if
set noteBody to noteBody & "<br><h2>Tasks</h2>"
move note yesterdaysName to folder "Archive"
set noteTitle to "<h1>" & todaysName & "</h1><br>"
make new note at folder "Projects" with properties {name:" ", body:noteTitle & noteBody}
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment