▍ humdrum codex / soft
1.4 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { SourceModule } from "../registry";
import { runOsa, parseRows, osaQuote } from "@/lib/mac";
import {
  ThingsConfig,
  ThingsPayload,
  type ThingsConfig as Config,
  type ThingsPayload as Payload,
} from "@/lib/schemas/sources/things";

// Pulls a Things 3 list (default "Today") via AppleScript. Read here; completion
// happens through /api/integrations/things/complete. Registered under kind "todos".
export const thingsModule: SourceModule<Config, Payload> = {
  kind: "todos",
  label: "Today",
  keyless: true, // local app, no API key
  defaultRefreshSeconds: 300,
  configSchema: ThingsConfig,
  payloadSchema: ThingsPayload,

  async fetch({ config }) {
    const script = [
      `set fs to (ASCII character 31)`,
      `set rs to (ASCII character 30)`,
      `set output to ""`,
      `tell application "Things3"`,
      `  repeat with t in to dos of list ${osaQuote(config.list)}`,
      `    set pj to ""`,
      `    try`,
      `      set pj to name of project of t`,
      `    end try`,
      `    set output to output & (id of t) & fs & (name of t) & fs & pj & rs`,
      `  end repeat`,
      `end tell`,
      `return output`,
    ].join("\n");

    const rows = parseRows(await runOsa(script));
    return {
      list: config.list,
      tasks: rows.map(([id, title, project]) => ({
        id: id ?? "",
        title: title ?? "",
        project: project ?? "",
      })),
    };
  },
};