Skip to content

Instantly share code, notes, and snippets.

@amygrinn
Last active October 4, 2024 05:16
Show Gist options
  • Save amygrinn/4b4f4d37e630988eff4d881e4e26360d to your computer and use it in GitHub Desktop.
Save amygrinn/4b4f4d37e630988eff4d881e4e26360d to your computer and use it in GitHub Desktop.
Autoshare -> Tasker -> Termux -> Org Protocol -> Org Capture
;; Required setup for emacs init file
(package-initialize)
(require 'org)
(require 'org-protocol)
;; Set 'my-org-directory' to the base directory of all of
;; your org files (with a trailing slash). For me it's:
(setq my-org-directory "/scp:org:/home/orgmode/")
;; But it could be a local directory for you
;; Revert buffers automatically if changed on disk. This is useful if you're using
;; a daemon and viewing shared files via tramp or syncthing or whichever tool.
(global-auto-revert-mode t)
(setq org-capture-templates
'(("L" "Link" entry
;; Place new links in the 'IN.org' file under the top-level headline 'Mobile'
(file+headline (lambda () (concat my-org-directory "IN.org")) "Mobile")
;; Ex: ** [[https://google.com][Google]]\n [2021-01-23 Sat 08:07]\n\n Cool site I found
"* %:annotation\n %U\n\n %i"
:prepend t
;; Immediate finish is required to save the file if run from the command line.
:immediate-finish t
;; Kill the buffer after inserting capture if it is not already opened. This prevents
;; overwriting IN.org from another device or daemon
:kill-buffer t)
("N" "Note" entry
;; Place new notes in the 'IN.org' file under the top-level headline 'Mobile'
(file+headline (lambda () (concat my-org-directory "IN.org")) "Mobile")
;; Ex: ** Here's that fix for the bug\n [2021-01-23 Sat 08:07]\n\n rm -rf /
"* %:description\n %U\n\n %i"
:prepend t
;; Immediate finish is required to save the file if run from the command line.
:immediate-finish t
;; Kill the buffer after inserting capture if it is not already opened. This prevents
;; overwriting IN.org from another device or daemon
:kill-buffer t)))
# The 'e' alias will connect to the already running daemon.
# A little speedier than the 'emacs' command because the init
# file has already been loaded.
# The 'e' alias will also start the daemon if it has crashed or failed to start
# Consider that any changes to the emacs init file will not take affect until after the daemon
# reloads. To kill a running daemon, run 'top', find the process number for the emacs daemon in
# the first column, and kill it with 'kill 9582' or whatever the number is.
alias e="emacsclient -nw -s /data/data/com.termux/files/usr/var/run/emacs10444/server -a \"\""
# Note: you may have to edit the above alias depending on where emacs stores it's server file.
# Check the parent directory of 'emacs10444' to get the correct url
# Run `source .profile` to have these changes take effect immediately.

How to add org capture buttons to the Android 'Share' and 'Copy and Paste' menus

This tutorial will show you how to share webpages or copy text directly into your org files without opening any apps.

I prefer this because of how seamless it is. The phone screen is never fully taken over to prevent context switching. Just keep browsing or whatever and know that you haven't forgotten about 'x'.

Install from Play Store ($11.61)

  • Hacker's Keyboard
  • Termux
  • Termux:Float
  • Termux:Boot
  • Tasker
  • AutoShare
  • AutoTools

All files go into the termux home directory unless otherwise specified.

Obviously, I meant for these files to be in a specific order but gists don't allow that. Just do the Termux setup first and the Tasker setup last and everything else is pretty independent.

Once finished, the 'AutoShare' button in the share menu will set off the link capture. For the note capture, select some text and hit the three dots next to 'Copy' and 'Paste' and click 'AutoShare'.

AutoShare setup

  1. Click 'Manage Commands', then the plus button to add a command named 'Org Link' and whichever icon
  2. Click 'Manage Commands', then the plus button to add a command named 'Org Note' and whichever icon
  3. Click 'Manage Text Processors', then the plus button to add a text processor named 'Org Note'

AutoTools setup

Just install AutoTools

#!/data/data/com.termux/files/usr/bin/bash -l
# Place this in ~/.termux/boot/emacs-service.sh
# You may have to create the .termux/boot directory
# This file will load emacs when the phone boots and have
# it running in the background. Must install Termux:Boot.
emacs -daemon
# I'm actually unsatisfied with this because the daemon sometimes hangs,
# and won't accept new clients but won't die. You'll have to kill
# it manually if a notification shows up for a long time that says 'Termux (x tasks)'.
# That means one or more of the captures couldn't connect to the daemon.
Now that you have the emacs init file set up, start the emacs daemon manually
the first time with:
emacs -daemon
It will load automatically whenever your phone boots.
I hope this works for you. Fill up your in-tray (IN.org) throughout the day on the go
with random thoughts and ideas and empty it out at least once a day from your computer.
#!/data/data/com.termux/files/usr/bin/bash -l
# Place this in ~/.termux/tasker/org-note.sh
# You may have to create the .termux/tasker directory
# Launches a capture template 'N' with the following variables available in the template:
# %:description - The title entered in the popup
# %i - The selected text
title=$(echo $1 | base64 --decode | jq -sRr @uri)
note=$(echo $2 | base64 --decode | jq -sRr @uri)
socketFile=/data/data/com.termux/files/usr/var/run/emacs10444/server
emacsclient \
-s $socketFile \
"org-protocol://capture?template=N&title=$title&body=$note"

Tasker setup

  1. Task setup
    Switch to the tasks tab and create two tasks
    All of these actions can be found in the 'Plugin' or 'Variables' sections.
    1. Org Note
      1. AutoTools -> Dialog -> Dialog Type -> Input. Configure this to prompt you for the title of your note. Configure the output variable to be "title"
      2. (x2) Variables -> Variable Convert
        1. Name: %astext, Function: Base64 Encode, Store result in %note
        2. Name: %title, Function: Base64 Encode, Store result in <Leave blank>
      3. Termux:Tasker. The executable name will be 'org-note.sh' and the arguments will be exactly %title %note. You may leave 'Execute in a terminal session' blank
      4. AutoTools -> Dialog -> Dialog Type -> OK. This will let you know that the note was stored ok. Configure how you'd like.
    2. Org Link
      1. AutoTools -> Dialog -> Dialog Type -> Input. Configure this to prompt you for notes. Configure the output variable to be "notes"
      2. (x3) Variables -> Variable Convert
        1. Name: %astext, Function: Base64 Encode, Store result in %url
        2. Name: %assubject, Function: Base64 Encode, Store result in %title
        3. Name: %notes, Function: Base64 Encode, Store result in <Leave blank>
      3. Termux:Tasker. The executable name will be 'org-link.sh' and the arguments will be exactly %url %title %notes. You may leave 'Execute in a terminal session' blank
      4. AutoTools -> Dialog -> Dialog Type -> OK. This will let you know that the link was stored ok. Configure how you'd like.
  2. Create 3 profiles
    Switch to the profiles tab and create 3 profiles. Each trigger can be found under 'Events' -> 'Plugins' > 'Autoshare'
    1. Click 'Autoshare' again
      1. Edit configuration, choose 'Command' -> 'Command Filter' and select 'Org Link'
      2. Add the 'Org Link' task.
    2. Click 'Autoshare' again
      1. Edit configuration, choose 'Command' -> 'Command Filter' and select 'Org Note'
      2. Add the 'Org Note' task.
    3. Click 'Process Text'
      1. Edit configuration, choose 'Text Processor' and select 'Org Note'
      2. Add the 'Org Note' task.

Termux setup

After installing termux, install these packages in the command line:

pkg install emacs jq
@amygrinn
Copy link
Author

Updates on Sep 22,2024

Simplified this whole process quite a bit in a new project here: https://gitlab.com/grinn.amy/read-later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment