How to copy the short link of an ID at Experts Exchange to the clipboard with a single keystroke

Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
CERTIFIED GOLD 2020
Published:
Updated:
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a technique for doing this with a single keystroke.

Each comment in Question threads here at Experts Exchange has a unique Identifier (ID). The full path of the link for an ID looks like this:


https://www.experts-exchange.com/questions/12345678/Title-of-this-question.html#a87654321


It is easy to copy that full path to the clipboard by right-clicking on it and then selecting the appropriate context menu item in your browser. For example, in Chrome, Opera, and Vivaldi, the menu item is Copy link address; in Firefox, it is Copy Link Location; in IE11, Copy shortcut. However, we often want to post a short link within a thread, rather than the full link. The short link is everything after and including the number symbol (aka hash, octothorpe, pound sign), such as:


#a87654321


An EE member recently posted a bug report pointing out that it is difficult to get the short link. He noted that a cumbersome, manual technique is to copy non-clickable text surrounding and including it, cleaning it up, and adding the pound sign, thereby creating the short link. Another technique, also manual and cumbersome, is to copy the full path, then delete everything before the short link. This article shows an easy, automated method for obtaining the short link with a single keystroke.


The primary component that we'll need for the solution is a scripting language that supports the Windows clipboard, and sending mouse clicks and keystrokes. In this article, I'll explain how to do it with AutoHotkey, my favorite programming/scripting language these days — but pick whatever language with which you're comfortable. If you'd like to learn about AutoHotkey, this EE article will get you going:


AutoHotkey - Getting Started


The method offered here is to position the mouse on the ID of interest and then tap a hotkey, which can be any key or key combination that you want, such as F5, NumPad+, Alt-Ctrl-I, whatever. Here's the AutoHotkey script for use with Firefox, containing ample comments so that it's easy to understand its operation:


F5::                               ; use F5 as hotkey

Clipboard:=""                      ; empty the clipboard

MouseClick,Right                   ; right-click at current mouse location

Sleep,60                           ; wait for 60 milliseconds to make sure context menu is there

SendInput a                        ; send letter "a", the Firefox shortcut for Copy Link Location

ClipWait,2                         ; wait at most two seconds for short link to appear in clipboard

If (ErrorLevel=1)                  ; ErrorLevel set to 1 when wait period expires

{

  MsgBox,4112,Error,Attempt to copy the short link onto the clipboard failed.

  Return ; 4112 above means put dialog on top and give it the red X (error/stop) icon

}

StringSplit,ShortLink,Clipboard,#  ; split long link into two strings, separated at pound sign

ShortLink:="#" . ShortLink2        ; add pound sign to second string, creating the short link

Clipboard:=ShortLink               ; put short link on clipboard

Return                             ; success


As you can see in the code above, if the short link does not appear on the clipboard within two seconds, the script displays this error dialog:


The "SendInput a" in the script works because the Firefox keyboard shortcut for Copy Link Location is "a". However, in Chrome, the keyboard shortcut for Copy Link Address is "e", so that line in the script should be changed to this:

 

SendInput e                        ; send letter "e", the Chrome shortcut for Copy Link Address


If the browser that you are using doesn't have a keyboard shortcut for the menu pick that copies the link to the clipboard (such as "a" and "e" in the examples above), then you can navigate through the context menu with the down arrow as many times as needed to get to that item, and then send the Enter key. For example, in Chrome, it takes five down-arrows to get to the Copy Link Address menu pick. So you would replace "SendInput e" in the script with this:

 

SendInput {Down 5}{Enter}          ; send five down arrows then the Enter key


Many users stick to a single browser, but if you use multiple browsers, an enhanced version of the script will support them. For example, here's a script that detects whether Chrome, Firefox, IE, or Opera is running and sends the correct keystroke(s) for each:

F5:: WinGetTitle,ActiveTitle,A                 ; get title of active window IfInString,ActiveTitle,Mozilla Firefox    ; check if active window is Firefox   ShortcutKey:="a"                        ; send letter "a", Firefox shortcut for Copy Link Location Else                                      ; it is not Firefox IfInString,ActiveTitle,Google Chrome      ; check if active window is Chrome   ShortcutKey:="e"                        ; send letter "e", Chrome shortcut for Copy Link Address Else                                      ; it is not Chrome IfInString,ActiveTitle,Internet Explorer  ; check if active window is IE   ShortcutKey:="t{Enter}"                 ; send letter "t", IE shortcut for Copy Shortcut, but also needs Enter Else                                      ; it is not IE IfInString,ActiveTitle,Opera              ; check if active window is Opera   ShortcutKey:="e"                        ; send letter "e", Opera shortcut for Copy Link Address Else                                      ; it is not Opera {   MsgBox,4112,Error,Active window is not a supported browser.   Return ; 4112 above means put dialog on top and give it the red X (error/stop) icon } Clipboard:=""                             ; empty the clipboard MouseClick,Right                          ; right-click at current mouse location Sleep,60                                  ; wait for 60 milliseconds to make sure context menu is there SendInput,%ShortcutKey%                   ; send shortcut to copy link ClipWait,2                                ; wait at most 2 seconds for short link to appear in clipboard If (ErrorLevel=1)                         ; ErrorLevel set to 1 when wait period expires {   MsgBox,4112,Error,Attempt to copy the short link onto the clipboard failed.   Return ; 4112 above means put dialog on top and give it the red X (error/stop) icon } StringSplit,ShortLink,Clipboard,#         ; split long link into two strings, separated at pound sign ShortLink:="#" . ShortLink2               ; add back pound sign to short link Clipboard:=ShortLink                      ; put short link on clipboard Return                                    ; success


This script works by looking at the title bar of the active window to see if it contains Mozilla Firefox, Google Chrome, Internet Explorer, or Opera. If it doesn't find one of those, it displays this error dialog:



There are two issues with the approach used in this script. First, a window title could contain one of those strings even if that browser is not running, e.g., if you are viewing a file called Mozilla Firefox ReadMe.txt in Notepad. Second, some browsers do not put the browser name in the window title, such as Vivaldi. To solve those issues, the script could use the ahk_exe parameter on the WinGetTitle statement, which identifies a window belonging to any process with the specified name or path, such as chrome.exe, firefox.exe, iexplore.exe, opera.exe, vivaldi.exe. I'll leave the creation of such a script to motivated readers. :)


In conclusion, after positioning the mouse on an EE comment ID and hitting the hotkey that you have defined, you will have the short link on the clipboard — simply do a Ctrl-V/Paste to put it wherever you want!


If you find this article to be helpful, please click the thumbs-up icon below. This lets me know what is valuable for EE members and provides direction for future articles. Thanks very much! Regards, Joe

5
2,712 Views
Joe WinogradDeveloper
CERTIFIED EXPERT
50+ years in computers
Development•Sales
CIO•Document Imaging
EE — FELLOW 2017
MVE 2015,2016,2018
RENOWNED 2018,2019
CERTIFIED GOLD 2020

Comments (8)

Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Andrew,
Thanks again for the comments. It's extremely helpful for authors to get feedback like that from readers. Regards, Joe
Luis DiazIT consultant

Commented:
Hi Joe,

This script is very useful a big thank you!
Knowing that one of final objective is to get the id of the comment and post it to a EE comment, I was wondering why not reinforce the script to have with two capabilities:

1.Copy just the ID (as is)
2.Copy the full link as expected to be posted for an EE comment

Example:
Case 1: a43019059
Case 2:#a43019059

If you want I can create a question for this.

Regards,
Luis.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
You're welcome, Luis, and my thanks back at you for the compliment and the article Endorsement...both appreciated!

No need to create a question...happy to answer it here. The script already does what you want in the sense that the ID without the hash symbol (pound sign) is already in the Shortlink2 variable after the StringSplit command. In fact, the code actually has to add it in, which is what the next line does (via concatenation):

ShortLink:="#" . ShortLink2 ; add back pound sign to short link

Open in new window

So, at the end of the code, prior to putting it on the clipboard, there are two variables with what you want:

Case 1 - just the ID - Shortlink2
Case 2 - link for posting - Shortlink

At that point, you may do with them whatever you want. You could, for example, create two hotkeys: one for Case 1, which puts Shortlink2 on the clipboard; one for Case 2, which puts Shortlink on the clipboard. Another idea is to ask the user what to do via a MsgBox dialog, but that would detract from the "single keystroke" objective.

I hope that helps. Regards, Joe
Luis DiazIT consultant

Commented:
Hi Joe,

Thank you very much for this useful comment: I reviewed as following to get the exact format required for EE comment:

^!d::
WinGetTitle,ActiveTitle,A                 ; get title of active window
IfInString,ActiveTitle,Mozilla Firefox    ; check if active window is Firefox
  ShortcutKey:="a"                        ; send letter "a", Firefox shortcut for Copy Link Location
Else                                      ; it is not Firefox
IfInString,ActiveTitle,Google Chrome      ; check if active window is Chrome
  ShortcutKey:="e"                        ; send letter "e", Chrome shortcut for Copy Link Address
Else                                      ; it is not Chrome
IfInString,ActiveTitle,Internet Explorer  ; check if active window is IE
  ShortcutKey:="t{Enter}"                 ; send letter "t", IE shortcut for Copy Shortcut, but also needs Enter
Else                                      ; it is not IE
IfInString,ActiveTitle,Opera              ; check if active window is Opera
  ShortcutKey:="e"                        ; send letter "e", Opera shortcut for Copy Link Address
Else                                      ; it is not Opera
{
  MsgBox,4112,Error,Active window is not a supported browser.
  Return ; 4112 above means put dialog on top and give it the red X (error/stop) icon
}
Clipboard:=""                             ; empty the clipboard
MouseClick,Right                          ; right-click at current mouse location
Sleep,60                                  ; wait for 60 milliseconds to make sure context menu is there
SendInput,%ShortcutKey%                   ; send shortcut to copy link
ClipWait,2                                ; wait at most 2 seconds for short link to appear in clipboard
If (ErrorLevel=1)                         ; ErrorLevel set to 1 when wait period expires
{
  MsgBox,4112,Error,Attempt to copy the short link onto the clipboard failed.
  Return ; 4112 above means put dialog on top and give it the red X (error/stop) icon
}
StringSplit,ShortLink,Clipboard,#         ; split long link into two strings, separated at pound sign
ShortLink:="#" . ShortLink2               ; add back pound sign to short link
;~ Clipboard:=ShortLink2                     ; put short link on clipboard
ClipboardVar:="[url=" . quote . Clipboard . quote . "]" . ShortLink . "[/url]"
Clipboard:=ClipboardVar
Return

Open in new window



I am just having a question, If I want to get ride of the not necessary part of the link to get just question ID and comment ID how can I proceed?

20200130_200648.png
Thank you in advance for your feedback.
Joe WinogradDeveloper
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018

Author

Commented:
Hi Luis,
Now you're beyond the article's objective and at this point I do think that creating a question is appropriate. The code in the article already generates both a12345678 (Shortlink2) and #a12345678 (Shortlink), which addressed your first question. Doing what you want in the second question is a different requirement. Regards, Joe

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
Continue Growing Your Skills and Your Career
  • Interact with leading experts on your specific technology problems.
  • Receive the guidance of experienced professionals.
  • Learn from troubleshooting others have experienced.
  • Gain knowledge from a library of courses, all included.