番外編: Alfred によるタスク作成(ancコマンド、その1): NotionRubyMapping 解説 (66)

はじめに

昨日はクリップボードの内容をタイトルにしたタスク作成を行いました。今日は時間を含むタスクを Notion とカレンダーに一括で登録する Workflow を紹介します。anc というコマンドの後に記述する内容によって、時間を様々に登録できます。

  1. anc タスク名 → 今日の日付でタスクを登録
  2. anc タスク名 4/29 → 4月29日にタスクを登録
  3. anc タスク名 4/29 10:00 → 4月29日 10:00 にタスクを登録
  4. anc タスク名 4/29 10:00 12:00 → 4月29日 10:00 から 12:00 にタスクを登録
  5. anc タスク名 10:00 → 今日の 10:00 にタスク登録
  6. anc タスク名 10:00 12:00 → 今日の 10:00 から 12:00 にタスクを登録

Notion 側の処理

長くなりそうなので、今日は Notion 側の処理だけ解説します。

Workflow

一番最初はキーワードです。今回は anc というキーワードを設定します。

Keyword

次の Run Script は昨日と同じで、システム Ruby の呼び出しです。

Run script

一画面に収まりきらないので、こちらにソースを掲載しておきます。スクリプトはそれほど難しいことはしていないと思います。

require "date"
require "notion_ruby_mapping"
include NotionRubyMapping
NotionRubyMapping.configure { |c| c.token = ENV["NOTION_API_KEY"] }
MY_TZ = ENV["MY_TZ"]

date = nil
end_time = nil
start_time = nil
words = ARGV[0].split " "

end_time = (words.pop + MY_TZ) if words[-1] =~ /\d+:\d+/
if end_time
  start_time = (words.pop + MY_TZ) if words[-1] =~ /\d+:\d+/
  unless start_time
    start_time = end_time
    end_time = nil
  end
end

begin
  date = Date.parse words[-1]
  # If the above Date.parse was success, the last parameter will remove.
  words.pop
rescue
end
date ||= Date.today

datetime = {start: [date, start_time].compact.join(" ")}
datetime[:end] = [date, end_time].join(" ") if end_time
summary = words.join " "
db = Database.find ENV["TASK_DB_ID"]
page = db.create_child_page do |p, pp|
  sd = pp["Start date"]
  sd.start_date = DateTime.parse("#{date} #{start_time}")
  sd.end_date = DateTime.parse("#{date} #{end_time}") if end_time
  pp["Task name"].text_objects << summary
end
print page["url"]

Open URL は昨日のものと同じ処理なので、そのまま合流させてしまっています。

おわりに

今回は、Notion 側の時間付きタスク登録機能までを解説しました。明日はカレンダーの登録部分を紹介します。こちらは NotionRubyMapping は使っていないのですが、よしとしましょう。