How to make org-protocol work?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 03:52:49

问题


I tried the instructions here - I am using firefox on Lubuntu (openbox). But I get the error "Firefox doesn't know how to open this address, because the protocol (org-protocol) isn't associated with any program".

How should I fix this?


回答1:


The following steps for setting up org-protocol work with Ubuntu 16.04 and presumably later versions. Org-mode is assumed to have already been set-up (and installed using apt-get install org-mode or via the ELPA repository).

Set-up

1) Add .desktop file

Create and save a file called org-protocol.desktop to ~/.local/share/applications containing:

[Desktop Entry]
Name=org-protocol
Exec=emacsclient %u
Type=Application
Terminal=false
Categories=System;
MimeType=x-scheme-handler/org-protocol;

Then run:

$ update-desktop-database ~/.local/share/applications/

This step makes Firefox aware that "org-protocol" is a valid scheme-handler or protocol (by updating ~/.local/share/applications/mimeinfo.cache), and causes Firefox to prompt for a program to use when opening these kinds of links.

2) Add config settings to ~/.emacs.d/init.el (or ~/.emacs) file

Have the following settings in your emacs configuration file:

(server-start)
(require 'org-protocol)

Also add some template definitions to the configuration file, for example:

(setq org-protocol-default-template-key "l")
(setq org-capture-templates
 '(("t" "Todo" entry (file+headline "/path/to/notes.org" "Tasks")
        "* TODO %?\n  %i\n  %a")
   ("l" "Link" entry (file+olp "/path/to/notes.org" "Web Links")
        "* %a\n %?\n %i")
   ("j" "Journal" entry (file+datetree "/path/to/journal.org")
        "* %?\nEntered on %U\n  %i\n  %a")))

Now run emacs.

3) Create your notes.org file

Assuming you use the capture templates defined in step 2, you will need to prepare a notes.org file at the location you specified in step 2. You must create this file -- if it is not created along with the headlines specified in step 2, org-mode will just give a warning when you try to capture web-pages. So, given the capture templates from step 2, notes.org should contain the following:

* Tasks
* Web Links

4) Add bookmarklet(s) to Firefox

Save bookmark to toolbar containing something like the following as the location:

javascript:location.href='org-protocol://capture?template=l&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&body='+encodeURIComponent(window.getSelection())

If you are using an older version of org-mode, you may need to use the following instead:

javascript:location.href='org-protocol://capture://l/'+encodeURIComponent(location.href)+'/'+encodeURIComponent(document.title)+'/'+encodeURIComponent(window.getSelection())

Notice the 'l' (lowercase L) in the above URL -- this is what chooses the capture template (automatically) -- it is the key one would normally have to press when capturing with org-mode via C-c c.

When you click on this bookmarklet, Firefox will ask what program to use to handle the "org-protocol" protocol. You can simply choose the default program that appears ("org-protocol").

Using it

(Optionally) select some text on a webpage you're viewing in Firefox. When you click on the bookmarklet, the link and selected text will be placed in the emacs capture buffer. Go to emacs, modify the capture buffer as desired, and press C-c C-c to save it.




回答2:


These instructions are more up-to-date than the ones above.

Add protocol handler

Create file ~/.local/share/applications/org-protocol.desktop containing:

[Desktop Entry]
Name=org-protocol
Exec=emacsclient %u
Type=Application
Terminal=false
Categories=System;
MimeType=x-scheme-handler/org-protocol;

Note: Each line's key must be capitalized exactly as displayed, or it will be an invalid .desktop file.

Then update ~/.local/share/applications/mimeinfo.cache by running:

  • On GNOME:

    update-desktop-database ~/.local/share/applications/
    
  • On KDE:

    kbuildsycoca4
    

Configure Emacs

Init file

Add to your Emacs init file:

(server-start)
(require 'org-protocol)

Capture template

You'll probably want to add a capture template something like this:

("w" "Web site"
 entry
 (file+olp "/path/to/inbox.org" "Web")
 "* %c :website:\n%U %?%:initial")

Note: Using %:initial instead of %i seems to handle multi-line content better.

This will result in a capture like this:

\* [[http://orgmode.org/worg/org-contrib/org-protocol.html][org-protocol.el – Intercept calls from emacsclient to trigger custom actions]] :website:
[2015-09-29 Tue 11:09] About org-protocol.el

org-protocol.el is based on code and ideas from org-annotation-helper.el and org-browser-url.el.

Configure Firefox

Expose protocol-handler

On some versions of Firefox, it may be necessary to add this setting. You may skip this step and come back to it if you get an error saying that Firefox doesn't know how to handle org-protocol links.

Open about:config and create a new boolean value named network.protocol-handler.expose.org-protocol and set it to true.

Note: If you do skip this step, and you do encounter the error, Firefox may replace all open tabs in the window with the error message, making it difficult or impossible to recover those tabs. It's best to use a new window with a throwaway tab to test this setup until you know it's working.

Make bookmarklet

Make a bookmarklet with the location:

javascript:location.href='org-protocol://capture://w/'+encodeURIComponent(location.href)+'/'+encodeURIComponent(document.title)+'/'+encodeURIComponent(window.getSelection())

Note: The w in the URL chooses the corresponding capture template. You can leave it out if you want to be prompted for the template.

When you click on this bookmarklet for the first time, Firefox will ask what program to use to handle the org-protocol protocol. If you are using Ubuntu 12.04, you must add the /usr/bin/emacsclient program, and choose it. With Ubuntu 12.10 or later, you can simply choose the default program that appears (org-protocol).

You can select text in the page when you capture and it will be copied into the template, or you can just capture the page title and URL.

Pentadactyl

If you're using Pentadactyl, you can map key sequences something like this:

map cc -javascript location.href='org-protocol://capture://w/'+encodeURIComponent(content.location.href)+'/'+encodeURIComponent(content.document.title)+'/'+encodeURIComponent(content.document.getSelection())

Note: The JavaScript objects are slightly different for running from Pentadactyl.

You might also want to add one for the `store-link` sub-protocol, like:

map cl -javascript location.href='org-protocol://store-link://'+encodeURIComponent(content.location.href)+'/'+encodeURIComponent(content.document.title)

Capture script

You may want to use this script to capture input from a terminal, either as an argument or piped in:

#!/bin/bash

if [[ $@ ]]
then
    data="$@"
else
    data=$(cat)
fi

if [[ -z $data ]]
then
    exit 1
fi

encoded=$(python -c "import sys, urllib; print urllib.quote(' '.join(sys.argv[1:]), safe='')" "${data[@]}")

# "link" and "title" are not used, but seem to be necessary to get
# $encoded to be captured
emacsclient "org-protocol://capture://link/title/$encoded"

Then you can capture input from the shell like this:

tail /var/log/syslog | org-capture

org-capture "I can capture from a terminal!"

Note: I edited all of this updated and expanded information into the answer above, but the edit was rejected because it "deviates from the original intent of the post" and doesn't "preserve the goals of the post's owner". I'm afraid the wonks have come over from Wikipedia, who would rather throw away useful, up-to-date information (the original answer is 3 years old) and take for granted contributors' valuable time (I've just spent several hours this morning setting up org-protocol and finding out the hard way that the other answer is out-of-date). So here is a new, separate answer that obsoletes the other one. I guess I should just post this on a blog somewhere, since Stack doesn't want to be up-to-date and useful anymore.



来源:https://stackoverflow.com/questions/7464951/how-to-make-org-protocol-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!