Copy note URL command

Brings “Copy note URL” action into the command pallete

!function() {
	function copyURL() {
		const vaultName = app.vault.getName().replace(/\s/gi, '%20')
		const noteName = app.workspace.activeLeaf.view.file.name.replace(/\s/gi, '%20')
		const path = `obsidian://open/?vault=${vaultName}&file=${noteName}`

        navigator.clipboard.writeText(path)
	}

	app.commands.addCommand({
	    id: 'app:copyURL',
	    name: 'Copy note URL',
	    callback: copyURL,
	    hotkeys: []
	})
}()

Tested on Windows 10.

Related:

4 Likes

Sorry for a silly question: once the code is copied to the console, how do you save it? It is not automatic because closing and reopening the console shows the code is not there. Cmd+S does not do it either - I am on macOS.

You should paste the code into the console and press Enter. So it’ll start to work.

After rerun Obsidian this functionality will be disabled, but you can enable it again by:

  1. opening console
  2. pressing Up Arrow (which should bring the earliest executed snippet)
  3. press Enter to execute the snippet
1 Like

@mrjackphil: works fine, and I am on macOS.
Many thanks.

Hi @mrjackphil, I’m new to javascript and have a question regarding your code:
Is there a reason why you (and many others) url-encode the whitespace “manually” instead of using encodeURIComponent? Are there maybe advantages/pitfalls to be aware of?

Actually, I don’t use it often so just forgot about it. Used it in my “Copy Markdown” plugin. Thanks :slight_smile: