Copy text and block link to clipboard

Things I have tried

Searching the forum, I found a post that seemed very similar. Unfortunately it doesn’t quite answer what I am looking for. Here’s a link: Copy text from one note to another with easy link back.

I have tried doing this via Templater as well as using AutoHotkey scripts, trying to utilize the ability to set a hotkey for Copy Block Link with the plugin that does this. But, I cannot get anything to accomplish this all in one step (or really two steps, as I would have to initiate the copy/create the block reference (1), then paste the copied text and block link (2))

What I’m trying to do

I want to be able to copy text from one note to the clipboard. At the same time, I would like for a line with a block reference to be generated above the selection in the source note. The link to this block should be copied to the clipboard as well. So when I do a paste into another note, it should add both the copied text and the block link to the line above the copied text in the source note. I realize that this can be accomplished in 4 steps, but having it in 2 steps would save so much time, as I am going to be doing this thousands of times for the next few weeks, as I try to arrange my backlog of content.

Thanks so much for any assistance. I really appreciate it very much.

1 Like

If you’re able to do it in 4 steps, then maybe it’s possible to combine those steps using a QuickAdd choice combining those steps?

I’m not sure what 4 steps you’re talking about, but QuickAdd is capable of combining loads of steps into a single choice (which can be linked to a command and then a hotkey).

1 Like

Thanks. I was thinking the same thing, but couldn’t figure out how to accomplish it. The 4 steps are:

  • copy selected text
  • paste selection into another note
  • add a new line above text in source note and copy block link from that line (using Copy block link plugin)
  • paste the block link aside text previously pasted into the other note

One thing I am considering is trying to use clipboard history, such that I can have an AHK macro that

  • first copies my selected text
  • then inserts a line above selection with some text like:
- Block
  • then triggers the hotkey for the Copy Block Link plugin to copy the block link to the clipboard, thus having the copied text in slot 2 of the clipboard history and the copied block link in slot 1

Then, I could have a separate macro that would allow me to first choose the location in another note where I want to paste, and trigger the macro, thus doing the following:

  • paste the copied text content (from slot 2 in clipboard history)
  • paste the copied block link (from slot 1 in the clipboard history)

Thanks for the response! I’ll definitely make another post if I can figure out a solution.

Come to think a little more about this, I thought of another way:

  • Go to the file you want to paste it into, this trigger it into the page history
  • go back to the file you want to copy from and select the block
  • insert a template which does the following:
    • insert the block with a blockId you generate yourself
    • reinsert the selected block
    • copy the block you made into the clipboard
    • move to the other file, and insert clipboard

All this should be doable within templater

Alternatively you could skip the alternate file stuff, and just select, replace and set the clipboard ready for insertion.

1 Like

Sorry for the long response.

Thanks! I love your solution. So if I am understanding correctly, the process would be as follows:

  • Go to the file you want to paste it into, this trigger it into the page history - I am doing this so that when the Templater script jumps back into this note later, it is already at the correct location. This is important since I do not want to simply add the block at the bottom of the chosen note. This was why thought I might have to rely on the clipboard, thus allowing me to place the pasted text via first placing my cursor before pressing ctrl v.
  • Go back to the file you want to copy from and select the block - For this, I should mention that I intend to sometimes have more than a single block selected. In some cases, it may even be possible that my selection will include heading structures. This was why I gave up on the idea that I would be able to have a link directly to a block for the source content. Instead, I opted to just add a line above the copied text and generate a blockid for that line. In the end, I am only trying to make it easy for me to be able to return to the source note from the note with the pasted text. By having the line above, I will at least be able to quickly navigate to the start of the content.
  • Insert a template which does the following:
    • insert the block with a blockId you generate yourself. I interpret this as the following. The triggered Templater script will first cut the selected text and save that to a variable. Then the script will open a dialog that would require me to enter a name for the blockid (for example Name I entered in dialog). After I enter the blockid name, the script would save that name in another variable. It would first make the entered text a legal blockid (no spaces, etc). Then it would add a line like Block is Below ^Name-I-entered-in-dialog for example.
    • reinsert the selected block - The script would then add a new line below and paste the selected text that was copied to the variable.
    • copy the block you made into the clipboard - For this, I am unclear exactly how you imagine this being accomplished, but I understand the reasoning. In my understanding explained above, I thought that this value might have been stored in a variable, which could be pasted. Perhaps I am misunderstanding how Templater would do this. I am reading up on it now.
    • move to the other file, and insert clipboard - For this, I am assuming that there is some way that Templater has access to the “page history” like you mentioned. So the script would jump back to the previously accessed note, and paste the following: a line with a link to the source note (adding the blockid, for example [[the source note#^Name-I-entered-in-dialog]]). Finally, the script would create a new line and paste the copied text from the source note.

I understand that this may not be exactly as you envisioned it. But I am trying to comprehend it in a way that I would imagine it actually being created with Templater. I am not too experienced in Templater, so perhaps I am misunderstanding what is exactly possible and how it would be accomplished. If you see any glaring misunderstandings, I would appreciate any sort of suggestions. Thanks @holroy! I feel indebted. But, of course, I understand that your curiosity is what keeps you solving these puzzles. I very much appreciate the assistance!

That seems about correct. And you’re correct about not needing to use the clipboard, and just using a variable. I saw that afterwards, but it was late…

Regarding the history thingy that’s a general api thing I’ve seen referenced around here, so it’s not Templater per se, but it’s accessible from there.

Lastly, I’m not sure how easy (if at all possible) it would be to select multiple paragraphs. I’m not even sure how easy the entirety of this will be, but it should be doable.

1 Like

Thanks!

So, do you mean something like this?

I am trying to read and further research this. But I might as well ask you directly. Would this be still done via Templater, just using the Obsidian module of Templater? I see that that gives access to these types of values.

This is all very interesting. And it definitely seems worth learning. I am just trying to figure out where to start. I am finding some posts that seem relevant.

Thanks again!

Yes, something like that used within templater, and maybe something related to some of AlanG’s stuff:

I’ve not figured out all of the bits and pieces myself, but I see that there is a way forward.

1 Like

Thanks! This is very interesting stuff. If I figure it out, I’ll surely post it here.

I kept on getting errors when trying to use getLastOpenFiles in various contexts. It is clear I am way beyond my depth. But, I persisted, and was able to find getMostRecentLeaf and setActiveLeaf while flipping through the api. I am not sure that these are actually useful, but they do appear relevant. I realize there is very little chance I will be able to piece this together at the level of understanding I currently have. But, I am going to start trying to further understand how all these methods can be used. It definitely was an eye opener to see how much is available to be manipulated. Thanks again!

@holroy I just wanted to share my current solution. It isn’t pretty, but it kind of gets the job done. At least for now, it appears to be working. I surely would like to find a better solution. But, this is where I’m at. For anyone seeing this, I definitely wouldn’t recommend actually using the script unless you are familiar with AHK and understand the potential consequences. Also, for it to actually work, you need to have the same hotkeys as I do, and be using Ditto as a clipboard manager, also with custom hotkeys. This is described within the commented lines (beginning with semicolons) in the script. So, I also attached a demo of it in action.
SourceToDestination_AHK_v1

I understand it could be a lot shorter. I just like to be able to understand and edit it later. I also have it doing a lot of sleeping so it works well:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;----------------------------------------------------------------------------------------
;----------------------------------------------------------------------------------------

;---INSPO DIVISION - H3 Version - Part 01

; Place cursor at desired placement within note on right
; Add separator below text fragment within note on left
; Before triggering script, select separator up to `Fragment - Name`


;- - - <HOTKEY DEFINITION {Alt 3}>
!3::

Sleep, 100

;- - - <COPY SELECTED TEXT SELECTION (WILL BE AT DITTO POSITION 3)>
Send, ^c

Sleep, 1000

;- - - <GO TO BEGINNING OF TOP LINE OF SELECTED TEXT>
Send, {Left}

Sleep, 100

;- - - <SELECT TOP LINE OF PREVIOUSLY SELECTED TEXT VIA SELECT BLOCK HOTKEY (TEXT TRANSPORTER PLUGIN)>
Send, !^+p

Sleep, 100

;- - - <COPY TOP LINE (FRAGMENT NAME) SELECTION (WILL BE AT DITTO POSITION 2)>
Send, ^c

Sleep, 100

;- - - <GO TO BEGINNING OF SELECTED TEXT>
Send, {Left}

Sleep, 100

;- - - <ADD 2 NEW LINES>
Send, {Enter}
Send, {Enter}

Sleep, 100

;- - - <GO UP TWO LINES>
Send, {Up}
Send, {Up}

Sleep, 100

;- - - <ADD SEPARATOR>
Send, - - - 

Sleep, 100

;- - - <NEW LINE>
Send, {Return}

Sleep, 100

;- - - <END BULLET MODE HOTKEY - (BULLETS KEPT FROM RETURNING NEW LINE FROM SEPARATOR {ALT SHIFT -}>
Send, !+-

Sleep, 100

;- - - <NEW LINE>
Send, {Return}

Sleep, 100

;- - - <ADD `FRAGMENT`>
Send, FRAGMENT

Sleep, 100

;- - - <SELECT BLOCK USING SELECT BLOCK HOTKEY (TEXT TRANSPORTER PLUGIN)>
Send, !^+p

Sleep, 100

;- - - <TOGGLE HIGHLIGHT HOTKEY>
Send, ^l

Sleep, 100

;- - - <GO TO END OF SELECTION>
Send, {Right}

Sleep, 100

;- - - <PLACE CURSOR OUTSIDE OF `==` FROM THE HIGHLIGHT FORMATTING>
Send, {Right}
Send, {Right}

Sleep, 100

;- - - <ADD SPACE>
Send, {Space}

Sleep, 3000

;- - - <COPY BLOCK LINK (CREATES A BLOCK ID AND COPIES LINK TO DITTO POSITION 1)> 
Send, ^y

Sleep, 100

;- - - <FOCUS RIGHT>
Send, !+{Right}

Sleep, 100

;- - - <H4 HEADING PREP. - {#### }>
Send, +3
Send, +3
Send, +3
Send, +3
Send, {Space}

Sleep, 100

;- - - <PASTE COPIED TEXT TITLE FROM DITTO POSITION 2 {CTRL SHIFT 8}>
Send, ^+8

Sleep, 100

;- - - <2 NEW LINES>
Send, {Return}
Send, {Return}

Sleep, 100

;- - - <H5 `UP` HEADING>
Send, +3
Send, +3
Send, +3
Send, +3
Send, +3
Send, {Space}
Send, UP

Sleep, 100

;- - - <3 NEW LINES>
Send, {Enter}
Send, {Enter}
Send, {Enter}

Sleep, 100

;- - - <UP ONE LINE>
Send, {Up}

Sleep, 100

;- - - <ADD EMPTY BULLET `- `>
Send, -
Send, {Space}

Sleep, 100

;- - - <DOWN ONE LINE>
Send, {Down}

Sleep, 100

;- - - <NEW LINE>
Send, {Enter}

Sleep, 100

;- - - <ADD SEPARATOR>
Send, - - - 

Sleep, 100

;- - - <NEW LINE>
Send, {Return}

Sleep, 100

;- - - <TOGGLE BULLET MODE HOTKEY {ALT SHIFT -}>
Send, !+-

Sleep, 100

;- - - <H5 `DOWN` HEADING>
Send, +3
Send, +3
Send, +3
Send, +3
Send, +3
Send, {Space}
Send, DOWN

Sleep, 100

;- - - <2 NEW LINES>
Send, {Enter}
Send, {Enter}

Sleep, 100

;- - - <ADD SOURCE FIELD - `Source:: `>
Send, Source
Send, +;
Send, +;
Send, {Space}

Sleep, 100

;- - - <PASTE BLOCK LINK VIA DITTO POSITION 3 HOTKEY {CTRL SHIFT 7}>
Send, ^+7

Sleep, 1000

;- - - <2 NEW LINES>
Send, {Return}
Send, {Return}

Sleep, 100

;- - - <PASTE COPIED TEXT USING DITTO POSITION 1 HOTKEY {CTRL SHIFT 9}>
Send, ^+9

Sleep, 100

;- - - <FOCUS LEFT> 
Send, !+{Left}

Return

Thanks for any suggestions moving forward!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.