-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes-cli.js
More file actions
179 lines (156 loc) · 4.36 KB
/
notes-cli.js
File metadata and controls
179 lines (156 loc) · 4.36 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
class NotesManager {
async executeAppleScript(script) {
try {
const { stdout, stderr } = await execAsync(`osascript -e '${script}'`);
if (stderr) console.error('AppleScript error:', stderr);
return stdout.trim();
} catch (error) {
console.error('Error executing AppleScript:', error.message);
return null;
}
}
async searchNotes(query) {
const script = `
tell application "Notes"
set searchResults to {}
repeat with n in notes
if (name of n contains "${query}") or (body of n contains "${query}") then
set end of searchResults to {name: name of n, body: body of n, id: id of n}
end if
end repeat
return searchResults
end tell
`;
const result = await this.executeAppleScript(script);
if (result) {
console.log('Search Results:');
console.log(result);
}
return result;
}
async createNote(title, body = '') {
const script = `
tell application "Notes"
make new note with properties {name:"${title}", body:"${body}"}
return "Note created: ${title}"
end tell
`;
const result = await this.executeAppleScript(script);
console.log(result);
return result;
}
async editNote(noteTitle, newBody) {
const script = `
tell application "Notes"
repeat with n in notes
if name of n is "${noteTitle}" then
set body of n to "${newBody}"
return "Note updated: ${noteTitle}"
end if
end repeat
return "Note not found: ${noteTitle}"
end tell
`;
const result = await this.executeAppleScript(script);
console.log(result);
return result;
}
async listNotes() {
const script = `
tell application "Notes"
set noteList to {}
repeat with n in notes
set end of noteList to name of n
end repeat
return noteList
end tell
`;
const result = await this.executeAppleScript(script);
if (result) {
console.log('All Notes:');
console.log(result);
}
return result;
}
async getNoteContent(noteTitle) {
const script = `
tell application "Notes"
repeat with n in notes
if name of n is "${noteTitle}" then
return body of n
end if
end repeat
return "Note not found: ${noteTitle}"
end tell
`;
const result = await this.executeAppleScript(script);
console.log(result);
return result;
}
}
async function main() {
const args = process.argv.slice(2);
const command = args[0];
const notesManager = new NotesManager();
switch (command) {
case 'search':
if (args[1]) {
await notesManager.searchNotes(args[1]);
} else {
console.log('Usage: notes-cli search <query>');
}
break;
case 'create':
if (args[1]) {
const title = args[1];
const body = args.slice(2).join(' ');
await notesManager.createNote(title, body);
} else {
console.log('Usage: notes-cli create <title> [body]');
}
break;
case 'edit':
if (args[1] && args[2]) {
const title = args[1];
const newBody = args.slice(2).join(' ');
await notesManager.editNote(title, newBody);
} else {
console.log('Usage: notes-cli edit <title> <new-body>');
}
break;
case 'list':
await notesManager.listNotes();
break;
case 'get':
if (args[1]) {
await notesManager.getNoteContent(args[1]);
} else {
console.log('Usage: notes-cli get <title>');
}
break;
default:
console.log(`
Apple Notes CLI Tool
Usage:
notes-cli search <query> - Search notes by title or content
notes-cli create <title> [body] - Create a new note
notes-cli edit <title> <new-body> - Edit existing note
notes-cli list - List all notes
notes-cli get <title> - Get content of specific note
Examples:
notes-cli search "meeting"
notes-cli create "My Note" "This is the content"
notes-cli edit "My Note" "Updated content"
notes-cli list
notes-cli get "My Note"
`);
}
}
if (require.main === module) {
main().catch(console.error);
}
module.exports = NotesManager;