Save Clipboard Snippets with Autohotkey

Autohotkey Clipboard History Program
In the first article of this series, we covered how to save the clipboard with a clip title and date/time. This involved a series of steps describing the process. Autohotkey is a simple macro programing language which canbe used to automate windows tasks. It is free and open source software and has powerful capabilities which may be utilized by non-programmers and programers alike. The original program is listed below with comments. If you would lke to know more about Autohotkey, go to the Autohotkey site at: www.autohokey.com.
; Script to collect clipboard data and display it with a timestamp.
; The script should be saved with a .ahk suffix in a text file.
#v::
clipboard=
MsgBox, Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:
ClipWait, 60
vTimeHour := Substr(A_Now,9,2) ; Start at 9th character, and get the next 2 depicting the hour
vTimeMinutes := Substr(A_Now,11,2) ; Start at 11th character and get the next 2 depicting the minutes
vYearVal := SubStr(A_Now, 1,4) ; Start at first character and get teh next 4 for the year
vMonthVal := SubStr(A_Now,5,2) ; Start at the 5th character and get the next 2 characters for the month
vDayVal := SubStr(A_Now,7,2) ; Start at the 7th character and get the next 2 for the day
vClipout := vMonthVal . “/” . vDayVal . “/” . vYearVal . " " . vTimeHour . “:” . vTimeMinutes . “`n`n============================`n`n” ; Add end newline here.
; The above demonstrates how variables are used in Autohotkey. Dynamic (as opposed to static, explained in later articles) variables are surrounded by “%”. Variables can be concatentaed with themself via the “.=” operator
; Finally, we add the clipboard contents and output via a message box:
vClipout := “Clipboard Contents @ " . vClipout . clipboard
MsgBox, %vClipout%
Return
When this code is run the output from a typical clipboard snippet would be like so (text snippet taken from the Bright Hub site):
The original program can now be expanded to store clipboards into a history file and store different clips based on date, for recall later.
Storing Clips in a File
In this article, some simple functions will be enhanced, and others will be added, so that the relevant clips are stored in a file in addition to being displayed. This means that clips will be available to copy and paste anywhere when needed, simply by opening up the file of saved clips and copying the information from there to a target document.
The first task is to initialize a file variable so that clips can be stopred in the file. This simply means setting a filename to be used later in the program for storing clips.
This is achieved by the following comment and command before other commands are processsed:
; New add a filename variable to save the information.
vFileName=clips.txt
The script now continues as before with:
clipboard=
to initialize the clipboard to null.
The next task is to ensure that the dialogue box displays when awaiting a clip to be collected from the clipboard by the user is always on top, so that it is constantly displayed. The user then clicks on the OK button to clear the dialogue. This achieved by changing the following command in the previous script:
MsgBox, Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:
To
MsgBox, 4096,,Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:
This ensures that the Msgtbox function always stays on top as the code, 4096 means stay on top. There are numerous other options available in this command, and other autohotkey commands. Generally, the options’ section of each command determines how it will behave according to a specific situation.
Another factor is the need to await clipboard contents before continuing. At present, the program expects clipboard data to be available, even if the user has clicked on the button on the OK dialogue. However, if the user has not actually collected clipboard data by using the “cut” or “paste” functions (control c, control-x) then no clipboard data will be present. It is required that in this case, the program should wait until data is available on the clipboard. This is achieved by the following command which is now added to the code:
ClipWait, 60
We can also add a dialogue to appear as soon as information appears on the clipboard, following the above “ClipWait” command. This is a MsgBox command with a timeout, requiring no user interaction as it will immediately disappear after three seconds due to the timeout value as follows:
MsgBox,,, Clipboard data collected!, 3
As we now plan to store the date and time into a file, rather than just output the information to the screen it makes sense to save the date time as a separate variable, instead of outputting all the information in one go. This way the date/time can be stored in the output file as well as displayed on the screen. This is achieved via the following variable assignment (assuming the time/values have been set):
vTimeDate := vMonthVal . “/” . vDayVal . “/” . vYearVal . " " . vTimeHour . “:” . vTimeMinutes
Finally, we record the information to a text file, in addition to displayed on the screen. The following command outputs the information into a file:
FileAppend, %vTimeDate%`n”%clipboard%"`n, %vFileName%
Clipboard Saver Usage
The method of using the clip saver is very simple. Each clip is saved into the file by the time and date displayed and the contents surrounded by " (speech marks). The process involves opening the “clips.txt” file produced using your favorite editor and any saved clips can be viewed and extracted in the normal way by using copy and paste into another document. This is a simple but effective clip board collector. If the text file starts to become too full, simply delete out the old clips. It is easy to search for clips by time and date as they are inserted in chronological order.
A typical set of clips would look like the following example data in the clips.txt file:
07/17/2011 21:06
“script to do something with clipboard …
store the date”
07/17/2011 21:06
“Write script to do something with clipboard …
store the date, clip into a file and recall when needed.”
07/17/2011 21:20
“Also make the dialogue box stay on top”
Any data collected on the clipboard using this utility will display the date/time it was collected and the information copied to the clipboard.
Complete Autohotkey Code Version 2: Clipboard History Collector
; Script to collect clipboard data and display it with a timestamp.
; The script should be saved with a .ahk suffix in a text file.
#v::
; New add a filename variable to save the information.
vFileName=clips.txt
clipboard=
MsgBox, 4096,,Please select and copy data using the mouse and “control-c” and press “Ok” on this dialogue to continue:
ClipWait, 60
MsgBox,,, Clipboard data collected!, 3
vTimeHour := Substr(A_Now,9,2) ; Start at 9th character, and get the next 2 depicting the hour
vTimeMinutes := Substr(A_Now,11,2) ; Start at 11th character and get the next 2 depicting the minutes
vYearVal := SubStr(A_Now, 1,4) ; Start at first character and get the next 4 for the year
vMonthVal := SubStr(A_Now,5,2) ; Start at the 5th character and get the next 2 characters for the month
vDayVal := SubStr(A_Now,7,2) ; Start at the 7th character and get the next 2 for the day
; Save a time/date variable
vTimeDate := vMonthVal . “/” . vDayVal . “/” . vYearVal . " " . vTimeHour . “:” . vTimeMinutes
vClipout := vTimeDate . “`n`n============================`n`n” ; Add end newline here.
; The above demonstrates how variables are used in Autohotkey. Dynamic (as opposed to static, explained in later articles) variables are surrounded by “%”. Variables can be concatentaed with themself via the “.=” operator
; Finally, we add the clipboard contents and output via a message box and output the clipboard information into a text file.
FileAppend, %vTimeDate%`n"%clipboard%"`n, %vFileName%
vClipout := “Clipboard Contents @ " . vClipout . clipboard
MsgBox, %vClipout%
Return
References
Autohotkey: https://www.autohotkey.com
Screenshot provided by author.
This post is part of the series: Autohotkey Tutorials
In this series of tutorials, we look at the powerful and time saving utility Autohotkey, a free and open source package available for download. Using the tool it is possible fo non-programmers or people with a basic programming knowledge to quickly and easily automate windows tasks.