Help! Copy text selected and its URL and paste into a specific format

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ManuelesAdrian
Posts: 6
Joined: 29 Apr 2024, 00:47

Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 01:02

Idk how to copy a text selected and its URL from a web page and the paste both of them at the same time. I want to use a copy shortkey (alt+c) for a text selected and then paste them using another shortkey (alt+v). I want to get this format: "(block'url)[text selected]". My purpose is copy text from the web and then paste it wiki format into Obsidian program.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 05:27

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v2.0
urlAndText := ''

!v::SendText urlAndText

#HotIf WinActive('ahk_exe chrome.exe')
!c:: {
 A_Clipboard := ''
 Send '^c'
 If ClipWait(1) {
  Global urlAndText := "(block'" getUrl() ')[' Trim(A_Clipboard, ' `t`r`n') ']'
  SoundBeep 1500
 } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 'Icon!'
}
#HotIf

getUrl() {  ; Get URL of the active Web browser window
 ; Adapted: https://gist.github.com/7cce378c9dfdaf733cb3ca6df345b140
 Static TreeScope_Descendants     := 4
      , UIA_ControlTypePropertyId := 30003
      , UIA_DocumentControlTypeId := 50030
      , UIA_EditControlTypeId     := 50004
      , UIA_ValueValuePropertyId  := 30045
 IUIAutomation := ComObject('{FF48DBA4-60EF-4201-AA87-54103EEF594E}'
                          , '{30CBE57D-D9D0-452A-AB13-7AC5AC4825EE}')
 eRoot         := ComValue(13, 0)
 If HRESULT    := ComCall(6, IUIAutomation, 'Ptr', WinGetID('A'), 'Ptr*', eRoot)
  Throw Error('IUIAutomation::ElementFromHandle()', -1, HRESULT)
 ctrlTypeId    := WinGetClass('A') ~= 'Chrome' ? UIA_DocumentControlTypeId : UIA_EditControlTypeId
 value         := Buffer(8 + 2 * A_PtrSize, 0)
 NumPut('UShort', 3, value, 0), NumPut('Ptr', ctrlTypeId, value, 8)
 condition     := ComValue(13, 0)
 If HRESULT    := A_PtrSize = 8
  ? ComCall(23, IUIAutomation, 'UInt', UIA_ControlTypePropertyId, 'Ptr', value, 'Ptr*', condition)
  : ComCall(23, IUIAutomation
     , 'UInt'  , UIA_ControlTypePropertyId
     , 'UInt64', NumGet(value, 0, 'UInt64')
     , 'UInt64', NumGet(value, 8, 'UInt64')
     , 'Ptr*'  , condition
    )
  Throw Error('IUIAutomation::CreatePropertyCondition()', -1, HRESULT)
 eFirst := ComValue(13, 0)
 If HRESULT := ComCall(5, eRoot, 'UInt', TreeScope_Descendants, 'Ptr', condition, 'Ptr*', eFirst)
  Throw Error('IUIAutomationElement::GetRootElement()', -1, HRESULT)
 propertyValue := Buffer(8 + 2 * A_PtrSize)
 If HRESULT := ComCall(10, eFirst, 'UInt', UIA_ValueValuePropertyId, 'Ptr', propertyValue)
  Throw Error('IUIAutomationElement::GetCurrentPropertyValue()', -1, HRESULT)
 ObjRelease(eFirst.Ptr), ObjRelease(eRoot.Ptr)
 Try {
  pProperty := NumGet(propertyValue, 8, 'Ptr')
  Return StrGet(pProperty, 'UTF-16')
 }
}
ManuelesAdrian
Posts: 6
Joined: 29 Apr 2024, 00:47

Re: Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 10:56

I am very grateful for your assistance. The code works very well. However, I owe you an apology as I did not manage to explain myself properly regarding what I wanted. When I referred to copying the URL of the selected text, I did not mean the URL of the entire webpage, but rather specifically the URL of the block of text I selected. Thus, when I click on that URL, it should direct me to the position of the selected text section on the webpage. Finally, I was wondering if you could assist me with changing the syntax so that it appears as follows (considering that what is in bold are the variables that will change according to what is copied):
selected text [url](URL of the selected text, not of the webpage).

This would be an example:
In July and August 2002, Radiohead toured Portugal and Spain, playing a number of new songs [url](https://en.wikipedia.org/wiki/Radiohead ... ew%20songs.)
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 11:04

Your goal remains unclear. It sounds like you would like to create a new URL, not the original one, that navigates to a specific area of text on a live Web page. I am not able to accomplish that through a new URL. Others may know the way.
ManuelesAdrian
Posts: 6
Joined: 29 Apr 2024, 00:47

Re: Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 11:20

I don't believe it's about creating a new URL. For example, in Chrome, when you right-click after selecting the text, a dropdown menu appears, where there is an option to copy the URL of the selected text. That URL is what I am looking for. I attempted to use mouse commands to right-click and then choose that option from the dropdown menu, but afterward, I couldn't proceed further.
This is what i mean:
image.png
image.png (38.12 KiB) Viewed 507 times
In my case, the option to copy the URL of the selected text is in the second position.
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 11:43

If you mean that you want to get the URL from an HTML hyperlink that shows other text instead of the URL, I believe that some other posts have addressed this. I have not tried to do it.
ManuelesAdrian
Posts: 6
Joined: 29 Apr 2024, 00:47

Re: Help! Copy text selected and its URL and paste into a specific format

29 Apr 2024, 12:38

I understand. I am very grateful for your assistance. Regards.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: DavidP and 80 guests