My Python script for creating an agenda

Hi. I just thought I’d share my Python script which I use to create an agenda query (I presume other people are doing similar things?). It calculates dates from today, so the query needs to be regenerated every day. On my system the script is called by Espanso (excellent cross-platform text expander), but it could also work well with Alfred (on Mac).

A few points

(1) The script finds tasks scheduled for a particular date. It finds lines which begin with a tickbox ("[ ]") and contain a date.

(2) The format for date links needs to be the following: “\w\kww YYYY-MM-DD dddd”. This needs to be set up for the Daily Notes plugin and the Natural Language Dates plugin (if you are using it - strongly recommended). It needs to be in this format, because the Python script generates queries which search using this format.

(3) I have a “quote of the day” section, and a section listing “backburners” (things I need to keep at the back of my mind). These may be easily removed if you don’t like them (see print statement at the bottom).

(4) Scheduled tasks will not necessarily be ordered by date within week blocks. They are ordered in terms of the alphabetical order of notes which contain them. It would be technically feasible to list tasks in chronological order by creating new queries for every single day of a given period. However, I haven’t done this, as there would be too much whitespace, and it would not be very readable. Perhaps there is someone out there who is a wiz at CSS who could get this to work well?

(5) This kind of thing could easily be worked up into a full blown addon by someone with the right technical background and the time on their hands.



qod = "<img style=\"width:350px;\" src=\"https://kwize.com/pics/Quote-of-the-Day-1-2.jpg\" alt=\"Quote of the Day\"><span style=\"display:block;font-size:14px;\">Kwize <a style=\"color:black;\" href=\"https://kwize.com/quote-of-the-day/\">Quote of the day</a></span>\n\n\n\n"


from datetime import datetime
current_week = "w[\\x6b]" + datetime.today().strftime("%W %Y")
current_date =  datetime.today().strftime("%Y-%m-%d")


# Calculate

from datetime import timedelta

tomorrow = (datetime.today() + timedelta(days=1)).strftime("%Y-%m-%d")

# NB I specify "k" using hexadecimals so that query doesn't find itself!!

week_plus_one_week = "w[\\x6b]" + (datetime.today() + timedelta(days=7)).strftime("%W %Y")
week_plus_two_weeks = "w[\\x6b]" + (datetime.today() + timedelta(days=14)).strftime("%W %Y")
week_plus_three_weeks = "w[\\x6b]" + (datetime.today() + timedelta(days=21)).strftime("%W %Y")
# week_plus_four_weeks = "w[\\x6b]" + (datetime.today() + timedelta(days=28)).strftime("%W %Y")
week_plus_two_or_three_weeks = "(" + week_plus_two_weeks + "|" + week_plus_three_weeks + ")"


week_minus_one_week = "w[\\x6b]" + (datetime.today() + timedelta(days=-7)).strftime("%W %Y")
week_minus_two_weeks = "w[\\x6b]" + (datetime.today() + timedelta(days=-14)).strftime("%W %Y")
week_minus_three_weeks = "w[\\x6b]" + (datetime.today() + timedelta(days=-21)).strftime("%W %Y")
week_minus_four_weeks = "w[\\x6b]" + (datetime.today() + timedelta(days=-28)).strftime("%W %Y")
last_four_weeks = "(" + week_minus_one_week + "|" + week_minus_two_weeks + "|" + week_minus_three_weeks + "|" + \
    week_minus_four_weeks + ")"

query_today = "line:(/\[ \].+" + current_date + ".*/)"
query_tomorrow = "line:(/\[ \].+" + tomorrow + ".*/)"


query_this_week_scheduled = "line:(/\[ \].+" + current_week + ".*/)"
query_next_week_scheduled = "line:(/\[ \].+" + week_plus_one_week + ".*/)"
query_next_fortnight_scheduled = "line:(/\[ \].+" + week_plus_two_or_three_weeks + ".*/)"

query_last_four_weeks = "line:(/\[ \].+" + last_four_weeks + ".*/)"


today_title = "<h4>&#x23F0; Today</h4>\n"

tomorrow_title = "<h4>&#x23F0; Tomorrow</h4>\n"


this_week_scheduled_title = "<h4>&#x23F0; This week</h4>\n"
next_week_scheduled_title = "<h4>&#x23F0; Next week</h4>\n"
next_fortnight_scheduled_title = "<h4>&#x23F0; Next fortnight</h4>\n"

backburner_title = "<h4>&#x1F525; Backburner!</h4>\n"

scheduled_last_four_weeks_title = "<h4>&#x23F0; Last four weeks</h4>\n"
scheduled_any_title = "<h4>&#x23F0; Any</h4>\n"

# Create blocks

today_block = today_title + "\n" + \
    "```query\n" + \
    query_today + \
    "\n```\n\n"

tomorrow_block = tomorrow_title + "\n" + \
    "```query\n" + \
    query_tomorrow + \
    "\n```\n\n"


this_week_scheduled_block = this_week_scheduled_title + "\n" + \
    "```query\n" + \
    query_this_week_scheduled + \
    "\n```\n\n"


next_week_scheduled_block = next_week_scheduled_title + "\n" + \
    "```query\n" + \
    query_next_week_scheduled + \
    "\n```\n\n"


next_fortnight_scheduled_block = next_fortnight_scheduled_title + "\n" + \
    "```query\n" + \
    query_next_fortnight_scheduled + \
    "\n```\n\n"

scheduled_last_four_weeks_block = scheduled_last_four_weeks_title + "\n" + \
    "```query\n" + \
    query_last_four_weeks + \
    "\n```\n\n"

scheduled_any_block = scheduled_any_title + "\n" + \
    "```query\n" + \
    "line:(/\[ \].*/)" + \
    "\n```\n\n"

backburner_block = backburner_title + "\n" + \
    "```query\n" + \
    "line:(/.+backburner\]\].*/)" + \
    "\n```\n\n"

print(qod +
     today_block + tomorrow_block + this_week_scheduled_block +
     scheduled_last_four_weeks_block +
     backburner_block +
     next_week_scheduled_block +
     next_fortnight_scheduled_block +
     scheduled_any_block)
1 Like

Espanso is an excellent text expander indeed, and it also works on Mac.

1 Like