-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorg-apple-notes.el
279 lines (248 loc) · 10.6 KB
/
org-apple-notes.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
;;; org-apple-notes.el --- Org to Apple Notes synchronizer -*- lexical-binding: t; -*-
;;; Customization
(defgroup org-apple-notes nil
"Customization options for org-apple-notes."
:group 'external)
(defcustom org-apple-notes-completing-read-fn #'completing-read
"ido users should set this to ido-completing-read.
Helm users should set this to helm-comp-read.
Ivy users should set this to ivy-completing-read."
:group 'org-apple-notes
:type 'function)
;;; Support
(setq org-apple-notes--applescript-tmpl-base "
on join(itemlist, delim)
set olddelims to AppleScript's text item delimiters
set str to \"\"
set AppleScript's text item delimiters to delim
set str to itemlist as string
set AppleScript's text item delimiters to olddelims
return str
end join
on replace(str, src, target)
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to the src
set the itemlist to every text item of str
set AppleScript's text item delimiters to the target
set newstr to the itemlist as string
set AppleScript's text item delimiters to olddelims
return newstr
end replace
on escape(str)
return my replace(str, \"\\\"\", \"\\\\\\\"\")
end escape
on qe(str)
return \"\\\"\" & my escape(str) & \"\\\"\"
end qe
")
(setq org-apple-notes--applescript-tmpl-list-notes "
tell application \"Notes\"
set accnames to the name of every account
set realaccounts to {}
repeat with h from 1 to (count of accnames)
set accname to (item h of accnames)
tell account accname
set foldernames to the name of every folder
set realfolders to {}
repeat with i from 1 to (count of foldernames)
set foldername to (item i of foldernames)
tell folder foldername
set notenames to the name of every note
set realnotes to {}
repeat with j from 1 to (count of notenames)
set notename to (item j of notenames)
set end of realnotes to (my qe(notename))
end repeat
set outnotes to (\"[\" & (my join(realnotes, \",\")) & \"]\")
end tell
set end of realfolders to (my qe(foldername) & \":\" & outnotes)
end repeat
set outfolders to (\"{\" & (my join(realfolders, \",\")) & \"}\")
end tell
set end of realaccounts to (my qe(accname) & \":\" & outfolders)
end repeat
set outputaccounts to (\"{\" & (my join(realaccounts, \",\")) & \"}\")
end tell
outputaccounts
")
(setq org-apple-notes--applescript-tmpl-note-exists "
set argacct to \"%s\"
set argfolder to \"%s\"
set argnote to \"%s\"
tell application \"Notes\"
tell account argacct
tell folder argfolder
if (note named argnote exists) then
\"true\"
else
\"false\"
end if
end tell
end tell
end tell
")
(setq org-apple-notes--applescript-tmpl-read-note "
set argacct to \"%s\"
set argfolder to \"%s\"
set argnote to \"%s\"
tell application \"Notes\"
tell account argacct
tell folder argfolder
body of note argnote
end tell
end tell
end tell
")
(setq org-apple-notes--applescript-tmpl-write-note "
set argacct to \"%s\"
set argfolder to \"%s\"
set argnote to \"%s\"
set argtext to \"%s\"
tell application \"Notes\"
tell account argacct
tell folder argfolder
if not (note named argnote exists) then
make new note with properties {name: argnote}
end if
set body of note argnote to argtext
end tell
end tell
end tell
")
(defun org-apple-notes--applescript (str)
(declare (indent 0))
(let ((scpt (format "%s\n%s" org-apple-notes--applescript-tmpl-base str)))
(do-applescript scpt)))
(defun org-apple-notes--list-apple-notes ()
"Returns all Notes documents."
(let* ((notes-raw (org-apple-notes--applescript
org-apple-notes--applescript-tmpl-list-notes))
(notes (json-read-from-string notes-raw)))
notes))
(defun org-apple-notes--select-apple-note-from-keywords ()
(let ((parsed-buffer (org-element-parse-buffer))
(note-vars (make-hash-table :test 'equal)))
(org-element-map parsed-buffer
'keyword (lambda (elt) (puthash (downcase (org-element-property :key elt)) (org-element-property :value elt) note-vars)))
(list (gethash "apple-notes-account" note-vars)
(gethash "apple-notes-folder" note-vars)
(or (gethash "apple-notes-title" note-vars)
(gethash "title" note-vars)))))
(defun org-apple-notes--select-apple-note-interactively ()
(let ((notes (org-apple-notes--list-apple-notes))
(notes-for-completing-read (make-hash-table :test 'equal))
(display-names (list)))
;; construct a map where the keys are display strings for note selection and
;; the values are lists consisting of the uniquely combination of account,
;; folder, and note title
(mapc (lambda (account)
(let ((account-name (car account))
(account-folders (cdr account)))
(mapc (lambda (folder)
(let ((folder-name (car folder))
(note-names (cdr folder)))
(mapc (lambda (note-name)
(let* ((args (list account-name folder-name note-name))
(display-name (apply #'format "%s / %s / %s" args)))
(push display-name display-names)
(puthash display-name args notes-for-completing-read)))
note-names)))
account-folders)))
notes)
(let ((selected-note (funcall org-apple-notes-completing-read-fn
"Select Apple note: "
(sort display-names #'string-lessp))))
;; TODO: Allow writing a new note. Prompt individually for account,
;; folder, and note name.
(gethash selected-note notes-for-completing-read))))
(defun org-apple-notes--select-apple-note ()
(let ((from-keywords (org-apple-notes--select-apple-note-from-keywords)))
(seq-let [account folder title] from-keywords
(if (and account folder title)
from-keywords
(org-apple-notes--select-apple-note-interactively)))))
(defun org-apple-notes--apple-note-exists-p (account folder note)
"Checks if a Notes document exists."
(let ((res (org-apple-notes--applescript
(format org-apple-notes--applescript-tmpl-note-exists
account folder note))))
(string-equal "true" res)))
(defun org-apple-notes--read-apple-note (account folder note)
"Read a Notes document."
(org-apple-notes--applescript
(format org-apple-notes--applescript-tmpl-read-note
account folder note)))
(defun org-apple-notes--write-apple-note (account folder note new-contents)
"Destructively write a Notes document."
(org-apple-notes--applescript
(format org-apple-notes--applescript-tmpl-write-note
account folder note
;; escape backslashes, then escape double quotes
(replace-regexp-in-string "\"" "\\\\\""
(replace-regexp-in-string
"\\\\" "\\\\\\\\" new-contents)))))
(defun org-apple-notes--replace-regex-in-buffer (rx replacement)
"Delete the given regex everywhere in the current buffer."
(let ((case-fold-search t))
(save-excursion
(goto-char (point-min))
(while (re-search-forward rx nil t)
(replace-match replacement)))))
;;; Commands
;;;###autoload
(defun org-apple-notes-push ()
"Save the current buffer's contents in a Notes document.
Determine the Notes document to write to as follows:
- if the APPLE-NOTES-ACCOUNT, APPLE-NOTES-FOLDER, and
APPLE-NOTES-TITLE or TITLE keywords are set in the Org file,
use them (i.e., #+APPLE-NOTES-FOLDER: Some Folder)
- otherwise, prompt using org-apple-notes-completing-read-fn
If the Notes document exists, prompt to overwrite it. With a
prefix argument, do not prompt and force overwrite.
"
(interactive)
(if (not (eq 'org-mode major-mode))
(message "not in Org mode")
(seq-let [account folder note] (org-apple-notes--select-apple-note)
(when (or current-prefix-arg
(not (org-apple-notes--apple-note-exists-p account folder note))
(yes-or-no-p (format "%s / %s / %s exists! Overwrite? " account folder note)))
(let ((title (file-name-base (buffer-file-name)))
(tmp-buffer-name (format "*%04x%04x*" (random (expt 16 4)) (random (expt 16 4))))
(org-export-with-section-numbers nil)
(org-export-with-author nil)
(org-export-with-date nil)
(org-export-with-statistics-cookies nil)
(org-export-with-toc nil)
(org-export-with-title nil)
(org-html-preamble nil)
(org-html-postamble nil)
(org-html-head-include-scripts nil)
(org-html-head-include-default-style nil)
(org-html-xml-declaration '(("html" . "")))
(org-html-doctype "")
(org-html-checkbox-type 'ascii)
(org-html-toplevel-hlevel 2))
(unwind-protect
(with-current-buffer (org-export-to-buffer 'html tmp-buffer-name)
;; clean up the exported HTML
(org-apple-notes--replace-regex-in-buffer "<head>.*\\(\n.*\\)*</head>\\(\n*\\)?" "")
(org-apple-notes--replace-regex-in-buffer "<html>\\(\n*\\)?" "")
(org-apple-notes--replace-regex-in-buffer "</html>\\(\n*\\)?" "")
(org-apple-notes--replace-regex-in-buffer "<body>\\(\n*\\)?" "")
(org-apple-notes--replace-regex-in-buffer "</body>\\(\n*\\)?" "")
(org-apple-notes--replace-regex-in-buffer "\\(<.*\\)[[:space:]]+class=\"[[:alnum:]-_]+\"" "\\1")
(org-apple-notes--replace-regex-in-buffer "\\(<.*\\)[[:space:]]+id=\"[[:alnum:]-_]+\"" "\\1")
;; clean up table newlines
(org-apple-notes--replace-regex-in-buffer "\n+<colgroup>" "\n<colgroup>")
(org-apple-notes--replace-regex-in-buffer "\n+<col[[:space:]*]>" "\n<col>")
(org-apple-notes--replace-regex-in-buffer "\n+<tr>" "\n<tr>")
;; replace other newlines with <br> tags
(org-apple-notes--replace-regex-in-buffer "<div>\n*<p>" "<div><br><p>")
(org-apple-notes--replace-regex-in-buffer "\\(\n\\)\\(\n\\)\\{2\\}" "\n<br><br>")
(org-apple-notes--replace-regex-in-buffer "\\(\n\\)\\(\n\\)\\{1\\}" "\n<br>")
;; write
(org-apple-notes--write-apple-note account folder note (buffer-string)))
(kill-buffer tmp-buffer-name)))))))
;;; Footer
(provide 'org-apple-notes)