-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapplenotes.el
443 lines (405 loc) · 15.9 KB
/
applenotes.el
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
;;; applenotes.el --- Use Apple Notes in Emacs through AppleScript
;; Copyright (C) 2017 David A. Shamma
;; Author: David A. Shamma
;; Version: 0.1
;; Keywords: apple,notes,note,emacs-applenotes
;; Package-Requires: ((emacs "24"))
;; URL: http://github.com/ayman/emacs-applenotes
;;; License:
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package wraps Apple Notes into elisp via AppleScript.
;;; Code:
(eval-when-compile (require 'markdown-mode))
(require 'ido)
(defgroup applenotes nil
"Interact with Apple Notes through emacs."
:group 'tools
:group 'convenience)
(defcustom applenotes-default-account "iCloud"
"Default account to store notes."
:group 'applenotes
:type 'string)
(defvar applenotes-mode-hook nil)
(defvar applenotes-mode-map
(let ((map (make-keymap)))
(define-key map "q" 'kill-this-buffer)
(define-key map "j" 'next-line)
(define-key map "k" 'previous-line)
(define-key map "g" 'applenotes--refresh-buffer)
map)
"Keymap for AppleNotes major mode.")
(defface applenotes-title-face
'((t :inherit button))
"Face for a title string in buffers."
:group 'applenotes-mode)
(defface applenotes-list-face
'((t :inherit button :bold semi-bold :underline nil))
"Face for a highlighted list."
:group 'applenotes-mode)
(defface applenotes-extra-keywords-face
'((t :inherit font-lock-builtin-face))
"Face for extra highlights."
:group 'applenotes-mode)
(defconst applenotes-font-lock-keywords-1
(list
'("\\(^\w+$\\)" . font-lock-constant-face))
"Minimal highlighting keywords for applenotes mode.")
(defconst applenotes-font-lock-keywords-2
(append applenotes-font-lock-keywords-1
(list '("\\(^ \+.*\\)" . applenotes-list-face)))
"Additional Keywords to highlight in applenotes mode.")
(defconst applenotes-font-lock-keywords-3
(append applenotes-font-lock-keywords-2
(list '("\\(^ Notes.*\\)" . applenotes-extra-keywords-face)))
"Additional Keywords to highlight in applenotes mode.")
(defvar applenotes-font-lock-keywords applenotes-font-lock-keywords-3
"Default highlighting expressions for applenotes mode.")
(defun applenotes-mode ()
"Major mode for navigation Apple Notes mode listings."
(kill-all-local-variables)
(use-local-map applenotes-mode-map)
(set (make-local-variable 'font-lock-defaults)
'(applenotes-font-lock-keywords))
(setq major-mode 'applenotes-mode)
(setq mode-name "Apple Notes")
(run-hooks 'applenotes-mode-hook))
(provide 'applenotes-mode)
;;;###autoload
(defun applenotes--get-account-list ()
"Applescript to get the accounts and folders."
(do-applescript
"tell application \"Notes\"
set noteList to \"\"
repeat with f in every folder
set noteList to noteList & name of (container of f) & \"\t\" & name of f & \"\t\" & id of f & \"\t\" & \"\n\"
end repeat
return noteList
end tell"))
(defun applenotes--get-account-names ()
"Get a list of all the account names."
(do-applescript
"tell application \"Notes\"
set accountList to \"\"
set al to name of every account
repeat with a in al
set accountList to accountList & a & \"\n\"
end repeat
end tell"))
(defun applenotes--get-folder-names (account)
"Get a list of all the ACCOUNT names."
(do-applescript (concat
"tell application \"Notes\"
set accountName to \"" account "\"
set folderNames to name of every folder in account accountName
set folderList to \"\"
repeat with f in folderNames
set folderList to folderList & f & \"\n\"
end repeat
return folderList
end tell")))
(defun applenotes--get-notes-list (location)
"Applescript to get notes in a folder.
Argument LOCATION The core os url (id) to a folder."
(do-applescript (concat
"tell application \"Notes\"
set noteList to \"\"
repeat with n in notes of folder id \"" location "\"
set noteList to noteList & name of n & \"\t\" & id of n & \"\t\" & modification date of n & \"\n\"
end repeat
end tell")))
(defun applenotes--get-all-notes ()
"Applescript to get all notes."
(do-applescript
"tell application \"Notes\"
set noteList to \"\"
repeat with n in every note
set f to (container of n)
set fn to (name of f)
set fi to (id of f)
set k to (container of f)
set kn to (name of k)
set ki to (id of k)
set noteList to noteList & kn & \"\t\" & ki & \"\t\" & fn & \"\t\" & fi & \"\t\" & name of n & \"\t\" & id of n & \"\t\" & modification date of n & \"\n\"
end repeat
end tell"))
(defun applenotes--get-note-body (location)
"Applescript to get the note body.
Argument LOCATION A note id URL."
(let* ((as (concat "tell application \"Notes\"\n"
"set n to note id \"" location "\"\n"
"return body of n\n"
"end tell")))
(message as)
(do-applescript as)))
(defun applenotes--set-note-body (location body)
"Applescript to save the note.
Argument LOCATION A note id URL.
Argument BODY Note body in HTML format."
(let* ((qs (regexp-quote "\\\""))
(escaped-html (replace-regexp-in-string "\"" qs body))
(as (concat "tell application \"Notes\"\n"
"set n to note id \"" location "\"\n"
"set body of n to \"" escaped-html "\"\n"
"end tell")))
(message escaped-html)
(message as)
(do-applescript as)))
(defun applenotes--create-note (account folder title)
"Create a new note in the ACCOUNT of a FOLDER with a TITLE."
(let* ((as (concat
"tell application \"Notes\"\n"
"tell account \"" account "\"\n"
"set n to make new note at folder \""
folder "\" with properties {name:\""
title "\", body:\"<h1>"
title "</h1>\"}\n"
"return id of n\n"
"end tell\n"
"end tell\n")))
(do-applescript as)))
(defun applenotes--md-to-html (md)
"Convert markdown string to HTML.
Argument MD A string in markdown format."
;; TODO: this pushes to the kill ring...we should pop it. I dunno if i can
;; just call (setq kill-ring (cdr kill-ring)).
(markdown-kill-ring-save)
(let* ((html-raw (car kill-ring))
(html (substring-no-properties html-raw))
(html (s-replace "\n" "</div>\n<div>" html))
(html (concat "<div>" html "</div>"))
(html (s-replace "<div></div>" "" html))
(html (s-replace "<div><ul></div>" "<p><ul>" html))
(html (s-replace "<div></ul></div>" "</ul></p>" html))
(html (s-replace "<div><li>" "<li>" html))
(html (s-replace "</li></div>" "</li>" html)))
html))
(defun applenotes--html-to-md (html)
"Convert HTML tring to markdown.
Argument HTML A string in HTML format."
(let* ((md (s-replace "<div>" "" html))
(md (s-replace "</div>" "" md))
(md (s-replace "<h1>" "# " md))
(md (s-replace "</h1>" " #" md))
(md (s-replace "<b>" "**" md))
(md (s-replace "</b>" "**" md))
(md (s-replace "<i>" "_" md))
(md (s-replace "</i>" "_" md))
(md (s-replace "<ul>" "" md))
(md (s-replace "</ul>" "" md))
(md (s-replace "</li>" "" md))
(md (replace-regexp-in-string "^[ \t\r\v\f]*<li>" " * " md))
(md (replace-regexp-in-string "<span .+\">" "" md))
(md (s-replace "</span>" "" md))
(md (s-replace "<br>" "" md))
(md (s-replace "<u>" "" md)) ;; kill underlines
(md (s-replace "</u>" "" md)))
md))
(defun applenotes--notes-list (folder name parent)
"Show the list of list of notes.
Argument FOLDER Folder ID url of the note.
Argument NAME Name (title) of the note.
Argument PARENT Container (account) id of the note."
(let* ((notes-list-raw (applenotes--get-notes-list folder))
(notes-list (substring notes-list-raw 1 -2))
(lines (split-string notes-list "\n"))
(notes-buffer-name
(concat "*Apple Notes " parent "-" name " List*")))
(with-current-buffer (get-buffer-create notes-buffer-name)
(switch-to-buffer notes-buffer-name)
(read-only-mode 0)
(erase-buffer)
(while lines
(let* ((l (car lines))
(ll (split-string l "\t"))
(title (car ll))
(location (cadr ll))
(mod-date (caddr ll)))
(insert " + ")
(insert-button title
'follow-link t
'help-echo (concat "Modified: " mod-date)
'name title
'link location
'action (lambda (b)
(applenotes--note-open
(button-get b 'link)
(button-get b 'name))))
(insert "\n")
(setq lines (cdr lines))))
(goto-char (point-min))
(read-only-mode)
(applenotes-mode))
(other-window 1)))
(defun applenotes--note-open (location title)
"Open an Apple note.
Argument LOCATION A note id URL.
Argument TITLE Title of the note (for the modeline)."
(let* ((note-body-raw (applenotes--get-note-body location))
(note-body (substring note-body-raw 1 -2))
(note-buffer-name (concat title " Apple Note"))
(note-buffer (get-buffer-create note-buffer-name)))
(with-current-buffer note-buffer
(switch-to-buffer note-buffer)
(display-buffer note-buffer-name)
(read-only-mode 0)
(erase-buffer)
(insert (applenotes--html-to-md note-body))
(goto-char (point-min))
(not-modified)
(markdown-mode)
(set (make-local-variable 'applenotes--is-note) 't)
(set (make-local-variable 'applenotes--loc) location)
(set (make-local-variable 'applenotes--name) title)
(local-set-key "\C-x\C-s" 'applenotes--note-save))
(other-window 1)))
(defun applenotes-all-accounts ()
"Show the list of list of notes."
(interactive)
(let* ((notes-list-raw (applenotes--get-account-list))
(notes-list (substring notes-list-raw 1 -2))
(lines (sort (split-string notes-list "\n") 'string<))
(accounts-buffer-name "*Apple Notes Accounts List*")
(accounts-buffer (get-buffer-create accounts-buffer-name)))
(with-current-buffer accounts-buffer
(switch-to-buffer accounts-buffer)
(read-only-mode 0)
(erase-buffer)
(setq last-account "")
(while lines
(let* ((l (car lines))
(ll (split-string l "\t"))
(account (car ll))
(notebook (cadr ll))
(location (caddr ll)))
(when (not (string= last-account account))
(insert (concat account "\n"))
(setq last-account account))
(insert " + ")
(insert-button notebook
'follow-link t
'help-echo (concat "Open notes in "
account ":" notebook)
'name notebook
'link location
'parent account
'action (lambda (b)
(applenotes--notes-list
(button-get b 'link)
(button-get b 'name)
(button-get b 'parent))))
(insert "\n")
(setq lines (cdr lines))))
(goto-char (point-min))
(read-only-mode)
(applenotes-mode))
(other-window 1)))
(defun applenotes-all-notes ()
"Show the list of list of notes."
(interactive)
(let* ((notes-list-raw (applenotes--get-all-notes))
(notes-list (substring notes-list-raw 1 -2))
(lines (split-string notes-list "\n"))
(notes-buffer-name (concat "*Apple Notes - All Notes*")))
(with-current-buffer (get-buffer-create notes-buffer-name)
(switch-to-buffer notes-buffer-name)
(read-only-mode 0)
(erase-buffer)
(while lines
(let* ((l (car lines))
(ll (split-string l "\t"))
(account-name (car ll))
(account-id (cadr ll))
(folder-name (caddr ll))
(folder-id (cadddr ll))
(title (car (cddddr ll)))
(location (cadr (cddddr ll)))
(mod-date (caddr (cddddr ll))))
(insert " + ")
(insert-button title
'follow-link t
'help-echo (concat "Modified: " mod-date)
'name title
'link location
'action (lambda (b)
(applenotes--note-open
(button-get b 'link)
(button-get b 'name))))
(insert (concat "\n (in "))
(insert-button folder-name
'follow-link t
'name title
'link location
'folder-name folder-name
'folder-id folder-id
'account-id account-id
'account-name account-name
'action (lambda (b)
(applenotes--notes-list
(button-get b 'folder-id)
(button-get b 'folder-name)
(button-get b 'account-id))))
(insert " of ")
(insert-button account-name
'follow-link t
'name title
'link location
'folder-name folder-name
'folder-id folder-id
'account-id account-id
'account-name account-name
'action (lambda (b)
(applenotes-account-list)))
(insert ")")
(insert "\n")
(setq lines (cdr lines))))
(goto-char (point-min))
(read-only-mode)
(applenotes-mode))
(other-window 1)))
(defun applenotes--note-save ()
"Save Apple Note IFF it's a Apple Note buffer."
(interactive)
(when (local-variable-if-set-p 'applenotes--is-note)
(let* ((html (applenotes--md-to-html (buffer-string))))
;; (message html)
(applenotes--set-note-body applenotes--loc html)
(not-modified)
(message (concat "Saved Apple Note: " applenotes--name))))
)
(defun applenotes-new-note ()
"Create a new note in an account and list.
You'll have to use a folder and account that is already there for now."
(interactive)
(let* ((account-list-raw (applenotes--get-account-names))
(account-list (substring account-list-raw 1 -2))
(accounts (sort (split-string account-list "\n") 'string>))
(account (ido-completing-read
"Create note in which account: " accounts))
(folders-raw (applenotes--get-folder-names account))
(folders-list (substring folders-raw 1 -2))
(folders (sort (split-string folders-list "\n") 'string>))
(folder (ido-completing-read
(concat "Create note in which folder of " account ":") folders))
(title (read-string "Title of new note: "))
(location (applenotes--create-note account folder title)))
(message location)
(applenotes--note-open location title)))
(defun applenotes--refresh-buffer ()
"Refresh the current buffer listing."
;; Get TYPE from buffer var if its a proper buffer.
(message "Refresh applenotes not implemented yet."))
(provide 'applenotes)
;;; applenotes.el ends here