110 lines
3.4 KiB
JavaScript
110 lines
3.4 KiB
JavaScript
import { plainTextToNoteHtml, runJxa } from "../apple-events.js";
|
|
|
|
const NOTES_LIST_SCRIPT = String.raw`
|
|
function run(argv) {
|
|
const input = JSON.parse(argv[0]);
|
|
const app = Application("/System/Applications/Notes.app");
|
|
const query = (input.query || "").toLowerCase();
|
|
const folderName = input.folder || "";
|
|
const limit = input.limit;
|
|
const found = [];
|
|
const accounts = app.accounts();
|
|
|
|
for (let a = 0; a < accounts.length && found.length < limit; a++) {
|
|
const folders = accounts[a].folders();
|
|
for (let f = 0; f < folders.length && found.length < limit; f++) {
|
|
const folder = folders[f];
|
|
const currentFolder = String(folder.name());
|
|
if (folderName && currentFolder !== folderName) continue;
|
|
const notes = folder.notes();
|
|
for (let n = 0; n < notes.length && found.length < limit; n++) {
|
|
const note = notes[n];
|
|
let title = "";
|
|
let text = "";
|
|
try { title = String(note.name()); } catch (_) {}
|
|
try { text = String(note.plaintext()); } catch (_) {}
|
|
if (query && (title + "\n" + text).toLowerCase().indexOf(query) === -1) continue;
|
|
found.push({
|
|
id: String(note.id()),
|
|
title: title,
|
|
folder: currentFolder,
|
|
modifiedAt: note.modificationDate().toISOString(),
|
|
preview: input.includePreview ? text.slice(0, 180) : undefined
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return JSON.stringify(found);
|
|
}`;
|
|
|
|
const NOTES_READ_SCRIPT = String.raw`
|
|
function run(argv) {
|
|
const input = JSON.parse(argv[0]);
|
|
const app = Application("/System/Applications/Notes.app");
|
|
const accounts = app.accounts();
|
|
for (let a = 0; a < accounts.length; a++) {
|
|
const folders = accounts[a].folders();
|
|
for (let f = 0; f < folders.length; f++) {
|
|
const notes = folders[f].notes();
|
|
for (let n = 0; n < notes.length; n++) {
|
|
const note = notes[n];
|
|
if (String(note.id()) === input.id) {
|
|
return JSON.stringify({
|
|
id: String(note.id()),
|
|
title: String(note.name()),
|
|
folder: String(folders[f].name()),
|
|
bodyHtml: String(note.body()),
|
|
plaintext: String(note.plaintext()),
|
|
createdAt: note.creationDate().toISOString(),
|
|
modifiedAt: note.modificationDate().toISOString()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
throw new Error("Note not found");
|
|
}`;
|
|
|
|
const NOTES_CREATE_SCRIPT = String.raw`
|
|
function run(argv) {
|
|
const input = JSON.parse(argv[0]);
|
|
const app = Application("/System/Applications/Notes.app");
|
|
const accounts = app.accounts();
|
|
let destination = null;
|
|
|
|
for (let a = 0; a < accounts.length && !destination; a++) {
|
|
const folders = accounts[a].folders();
|
|
for (let f = 0; f < folders.length; f++) {
|
|
if (!input.folder || String(folders[f].name()) === input.folder) {
|
|
destination = folders[f];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (!destination) throw new Error("Notes destination folder not found");
|
|
|
|
const note = app.Note({body: input.html});
|
|
destination.notes.push(note);
|
|
return JSON.stringify({
|
|
id: String(note.id()),
|
|
title: String(note.name()),
|
|
folder: String(destination.name())
|
|
});
|
|
}`;
|
|
|
|
export function listNotes(input) {
|
|
return runJxa(NOTES_LIST_SCRIPT, input);
|
|
}
|
|
|
|
export function readNote(id) {
|
|
return runJxa(NOTES_READ_SCRIPT, { id });
|
|
}
|
|
|
|
export function createNote({ title, body, folder }) {
|
|
return runJxa(NOTES_CREATE_SCRIPT, {
|
|
folder,
|
|
html: plainTextToNoteHtml(title, body),
|
|
});
|
|
}
|