Text with embedded hyperlink Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Craash
Posts: 6
Joined: 14 Mar 2023, 15:22

Text with embedded hyperlink

Post by Craash » 18 Mar 2023, 10:02

I've searched the documentation and the forum, but I can't seem to find a way to create Text with an embedded hyperlink.

For example, I would like my output to be the below:

"Some Text" as a clickable hyperlink. The hyperlink won't display, just "some text". I'm new to v2, so please be kind. :)

I appreciate your time and knowledge.

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Text with embedded hyperlink

Post by mikeyww » 18 Mar 2023, 10:45

Welcome to this AutoHotkey forum!

Code: Select all

; This script copies HTML to the clipboard
#Requires AutoHotkey v2.0
#Include d:\utils\WinClipV2\WinClip.ahk     ; https://github.com/TheArkive/WinClip_ahk2
#Include d:\utils\WinClipV2\WinClipAPI.ahk
wc := WinClip()
wc.Clear()
wc.SetHTML('<a href="http://www.autohotkey.com/">Some text</a>')

Craash
Posts: 6
Joined: 14 Mar 2023, 15:22

Re: Text with embedded hyperlink

Post by Craash » 18 Mar 2023, 10:53

Thank you for your response and warm welcome.

This might go beyond my needs. I just simply want to type the hotkey name and end up the the "Text" with embedded hyperlink (which will never change) in a email. Is this still the best way?

Maybe I should explain more. If I was to type something like "cal" I'd like to end up with "Click HERE to schedule time on my calendar". I'm trying to automate a work email that has my scheduling link in the signature.

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Text with embedded hyperlink

Post by mikeyww » 18 Mar 2023, 13:47

Code: Select all

; This script copies HTML to the clipboard
#Requires AutoHotkey v2.0
#Include d:\utils\WinClipV2\WinClip.ahk     ; https://github.com/TheArkive/WinClip_ahk2
#Include d:\utils\WinClipV2\WinClipAPI.ahk
url := 'http://www.autohotkey.com/'

::cal:: {
 wc := WinClip(), wc.Clear()
 wc.SetHTML('<a href="' url '">Click HERE to schedule time on my calendar</a>')
 ClipWait(1, True) ? Send('^v') : MsgBox('An error occurred while waiting for the clipboard.', 'Error', 48)
}
There could be better or worse ways. You can be the judge!

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: Text with embedded hyperlink

Post by teadrinker » 18 Mar 2023, 16:03

Why not the link control?

Craash
Posts: 6
Joined: 14 Mar 2023, 15:22

Re: Text with embedded hyperlink

Post by Craash » 18 Mar 2023, 16:19

teadrinker wrote:
18 Mar 2023, 16:03
Why not the link control?
Sounds perfect, can you expand? Maybe some complete code?

Craash
Posts: 6
Joined: 14 Mar 2023, 15:22

Re: Text with embedded hyperlink

Post by Craash » 18 Mar 2023, 21:43

mikeyww wrote:
18 Mar 2023, 13:47

Code: Select all

; This script copies HTML to the clipboard
#Requires AutoHotkey v2.0
#Include d:\utils\WinClipV2\WinClip.ahk     ; https://github.com/TheArkive/WinClip_ahk2
#Include d:\utils\WinClipV2\WinClipAPI.ahk
url := 'http://www.autohotkey.com/'

::cal:: {
 wc := WinClip(), wc.Clear()
 wc.SetHTML('<a href="' url '">Click HERE to schedule time on my calendar</a>')
 ClipWait(1, True) ? Send('^v') : MsgBox('An error occurred while waiting for the clipboard.', 'Error', 48)
}
There could be better or worse ways. You can be the judge!
I'm seeing this error when trying to use the WinClip?
image.png
image.png (121.15 KiB) Viewed 5368 times

User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: Text with embedded hyperlink

Post by boiler » 18 Mar 2023, 23:29

Did you save the html source of the github page that contains the AHK code as WinClip.ahk rather than the AHK code itself? Can you paste the contents of your WinClip.ahk file here? Put it between [code][/code] tags.

User avatar
Datapoint
Posts: 296
Joined: 18 Mar 2018, 17:06

Re: Text with embedded hyperlink

Post by Datapoint » 18 Mar 2023, 23:49

Craash wrote:
18 Mar 2023, 16:19
Sounds perfect, can you expand? Maybe some complete code?
https://www.autohotkey.com/docs/v2/lib/GuiControls.htm#Link

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Text with embedded hyperlink  Topic is solved

Post by mikeyww » 19 Mar 2023, 06:22

This idea from a recent post requires no external files.

Code: Select all

; This script copies HTML to the clipboard (alternative to WinClip)
#Requires AutoHotkey v2.0
url := 'http://www.autohotkey.com/'

#HotIf WinActive('ahk_exe OUTLOOK.exe')
::cal:: {  ; Paste HTML into Outlook message
 A_Clipboard := ''
 HTMLtoClipboard('<a href="' url '">Click HERE to schedule time on my calendar</a>')
 If ClipWait(1, True) {
  wEditor := ComObjActive('Outlook.Application').ActiveInspector.WordEditor
  wEditor.Application.Selection.Paste
 } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 48
}
#HotIf

HTMLtoClipboard(html) {
 ; Idea from https://www.autohotkey.com/boards/viewtopic.php?p=513124#p513124
 htmlFile  := ComObject('HTMLfile')
 htmlFile.write(html)
 bodyRange := htmlFile.body.createTextRange()
 bodyRange.select(), bodyRange.execCommand('Copy')
 bodyRange := htmlFile := ''
}

Craash
Posts: 6
Joined: 14 Mar 2023, 15:22

Re: Text with embedded hyperlink

Post by Craash » 19 Mar 2023, 10:32

mikeyww wrote:
19 Mar 2023, 06:22
This idea from a recent post requires no external files.
Thank you! I'll try this today or tomorrow morning!

Craash
Posts: 6
Joined: 14 Mar 2023, 15:22

Re: Text with embedded hyperlink

Post by Craash » 20 Mar 2023, 08:22

@mikeyww, that second piece of code does it! Thank you so much!

User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: Text with embedded hyperlink

Post by JoeSchmoe » 20 Mar 2023, 10:46

mikeyww wrote:
19 Mar 2023, 06:22

Code: Select all

 HTMLtoClipboard('<a href="' url '">Click HERE to schedule time on my calendar</a>')
 If !ClipWait(1, True) 
 	MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 48
}
Thanks for that example, @mikeyww. I'd like to learn more about why it's necessary to use ClipWait after HTMLtoClipboard. In a single-threaded script, is this necessary? Won't HTMLtoClipboard not finish until the HTMLFile Comobject has completed it's action? Are you just being extra cautious because it's easier to just add the ClipWait code than check whether it's necessary?

I ask because I've been using WinClip to put HTML into the clipboard and I've been having some issues. I wonder if I should be adding ClipWait. At the same time, I love simple and blazingly fast code, so I don't want to add anything extra. I'll likely try replacing my WinClip code with this code, so I want to know how to optimize my usage of both HTMLtoClipboard and WinClip (in terms of whether I need to add "ClipWait").

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Text with embedded hyperlink

Post by mikeyww » 20 Mar 2023, 10:50

I use ClipWait because the Windows clipboard can be slow. I do not know whether execCommand('Copy') has a built-in wait; you can try it and see. I have assumed that it simply executes a copy action without any additional action. In any case, ClipWait waits only until the clipboard is populated or the timeout occurs, so if the clipboard is populated immediately, there is no wait.
I wonder if I should be adding ClipWait.
The question can be answered quickly. Try it, and see if the problem disappears.

The effect of not using ClipWait when it matters is that immediately accessing the clipboard may use the clipboard's previous contents, which would be null in this case, or whatever the previous contents are, if the clipboard is not cleared before setting it again.

User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: Text with embedded hyperlink

Post by JoeSchmoe » 20 Mar 2023, 10:57

Thanks! I've had intermittent problems, and they've been hard to debug, because I don't know which part of my toolchain is failing (or if there's a bug elsewhere in my own codebase).

I'll start using Clipwait for all clipboard operations just in case.

User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: Text with embedded hyperlink

Post by mikeyww » 20 Mar 2023, 11:14

You can also learn the value of the clipboard by displaying it after you set it!

You can quickly see different results by clipping something in Microsoft Excel.

Code: Select all

#Requires AutoHotkey v2.0
^F3:: {
 A_Clipboard := '', Send('^c'), noWait := A_Clipboard
 ClipWait(1)
 MsgBox '#' noWait '#`n`n#' A_Clipboard '#', 'Result', 64
}
image230320-1224-001_cr.png
Output
image230320-1224-001_cr.png (12.1 KiB) Viewed 5223 times

When I tested with execCommand, I did not see this problem, but I do not have a way to test across different CPUs & conditions.

Post Reply

Return to “Ask for Help (v2)”