jgclark / NotePlan-tools

This Ruby script adds functionality to the [NotePlan app](https://noteplan.co/). Particularly when run frequently, this provides a more flexible system for repeating tasks, allows for due dates to be expressed as offsets which allows for templates, and moves or files items from Daily files to Note files. It incorporates an ealier script to 'clean' or tidy up NotePlan's data files.
26 stars 6 forks source link

NotePlan Tools

Note: All of the capability of this script is now available through my NotePlan Plugins available directly in the app. Please see this overview, and the individual plugins' details in the GitHub repository. In particular see 'Tidy Up', 'Repeat Extensions' and 'Event Helpers'.

npTools.rb is a Ruby script that adds functionality to the NotePlan app. Particularly when run frequently, this provides a more flexible system for repeating tasks, allows for due dates to be expressed as offsets and therefore templates, moves items from Daily files to Note files, and creates events. It incorporates an earlier script to 'clean' or tidy up NotePlan's data files.

Each time the script runs, it does a number of things, explained in each section:

Tidy up

It tidies up data files, by:

  1. removing the time part of any @done(...) mentions that NotePlan automatically adds when the 'Append Completion Date' option is on.
  2. removing any @done(...) mentions on completed Checklist items that NotePlan automatically adds when the 'Append Completion Date' option is on. (From NP v3.8)
  3. removing #waiting or #high tags or <dates from completed or cancelled tasks (configurable)
  4. removing scheduled (* [>] task) items in calendar files (as they've been copied to a new day)
  5. removing any lines with just *or - or starting #s
  6. removing header lines without any content before the next header line of the same or higher level (i.e. fewer #s)
  7. removing any multiple consecutive blank lines.

Moves Daily (Calendar) note items

(From v1.9.2) The script can now move any Daily note entries with a >date in it to the mentioned Daily note. To do so requires turning on through the -m option.

In more detail:

NB: This only operates from Daily (Calendar) notes; it therefore doesn't interfere with linking and back-linking between main notes.

Files Daily (Calendar) note tasks

The script can move any Daily note tasks with a [[Note title]] in it to the mentioned note, filing them directly after the header section.

In more detail:

This feature can be turned on using two different options, with slightly different triggers:

NB: This only operates from Daily (Calendar) notes; therefore it doesn't interfere with linking and back-linking between regular notes.

Templates for dates

It changes any mentions of date offset patterns (such as {-10d}, {+2w}, {-3m}) into scheduled dates (e.g. >2020-02-27), if it can find a valid date pattern in the previous heading, previous main task if it has sub-tasks, or in the line itself. This allows for users to define simple template sections and copy and paste them into the note, set the due date at the start, and the other dates are then worked out for you.

For example ... ... becomes
### Christmas Cards 25/12/2020
* Write cards {-20d}
* Post overseas cards {-15d}
* Post cards to this country {-10d}
* Store spare cards for next year {+3d}
### Christmas Cards 25/12/2020
* Write cards >2020-12-05
* Post overseas cards >2020-12-10
Post cards to this country >2020-12-15
\
Store spare cards for next year >2020-12-28
* Bob's birthday on 14/09/2020
  * Find present {-6d}
  * Wrap & post present {-3d}
  * Call Bob {0d}
* Bob's birthday on 14/09/2020
  * Find present >2020-09-08
  * Wrap & post present >2020-09-11
  * Call Bob >2020-09-14

You can use this within a line to have both a deadline and a calculated start date:

For example ... ... becomes
* Post cards deadline 2020-12-18 {-10d} * Post cards deadline 2020-12-18 >2020-12-08

In more detail:

Create new Calendar Events

NotePlan allows for time-blocking, but this only works to its in-built calendar display. With npTools you also create events that are visible and editable in Apple Calendar, or any other apps that use iCloud, not just NotePlan. To do this use the #create_event tag on a line (task, comment or heading) with a timeblocking command such as 3:00-3:45[AM|PM] or 3-5pm or 3PM and optional location such as at Jim's. This allows for meeting events to be listed on a day, and also created in the calendar. In combination with the date offset patterns above, it further allows scheduling preparation time (for example) days or hours before events.

The date of the event is determined in this order:

  1. it will use a >YYYY-MM-DD mentioned in the line. NB: date offset patterns get calculated before it looks to create events.
  2. if it's a calendar note, then use that date
  3. otherwise use today's date.

For example to create a meeting event for a certain day put this in it's daily (calendar) note:

### Project X Meeting #create_event 10:30am at Jim's

To extend this and use the date templates, add a timed task with calendar entry to do some associated tasks 5 days before it, and the following morning:

### Project X Meeting 21-12-2020 #create_event 10:30am at Jim's
* write and circulate agenda {-5d} #create_event 4pm
* send out actions {1d} #create_event 9am

Video showing event creation

Notes on this:

Extend the existing @repeat mechanism

It creates new repeats for newly completed tasks that include a @repeat(interval), on the appropriate future date.

NB: For this feature to work, you need to have the 'Append Completion Date' NotePlan setting turned on, and to have the first type of tidy up (above) happening.

Running the Tools

There are 2 ways of running the script:

  1. with no arguments (ruby npTools.rb), it checks all note and daily files updated in the last 24 hours. This is the way to use it automatically, running one or more times each day. (This is configurable by HOURS_TO_PROCESS below.)
  2. with passed filename pattern(s), where it works on any matching Calendar or Note files. For example, to match the Daily file from 24/3/2020 use ruby npTools.rb 20200324.txt. It can include wildcard patterns to match multiple files, for example "202003*.txt" to process all Daily files from March 2020. (It now needs to be in double quotes for the file pattern matching to work.) If no . is found in the pattern, the pattern matches all files as "*pattern*.*".

You can also specify the following options:

It works with all 3 storage options for storing NotePlan data: CloudKit (the default from NotePlan v3), iCloud Drive and Dropbox.

NB: NotePlan has several options in the Markdown settings for how to mark a task, including -, - [ ]',and [ ]`. All are supported by this script.

Installation and Configuration

  1. Check you have a working Ruby installation.
  2. Install two ruby gems (libraries) (sudo gem install colorize optparse)
  3. Download and copy the script to a place where it can be found on your FILE filepath (perhaps sudo cp npTools.rb /usr/local/bin/)
  4. Make the script executable (chmod 755 npTools.rb)
  5. Change the following constants at the top of the script, as required:
    • hours_to_process: will process all files changed within this number of hours (default 24)
    • TAGS_TO_REMOVE: list of tags to remove. Default ["#waiting","#high"]
    • DAILY_TASKS_SECTION_NAME: the section heading name (without # marks) to file moved tasks in
    • DATE_TIME_LOG_FORMAT: date string format to use in logs
    • DATE_TIME_APPLESCRIPT_FORMAT: date string format to use in AppleScript for event creation -- depends on various locale settings
    • CALENDAR_APP_TO_USE: name of Calendar app to use in create_event AppleScript. Default is 'Calendar'. Can ignore if not using this for event creation.
    • CALENDAR_NAME_TO_USE: name of Calendar to create any new events in. Can ignore if not using this for event creation.
    • CREATE_EVENT_TAG_TO_USE: name of tag to use to trigger creating events. Default is #create_event. Can ignore if not using this for event creation.
    • for completeness, NP_BASE_DIR automatically works out where NotePlan data files are located. (If there are multiple isntallations it selects using the priority CloudKit > iCloudDrive > DropBox.)
    • RE_DATE_OFFSET_FORMAT: regular expression to find date strings in your chosen format, to use as the base date in date offset patterns. See example in the code, but don't change unless you're familiar with regular expressions.
  6. Then run ruby npTools.rb [-options]

The first time you attempt to #create_event, macOS (at least Catalina and Big Sur) will probably ask for permission to update your Calendar.

Automatic running

If you wish to run this automatically in the background on macOS, you can do this using the built-in launchctl system. (For more info on this see for example How to Use launchd to Run Services in macOS.)

Here's the configuration file jgc.npTools.plist that I use to automatically run npTools.rb three times a day:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!-- To load this:   launchctl load ~/Library/LaunchAgents/jgc.npTools.plist
     To unload this: launchctl unload ~/Library/LaunchAgents/jgc.npTools.plist -->
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>jgc.npTools</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/ruby</string>
        <string>/Users/jonathan/bin/npTools</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>09</integer>
            <key>Minute</key>
            <integer>09</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>12</integer>
            <key>Minute</key>
            <integer>09</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>18</integer>
            <key>Minute</key>
            <integer>09</integer>
        </dict>
     </array>
    <key>StandardOutPath</key>
     <string>/tmp/jgc.npTools.stdout</string>
     <key>StandardErrorPath</key>
     <string>/tmp/jgc.npTools.stderr</string>
</dict>
</plist>

Update the filepaths to suit your particular configuration, place this in the ~/Library/LaunchAgents directory, and then run the following terminal command:

launchctl load ~/Library/LaunchAgents/jgc.npTools.plist

Problems? Suggestions?

I now work on improving the Plugins, instead of these old scripts. (Though sadly I have to use JavaScript for them, not Ruby which I do have a soft spot for.)