Clip selection to Obsidian

This extension snippet will copy and insert the current selection in a document or web page to Obsidian.

There are a few prerequisites.

  • The Advanced Obsidian URI community plug-in for Obsidian must be installed. (Info on Obsidian community plugins)
  • You must know the name of the destination “vault” in Obsidian. (A macOS folder that you have configured as a vault in Obsidian.) The vault name must be URL-encoded.
  • This example puts the text in the “daily note” (configured in Obsidian settings), in a header section named “Clippings”. These are both configurable. (Note, the %0A string means add a new line before pasting.)
# Popclip
name: OBSClipper
icon: O
url: obsidian://advanced-uri?vault=<vault name URL encoded>&daily=true&heading=Clippings&data=%0A***&mode=append

This version of the snippet will append the selected text to the “daily note” within a heading section of that note named Clippings. If you check the Advanced Obsidian URI documentation there is a very robust set of options for configuring the URL.

Question for @nick – I would like to append to the text the markdown-formated URL of the source webpage. Is it possible to grab both this and the selected text in the extension snippet? So I would paste <text> plus <markdown-formated source URL>

4 Likes

This is very nice, thanks for sharing! I’ve had a few emails recenly asking for an Obsidian extension so maybe I can point them to this.

I’m really liking how with the new snippets, the source is front and centre, which invites people to see how it works and to think of customizing it.

Regarding the question. It isn’t possible currently with the “URL” extension type but you could do with with a little bit of JavaScript. I’ll have a go…

I’ve also modified it to use the markdown backing the selection rather than just the plain text.

# PopClip - Obsidian extension, markdown variant
name: OBSClipper
icon: O
capture html: true # this is needed when we want markdown text
javascript: | 
  const vaultName = encodeURIComponent('Test Vault')
  let clipping = popclip.input.markdown
  if (popclip.context.browserUrl.length > 0) { // append markdown source link if available
    clipping += `\n[${popclip.context.browserTitle}](${popclip.context.browserUrl})`
  }
  clipping = encodeURIComponent(clipping)
  popclip.openUrl(`obsidian://advanced-uri?vault=${vaultName}&daily=true&heading=Clippings&data=%0A${clipping}&mode=append`)

(The above block is an extension snippet - select it to install the extension with PopClip)

Edit to add: I got it working! I’m new to Obsidian — the following is probably obvious to Obsidian users, but for anyone trying Obsidian for the first time, I also had to enable the “Daily Note” core plugin which was disabled by default. This created a note with the current date. And I had to manually add a heading # Clippings in the daily note before it would work (@EdM - Do we have to add the heading every day?)

Yes I forgot to mention these prerequesites:

  • You must have Obsidian installed.
  • The Daily Note plug-in must be activated.
  • Either Include a heading # Clippings in the template file for the Daily Note, or add the heading manually.

This and my original post seems like a lot of prerequisites, but I believe these are standard one-time set-up tasks for anyone beginning to work with Obsidian.

Very Important

The reivsed code for the Obsidian extension snippet that @nick posted above is great – however do not use it as-is. You must replace the string test vault (in single quotes) in line 6 with the name of your own destination vault. Otherwise, the clipper will fail with a very annyoying Obsidian message.

1 Like

Great stuff!

You must replace the string test vault

This made me think to add another example showing how we can add an option to the extension for setting the vault name:

# PopClip - Obsidian extension, markdown variant
name: OBSClipper
icon: O
options:
- identifier: vault
  label: Vault Name
  type: string
capture html: true # this is needed when we want markdown text
javascript: | 
  const vaultName = encodeURIComponent(popclip.options.vault)
  let clipping = popclip.input.markdown
  if (popclip.context.browserUrl.length > 0) { // append markdown source link if available
    clipping += `\n[${popclip.context.browserTitle}](${popclip.context.browserUrl})`
  }
  clipping = encodeURIComponent(clipping)
  popclip.openUrl(`obsidian://advanced-uri?vault=${vaultName}&daily=true&heading=Clippings&data=%0A${clipping}&mode=append`)
#end
1 Like

I scratched my head a bit, then realized this is what happens:

image

Cool!

@nick – is there a comprehensive manual for extension snipppet syntax and best practices? I keep seeing things I didn’t know how to do or didn’t know were possible.

The main documentation of the extension structure is here.

The JavaScript interface is documented here (WIP): PopClip JavaScript reference

I realise it is quite a lot to take in. Let me know if anything doesn’t make sense.

1 Like

This is awesome! Thanks

1 Like

Thank you! Bookmarked. This would be an excellent note for the “How-Tos…” category of the forum, which is were I looked first.

1 Like

Hi, I have been using pop clip and obsidian but the idea of snippets is new to me. I am not sure if I have done everything right because the app does not do anything after I click “install extension OBSClipper” button. I generated my own obsidian URI and I am familiar with it.

apparently, the final “#end” is not needed—when i included it, the script confused me by opening that small window; but it still worked fine, and didn’t open that window, when i copied the text without that term…

also, this works with the Periodic Notes plugin, which should ⁄NOT\ be used at the same time as the (Core) Daily Note plugin

last—for simplicity & less headache, “# Clippings” should be at the bottom of the template followed by a new line (tho it may not be absolutely required)…

1 Like

My most used popclip utility becomes even more used with this! So cool!!

I want to try this to capture text from my email followed by its link. Superhuman email link is generated and pasted to the clipboard using the key combo “ctrl + /”. I assume I have to modify this part of the code:

to add the email link generated by the key combo press instead of the browser url.

Appreciate any help on what the modified code can look like.

1 Like

I only added the #end to make it easier to select the text block. Omitting it should make not difference however. Thanks for ther tips and welcome to the forum!

Appreciate any help on what the modified code can look like.

Maybe something like:

popclip.pressKey('\', util.constant.MODIFER_CONTROL)
clipping += `\n${pasteboard.text}`
1 Like

Not required. It’s a personal choice. It can be anywhere within the note that makes sense to the user – Advanced URI will find that heading as long as it exists.

Perfect! Thanks much :slight_smile:

1 Like

Any help would be deeply appreciated. You’ll are doing something I am missing.

I installed the extension but when I try to clip something, it opens the correct Vault but to the most recent accessed note (not specifically the Daily Note) and it does not add any content. What might I be missing?

Yes, I have the Obsidian URI plugin and daily notes and have created a Clippings heading in my daily note template.

If the obisidian://advanced-uri?... link does not work for you, the best way to troubleshoot it is to create the link manually – say in TextEdit or other plain text context – and test it apart from the PopClip extension. File in the fields in the test URL with dummy text. If the test URL created this way still does not work, then make sure your Daily Note and Advanced URI plugin settings in Obsidian are correct. If that still results in error, then open a ticket with the devleoper of the Advanced URI plugin.

The PopClip extension in @nick’s revision here works. Errors are most likely on the user side.

Indeed… simple problem - I installed but did not activate the URI plugin.

Works fine now.

Thanks

Here’s my version

# PopClip superhuman with link to obsidian
name: SHEntry
icon: circle S
capture html: true # this is needed when we want markdown text
javascript: |
  let clipping = popclip.input.markdown
  popclip.pressKey('/', util.constant.MODIFIER_CONTROL)
  clipping += `\n[Email link](${pasteboard.text})`
  clipping = encodeURIComponent(clipping)
  popclip.openUrl(`obsidian://advanced-uri?vault=notes&filepath=/fleeting/note&data=%0A${clipping}`)

The pasteboard.text returns the clipboard content prior to running the snippet. The script copies the correct link but that is not added to the clipping. Instead it remains in the clipboard after the snippet is run. I’m sure I’m missing something obvious :thinking:

Also, is there a way to dynamically change the created note filename each time this is run (e.g. using timestamp)?