Simple tool for putting [URL] tags in AHK Forum posts..

Post your working scripts, libraries and tools.
User avatar
kunkel321
Posts: 1194
Joined: 30 Nov 2015, 21:19

Simple tool for putting [URL] tags in AHK Forum posts..

Post by kunkel321 » 18 May 2024, 08:20

Super simple, but I thought I'd share. This was inspired by the discussion with @Andymbody, in the post here.

EDIT: Might as well add support for ahk code blocks too...
EDIT 3: The chr(93) allows us to share the below code block, but also will paste the tag appropriately. Cool tip Andy!

Code: Select all

;===============================================================================
; https://www.autohotkey.com/boards/viewtopic.php?f=83&t=129926
; Put url on clipboard, then select some text and press key to embed link in text. 
; If code block, imgur, or username, just put on clipboard, then press key. 
#HotIf WinActive("AutoHotkey Community")
^k:: 
{	If InStr(A_Clipboard, "#Requires AutoHotkey") || InStr(A_Clipboard, "::")
	{	A_Clipboard := "[Codebox=autohotkey file=Untitled.ahk]" A_Clipboard "[/Codebox" chr(93)
	}
	Else If InStr(A_Clipboard, "imgur") ||  InStr(A_Clipboard, "screencast") ||  InStr(A_Clipboard, "prnt")
	{	A_Clipboard := "[Img]" A_Clipboard "[/Img]"
	}
	Else If RegExMatch(A_Clipboard, "^(https?://|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$")
	{	holdClip := A_Clipboard
		A_Clipboard := ''
		Send "^c"
		Sleep 100
		A_Clipboard := "[url=" holdClip "]" A_Clipboard "[/url]"
	}
	Else 
	{	A_Clipboard := "[mention]" A_Clipboard "[/mention]"
	}
	Send "^v"
}
#HotIf
Rules/Tips/Comments
-Uses hotkey Ctrl+K, which is the one that MS Office apps use to insert a link.
-Hotkey is only active in AHK Forums.
-First, select
* a block of AHK code
* an imgur url
* a URL that is not imgur
* or a username
-Press Ctrl+C to copy to clipboard.
-Click in your forum post (that you are writing/editing)
-If it's a code block, an imgur URL, or a username, just press Ctrl+K
-If it's a non-imgur URL, then first select the text in your post that you want to embed the hyperlink in, and press Ctrl+K.
Last edited by kunkel321 on 18 May 2024, 11:41, edited 2 times in total.
ste(phen|ve) kunkel
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by andymbody » 18 May 2024, 09:24

Cool... Thank you... Very clever! I'm not sure what i.imgur is tho ?
Marium0505
Posts: 43
Joined: 11 May 2020, 20:45

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by Marium0505 » 18 May 2024, 09:28

@andymbody
A website (https://imgur.com/) used to upload and share screenshots/photos online, and same with Lightshot's https://prnt.sc/ and similar tools.

In other words: He checks if the url is to an image and then replaces the tag with the image/photo tag instead of the link tag.
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by andymbody » 18 May 2024, 09:36

Marium0505 wrote:
18 May 2024, 09:28
checks if the url is to an image and then replaces the tag with the image/photo tag instead of the link tag.
Ahh... Thank you!

Checking the clipboard contents to provide multiple uses for a single hotkey is brilliant @kunkel321! New toys! Just used it here... :D
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by andymbody » 18 May 2024, 10:43

kunkel321 wrote:
18 May 2024, 08:20
I have to break the code block tag in order to post the below code
Suggestion.. use chr() to avoid pasting code issues. Or create a var that holds the character instead.

A few personal tweaks of my own...

Code: Select all

#HotIf WinActive('AutoHotkey Community')
^k::
{
	If (InStr(A_Clipboard, '#Requires AutoHotkey') || InStr(A_Clipboard, '::'))
	{
		;A_Clipboard := ' [code]`n' A_Clipboard '`n[/code' chr(93)
		A_Clipboard := ' [Codebox=autohotkey file=Untitled.ahk]`n' A_Clipboard '`n[/Codebox' chr(93)
		Send('^v')
	}
	Else If (InStr(A_Clipboard, 'i.imgur'))
	{
		A_Clipboard	:= ' [Img]' A_Clipboard '[/Img]', Send('^v')
	}
	Else If (RegExMatch(A_Clipboard, '^(https?://|www\.).+$'))
	{
		holdUrl	:= A_Clipboard, A_Clipboard := '', Send('^c'), Sleep(100)
		text	:= A_Clipboard, A_Clipboard := ' [url=' holdUrl ']' text '[/url]', Send('^v')
		if (!text)					; if no text was selected...
			Send('{end}{left 6}')	; position cursor to add text manually
	}
	Else if (RegExMatch(A_Clipboard, '^([\w+-]+)( (?1))?$'))	; a little more specific for content
	{
		A_Clipboard := ' [mention]' A_Clipboard '[/mention]', Send('^v')
	}
	; add more elseifs
	; else do nothing
}
#HotIf
User avatar
kunkel321
Posts: 1194
Joined: 30 Nov 2015, 21:19

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by kunkel321 » 18 May 2024, 11:34

andymbody wrote:
18 May 2024, 10:43
Cool tip with embedding the chr(93) !! I've updated the top post. Thanks for the tip!
I added a couple more image hosting sites too. I guess those could be made into a single RegExMatch. Also the big regex for matching urls might be overkill... We don't really need to ensure that the url is valid, we just need to see if it is indeed a url. I think I found it in the ahk docs... I don't remember.
EDIT: I see you already cleaned up the url regex -- Excellent :)
ste(phen|ve) kunkel
User avatar
kunkel321
Posts: 1194
Joined: 30 Nov 2015, 21:19

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by kunkel321 » 18 May 2024, 11:39

Interesting side note: At first, I had the code "typing out" the result, but it would occasionally trigger the Windows Notification Pane on the right of the screen. That's why I changed it to save to the clipboard, then type/send ^v. Seems to have fixed the glitch.
ste(phen|ve) kunkel
User avatar
andymbody
Posts: 996
Joined: 02 Jul 2017, 23:47

Re: Simple tool for putting [URL] tags in AHK Forum posts..

Post by andymbody » 18 May 2024, 12:03

kunkel321 wrote:
18 May 2024, 11:39
Interesting side note: At first, I had the code "typing out" the result, but it would occasionally trigger the Windows Notification Pane on the right of the screen. That's why I changed it to save to the clipboard, then type/send ^v. Seems to have fixed the glitch.
I like your choice... the slow moving text is annoying anyway... reminds me too much of command line character speed many decades ago. Snappy output is better IMO (at the expense of a few extra code characters initially)!
Post Reply

Return to “Scripts and Functions (v2)”