<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

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

Published on
4,575 Points
675 Views
4 Endorsements
Last Modified:
Joe Winograd, EE MVE 2015&2016
50+ yrs in computer industry. Everything from programming to sales. OS kernel dev on mainframes. CIO. Document imaging. EE MVE 2015 & 2016.
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

4
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Andrew Leniart
Great article for a total newb to programming like me Joe.

Surprisingly, I found myself able to actually understand your examples! lol

Am I correct in concluding that in order to utilize the above sample code, that one would first need to install AutoHotkey per your link and then enter the code into that application?  If so, then the educational flow of this article is brilliant :)
0
 
LVL 55

Author Comment

by:Joe Winograd, EE MVE 2015&2016
Hi Andrew,
Thanks for the kind words — very nice to hear! I'm glad that you were able to understand my examples.

Yes, you are correct that in order to utilize the source code, you need to install AutoHotkey. But you don't "enter the code into that application" — you enter the code into a plain text editor, such as Notepad or Notepad++ or whatever your favorite text editor is, and then save the file with a file extension of AHK (not TXT). At that point, if you've done a standard installation of AutoHotkey, double-clicking on the source code file (the .ahk file) in Windows/File Explorer (or whatever file manager you use) will execute the script.

You may also run scripts on computers without AutoHotkey installed by compiling them into stand-alone executables, i.e., EXE files (this is explained in more detail in my EE AutoHotkey article). In fact, I've been doing most of my custom software development in recent years by coding the programs in AutoHotkey, compiling them into EXE files, and creating a standard Windows installer/uninstaller — a Setup.exe file — using the Nullsoft Scriptable Install System (NSIS). For example, that's what I did as the solution to this EE Gig:
Rename PDF files based on content

Thanks again for your feedback — much appreciated! Regards, Joe
2
 
LVL 12

Expert Comment

by:Andrew Leniart
Thank you for the clarification Joe.
I'm glad that you were able to understand my examples.
It's actually quite an accomplishment on you part! I guess you could say I'm "programmatically challenged". Whenever I try to understand sample code, all those commands and squiggly } lines start to make the whole page go blurry on me lol..  Your comments beside the commands make it very easy to follow indeed.  Will definitely be checking out your other AutoHotkey articles so thanks for the links.

Best..
0
 
LVL 55

Author Comment

by:Joe Winograd, EE MVE 2015&2016
Andrew,
Thanks again for the comments. It's extremely helpful for authors to get feedback like that from readers. Regards, Joe
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month