org-attaching the latest image from my Supernote via Browse and Access
Posted: - Modified: | emacs, supernote, org: Use sketch links when possible. Recolor before cropping so that the grid is removed.
Text from sketch
Supernote A5X
- Screen mirroring (pixelated) -> Puppeteer screenshot (or maybe .mjpeg?)
- Browse & Access (HTTP) -> latest file: recognize text, recolor, crop, upload?
- Dropbox/Google Drive (slow) -> batch process: recognize text, recolor, upload
Bonus: Autocropping encourages me to just get stuff out there even if I haven't filled a page
ideas: remove template automatically? I wonder if I can use another color…
2024-09-26-01
I want to quickly get drawings from my Supernote A5X into Emacs so that I can include them in blog posts. Dropbox/Google Drive sync is slow because it synchronizes all the files. The Supernote can mirror its screen as an .mjpeg stream. I couldn't figure out how to grab a frame from that, but I did find out how to use Puppeteer to take an screenshot of the Supernote's screen mirror. Still, the resulting image is a little pixelated. If I turn on Browse and Access, the Supernote can serve directories and files as webpages. This lets me grab the latest file and process it. I don't often have time to fill a full A5 page with thoughts, so autocropping the image encourages me to get stuff out there instead of holding on to things.
(defvar my-supernote-ip-address "192.168.1.221")
(defun my-supernote-get-exported-files ()
(condition-case nil
(let ((data (plz 'get (format "http://%s:8089/EXPORT" my-supernote-ip-address)))
(list))
(when (string-match "const json = '\\(.*\\)'" data)
(sort
(alist-get 'fileList (json-parse-string (match-string 1 data) :object-type 'alist :array-type 'list))
:key (lambda (o) (alist-get 'date o))
:lessp 'string<
:reverse t)))
(error nil)))
(defun my-sketch-insert-latest-doodle ()
(interactive)
(let* ((file (my-latest-sketch)))
(insert
(format
"#+begin_right-doodle
#+ATTR_HTML: :title
%s
#+end_right-doodle"
(org-link-make-string (concat "file:" file))))))
(defun my-supernote-download-latest-exported-file ()
"Save exported file in downloads dir."
(interactive)
(let* ((info (car (my-supernote-get-exported-files)))
(dest-dir my-download-dir)
(new-file (and info (expand-file-name (file-name-nondirectory (alist-get 'name info)) dest-dir)))
renamed)
(when info
(copy-file
(plz 'get (format "http://%s:8089%s" my-supernote-ip-address
(alist-get 'uri info))
:as 'file)
new-file
t)
new-file)))
(defun my-sketch-insert-latest ()
(interactive)
(let ((renamed (my-latest-sketch)))
(when (and renamed (derived-mode-p 'org-mode))
(if (string-match "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9] "
(file-name-base renamed))
(org-insert-link nil (concat "sketchFull:" (file-name-base renamed)))
;; insert the link
(org-insert-link nil (concat "file:" renamed)))
(org-redisplay-inline-images))))
(defun my-supernote-org-attach-latest-exported-file ()
(interactive)
;; save the file to the screenshot directory
(let ((info (car (my-supernote-get-exported-files)))
new-file
renamed)
;; delete matching files
(setq new-file (expand-file-name
(replace-regexp-in-string " " "%20" (alist-get 'name info) (org-attach-dir))))
(when (file-exists-p new-file)
(delete-file new-file))
(org-attach-attach
(format "http://%s:8089%s" my-supernote-ip-address
(alist-get 'uri info))
nil
'url)
(setq new-file (my-latest-file (org-attach-dir)))
;; recolor
(my-sketch-recolor-png new-file)
;; autocrop that image
(my-image-autocrop new-file)
;; possibly rename
(setq renamed (my-image-recognize-get-new-filename new-file))
(when renamed
(setq renamed (expand-file-name renamed (org-attach-dir)))
(rename-file new-file renamed t)
(my-image-store renamed) ; file it in my archive
(setq new-file renamed))
;; use a sketch link if it has an ID
(if (string-match "^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9] "
(file-name-base renamed))
(org-insert-link nil (concat "sketchFull:" (file-name-base renamed)))
;; insert the link
(org-insert-link nil (concat "attachment:" (replace-regexp-in-string "#" "%23" (file-name-nondirectory new-file)))))
(org-redisplay-inline-images)))