I want to make a button to copy text I specify to the clipboard

Things I have tried

```button
name copy_to_clipboard
action <%*navigator.clipboard.writeText("text copied");%>
templater true
```
^button-1

this works but it does not work if you add more buttons to the page…
what it does is that if you put more buttons, it will only copy the last thing put in “text copied”

What I’m trying to do

I want it to just copy text specified in the button itself not the so i can make multiple buttons copy text i want quickly.
(Plugins i used)
buttons
templater

It’s a little unclear to me what you want out of this? Why do you want the text copied to the clipboard? Which texts do you want copied?

How would you have access to the buttons once you get a little text on your note? Is this just for one note, or multiple?

It’s a little unclear to me what you want out of this? Why do you want the text copied to the clipboard?

I want a button to copy text for me so that I don’t have to highlight text and copy manually.
I thought it was a tedious job and I though with templater&buttons plugins I could make a button using js to make what I want.

Which texts do you want copied?

in my original example code the part where it said “text copied” is what I want my clipboard to have copied.

How would you have access to the buttons once you get a little text on your note?

I would like to have access to the button wherever put it on the page. I would also like access to use the button if I link the page with ![[page with button in it]] to preview it.

So far the way I tried solving this problem allows me to click the button with other text in the page but not when I’m previewing it on a different page with ![[ ]].

The button I made works unless I have more of them in the same page, I can add text whenever and how ever but if I use this string again but with something different I want copied.

<%*navigator.clipboard.writeText("text copied");%>

any button i would click on the same page would only copy the last thing i want copied
ie.

# Testing

```button
name copy_to_clipboard
action <%*navigator.clipboard.writeText("text copied 0");%>
templater true
```
^button-1

```button
name copy_to_clipboard
action <%*navigator.clipboard.writeText("text copied 1");%>
templater true
```
^button-2

```button
name copy_to_clipboard
action <%*navigator.clipboard.writeText("text copied 3");%>
templater true
```
^button-3

any button i would click would only copy “text copied 3” into my clipboard.

Is this just for one note, or multiple?

I would like to put the different buttons for different things I want copied in different notes, So yes for multiple notes, but I would like to have 2 buttons copy 2 different thing based on what I specified on 1 page minimum.

Sorry if I wasn’t clear on my first post… still new to this forum stuff, and thankyou for replying so soon!

Alternate solution, which I think might be better

Thanks for the details, as it illustrates your use case a lot better. In fact, I would like to point your attention to this thread answer of mine:

It shows an alternate way of handling a similar use case. In this use case, you can write a shorter text, hit the hotkey, and insert the longer text from your snippet collection.

Regarding the error, and possible solution

Regarding your error I reckon it’s due to Obsidian executing all the templater code when you hit either button. Not sure if it’s supposed to do so, or not, but I think that’s what’s happening. So not sure if it’s doable using those buttons that way.

There are some other options, though, and that would be that each button triggers another template, which either holds the text you want copied, or does the copy to clipboard. Something like the following:

```button
name Log
type append template
action Hourly Log Template Note
```
^button-log

The question I would like to ask then though is: Do you really need to push the button? Wouldn’t it be better to just trigger the template to insert stuff directly?

Using hotkeys to trigger templates

It turns out that using Templater you can add shortcuts to the various templates, so I would consider having your various texts as templates, and then connect a hotkey to each template.

This can be done going to Settings > Templater > Template Hotkeys, and then Add new hotkey for template. Find your template after clicking in the box, and then hit the plus icon to select a hotkey to trigger this particular template.

Using a Templater suggester

I’ve not used these myself, but a template in Templater can populate a selection box, where you can choose from a list of values (which you specify). This value can then be processed further, and in your case you just insert it into your text.

In summary

Both of these last solutions allows for the removal of the buttons in your notes (and thusly removes a little bit of clutter), whilst still allowing for easy insert of your various texts.

Still I kind of lean towards the first solution, as that would give you the most flexibility (and reduced clutter overall) to easily insert text based upon some abbreviation/shortcut of your choice.

1 Like