ClipArray (multiple clipboards w/ added functionality)

Post your working scripts, libraries and tools for AHK v1.1 and older
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

ClipArray (multiple clipboards w/ added functionality)

07 Sep 2020, 16:41

Hello! I thought I would post a script I made that emulates having a virtual clipboard with extra functionality such as:
Ability to store multiple copied entries in an array and paste them whenever you like, as many times as needed.
Ability to split up a copied selection into individual parts to be pasted based on tabs/newlines/cells (from Excel).
Ability to look through every entry that was copied and select the one to be pasted.
Ability to run code directly after pasting from a selection, such as code to sleep a certain amount of time and then tab directly afterward.
Some functionality to alter what was copied, such as trimming spaces off the ends of copied entries.

This script is controlled using the F1-F5 keys.
F1 will take whatever is currently selected and copy it to an array that you can scroll backwards and forwards through using F2 and F4. F3 will paste the currently selected entry in the array and increment the array by 1 automatically. F5 will reopen the settings gui.

I've included a video I made to demonstrate its features and use-cases:



Code here (ClipArray V1.1):

Code: Select all

/*
Created by Epishade
Credit to AfterLemon for his In-Line Send Sleep Function
*/

#NoEnv
#SingleInstance Force
SendMode Input

Global Array := []
Global F1Count = 0
Global F3Count = 0

Global DelimitByCell = 1
Global InterpretAsCode = 1
Global TrimEnds = 1
Global RemoveBlanks = 0

Gui:
Gui, Font, s10
Gui, Add, CheckBox, vDelimitByCell Checked%DelimitByCell%, Delimit selection by tabs/newlines/cells?
Gui, Add, CheckBox, vInterpretAsCode Checked%InterpretAsCode%, Some lines such as abc{1000}{tab} etc will be interpretted as code. Allow?
Gui, Add, CheckBox, vTrimEnds Checked%TrimEnds%, Trim spaces from the ends?
Gui, Add, CheckBox, vRemoveBlanks Checked%RemoveBlanks%, Remove blanks between entries?
Gui, Add, Button, x12 y100 w100 vButtonSave Default, Save
Gui, Add, Button, x125 y100 w100 vButtonHelp, Help
Gui, Add, Button, x238 y100 w100 vButtonReset, Reset
Gui, Show, , New GUI Window
Return
 
ButtonSave:
Gui, submit
Return

ButtonHelp:
MsgBox,
(
This script is used to copy and store large amounts of information to be manipulated and sequentially pasted as needed. It works best with Excel, but can also be used to copy information from elsewhere.

F1 = Copies the current selection and stores in array.
F2 = Decreases the current location in the array to be pasted.
F3 = Sends or pastes the current entry in the array.
F4 = Increases the current location in the array to be pasted.
F5 = Opens the Gui to make any settings changes.

Delimit selection by tabs/newlines/cells allows the user to select multiple cells to be copied, each cell of which will be placed independently in the array from the others.

Interpretting lines as code allows the user to write code into the copied selection, which will be executed when that selection is sent using F3.
This is useful for sending code to tab a certain amount of times or sleep for a little bit upon sending the array entry. Sleep is denotated by a number within curly braces. Example: Copying a cell with abc{1000}{tab} will, when pasted, result in "abc" being sent followed by a sleep of 1 second, and then a tab keypress.
Note that certain symbols, if copied, will also attempt to run code or alter the keys immediately following them. Example: Copying a cell with ^c anywhere within the text will interpret that as ctrl + c (copy).

Trim spaces from the ends removes any spaces from the ends of copied entries.

Remove blanks between entries will mean that any empty line data will be excluded in the copy array.
This is useful for ignoring blank entries in between sets of data.

)
Return

ButtonReset:
Reload
Return

GuiClose:
ExitApp
Return

F1::
Clipboard := ""
Send ^c
ClipWait, 1
Gosub ClipboardFormat
Return

F2::
F3Count -= 1
F1Count := F3Count
Gosub WatchToolTip
Return

F3::
F3Count += 1
F1Count := F3Count
If (InterpretAsCode = 0)     ; Uses SendRaw.
	SendRaw % Array[F3Count]
If (InterpretAsCode = 1)     ; Uses Send function.
	Send(Array[F3Count])
Gosub WatchToolTip
Return

F4::
F3Count += 1
F1Count := F3Count
Gosub WatchToolTip
Return

F5::
GUI Destroy
Gosub GUI
Return

WatchTooltip:
NextItem := Array[F3Count+1]
PreviousItem := Array[F3Count]
EntryCount := F3Count
ToolTip, Entry: %EntryCount% `r`nNext Entry: %NextItem% `r`nPrevious Entry: %PreviousItem%
SetTimer, RemoveTooltip, -3000
Return

RemoveTooltip:
ToolTip
Return

ClipboardFormat:
If (DelimitByCell = 0)
{
	F1Count += 1
	Clipboard := StrReplace(Clipboard,"`r`n","`n")     ; Fixes double-newline issue.
	Array[F1Count] := ClipboardTrim(Clipboard)
	If (RemoveBlanks = 1) && Trim(Clipboard = "`n")     ;If Clipboard is only composed of a newline, then it can be ignored.
	{
		F1Count -= 1 
	}
}
If (DelimitByCell = 1)
{
	Clipboard := StrReplace(Clipboard,"`r`n","¶")     ; Replaces newlines (vertical cells) with ¶ symbol.
	Clipboard := StrReplace(Clipboard,"`t","¶")     ; Replaces tabs (horizontal cells) with ¶ symbol.
	If (SubStr(Clipboard,0) = "¶")     ; Removes the last ¶ symbol from the string, if it exists, meaning selection was copied from excel.
	{
		Clipboard := Substr(Clipboard, 1, StrLen(Clipboard) - 1)
	}
	Loop, Parse, Clipboard, "¶"
	{
		F1Count += 1
		Array[F1Count] := ClipboardTrim(A_LoopField)
		If (RemoveBlanks = 1) && (Array[F1Count] = "")     ;If Clipboard is blank, then it can be ignored.
		{
			F1Count -= 1 
		}
	}
}
Return

ClipboardTrim(StringToTrim)
{
If (TrimEnds = 1)
{
	StringToTrim := Trim(StringToTrim)
}

Return StringToTrim
}

Send(String, Raw:="", RawKeys:="")
{
D:="{",E="}",S:=String D,i=0,T=1,R=(Raw?1:(SubStr(S,1,5)="{RAW}"?1:0)),M="+,!,#,^",K=RawKeys
While i:=InStr(S,D,V,i+1){
Send,% (R?"{RAW}":"") SubStr(S,T,InStr(S,D,V,i)-T)
B:=SubStr(S,InStr(S,D,V,i)+1,InStr(S,E,V,i)-StrLen(S)-1),A=SubStr(B,1,-1)
If InStr(S,D,V,i+1)
If(B&1=""){
If(A&1!="")
Sleep,% A*1000
else{
L:=(!R?(InStr(S,E,V,i)-StrLen(B)-2>4?4:InStr(S,E,V,i)-StrLen(B)-2):0)
Loop,%L%{
C:=SubStr(SubStr(S,InStr(S,D,V,i)-L,L),A_Index,1)
If C in %M%
{ C:=SubStr(S,InStr(S,D,V,i)-(L+1-A_Index),L+1-A_Index)
break
}else C:=""
}Send,% (K?"{RAW}":"") C "{" B "}"
}}else Sleep,%B%
T:=InStr(S,E,V,i+1)+1
}}
A few minor things to note:

I wrote this primarily to be used with Excel. As such, when multiple cells in Excel are selected and copied to the copy-array using F1, each cell will be split from the others and placed as its own separate entry in the copy-array. This setting works based on tabs and newlines, so if you are copying from somewhere outside of excel and your data has either tabs or newlines, it will attempt to split those entries into their own spots in the copy-array. If you don't want that functionality to split apart copied entries based on tabs or newlines, that can be unchecked in the gui settings.
Any copied entries that include curly brackets will attempt to run whatever is within those curly brackets as if they were code. If you have entries with curly brackets that you want to be pasted literally instead of ran as code, you will want to uncheck that option in the gui settings.
If your copied selection has a literal ¶ symbol in it (not likely), there may be some issues in splitting that copied selection up, and turning off the delimiter option may work better.

I'd also like to extend some credit to AfterLemon for his in-line sleep script found here:
viewtopic.php?t=392

Hope this script comes in handy for some people!
Last edited by Epishade on 21 Dec 2021, 17:06, edited 2 times in total.
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

07 Sep 2020, 17:08

Legacy Code:
ClipArray V1.0

Code: Select all

/*
Created by Epishade
Credit to AfterLemon for his In-Line Send Sleep Function
*/

#NoEnv
#SingleInstance Force
SendMode Input

Global Array := []
Global F1Count = 0
Global F3Count = 0

Global DelimitByCell = 1
Global InterpretAsCode = 1
Global TrimEnds = 1

Gui:
Gui, Font, s10
Gui, Add, CheckBox, vDelimitByCell Checked%DelimitByCell%, Delimit selection by tabs/newlines/cells?
Gui, Add, CheckBox, vInterpretAsCode Checked%InterpretAsCode%, Some lines such as abc{1000}{tab} etc will be interpretted as code. Allow?
Gui, Add, CheckBox, vTrimEnds Checked%TrimEnds%, Trim spaces from the ends?
Gui, Font, s15
Gui, Add, Button, w100 vButtonSave Default, Save
Gui, Add, Button, x125 y76 w100 vButtonHelp, Help
Gui, Add, Button, x238 y76 w100 vButtonReset, Reset
Gui, Show, , New GUI Window
Return

ButtonSave:
Gui, submit
Return

ButtonHelp:
MsgBox,
(
This script is used to copy and store multiple entries of information in a virtual clipboard to be manipulated and sequentially pasted as needed.

F1 = Copies the current selection and stores in array.
F2 = Decreases the current location in the array to be pasted.
F3 = Pastes the current entry in the array and increments array by 1.
F4 = Increases the current location in the array to be pasted.
F5 = Opens the Gui to make any settings changes.

Delimit selection by tabs/newlines/cells allows the user to select multiple entries to be copied, each of which will be placed independently in the copy array.

Interpretting lines as code allows the user to write code into the copied selection, which will be executed when that selection is sent using F3.
This is useful for sending code to tab a certain amount of times or sleep for a little bit upon sending the array entry. Sleep is denotated by a number within curly braces. Example: Copying an entry with abc{1000}{tab} will, when pasted, result in "abc" being sent followed by a sleep of 1 second, and then a tab keypress.

Trim spaces from the ends removes any spaces from the ends of copied entries.
)
Return

ButtonReset:
Reload
Return

GuiClose:
ExitApp
Return

F1::
Clipboard := ""
Send ^c
ClipWait, 1
Gosub ClipboardFormat
Return

F2::
F3Count -= 1
F1Count := F3Count
Gosub WatchToolTip
Return

F3::
F3Count += 1
F1Count := F3Count
If (InterpretAsCode = 0)     ; Uses SendRaw.
	SendRaw % Array[F3Count]
If (InterpretAsCode = 1)     ; Uses Send function.
	Send(Array[F3Count])
Gosub WatchToolTip
Return

F4::
F3Count += 1
F1Count := F3Count
Gosub WatchToolTip
Return

F5::
GUI Destroy
Gosub GUI
Return

WatchTooltip:
NextItem := Array[F3Count+1]
PreviousItem := Array[F3Count]
EntryCount := F3Count
ToolTip, Entry: %EntryCount% `r`nNext Entry: %NextItem% `r`nPrevious Entry: %PreviousItem%
SetTimer, RemoveTooltip, -3000
Return

RemoveTooltip:
ToolTip
Return

ClipboardFormat:
If (DelimitByCell = 0)
{
	F1Count += 1
	Clipboard := StrReplace(Clipboard,"`r`n","`n")     ; Fixes double-newline issue.
	Array[F1Count] := ClipboardTrim(Clipboard)
}
If (DelimitByCell = 1)
{
	Clipboard := StrReplace(Clipboard,"`r`n","¶")     ; Replaces newlines (vertical cells) with ¶ symbol.
	Clipboard := StrReplace(Clipboard,"`t","¶")     ; Replaces tabs (horizontal cells) with ¶ symbol.
	If (SubStr(Clipboard,0) = "¶")     ; Removes the last ¶ symbol from the string, if it exists, meaning selection was copied from excel.
	{
		Clipboard := Substr(Clipboard, 1, StrLen(Clipboard) - 1)
	}
	Loop, Parse, Clipboard, "¶"
	{
		F1Count += 1
		Array[F1Count] := ClipboardTrim(A_LoopField)
	}
}
Return

ClipboardTrim(StringToTrim)
{
If (TrimEnds = 1)
{
	StringToTrim := Trim(StringToTrim)
}
Return StringToTrim
}

Send(String, Raw:="", RawKeys:="")     ; Function to allow In-Line Send Sleep using curly brackets
{
D:="{",E="}",S:=String D,i=0,T=1,R=(Raw?1:(SubStr(S,1,5)="{RAW}"?1:0)),M="+,!,#,^",K=RawKeys
While i:=InStr(S,D,V,i+1){
Send,% (R?"{RAW}":"") SubStr(S,T,InStr(S,D,V,i)-T)
B:=SubStr(S,InStr(S,D,V,i)+1,InStr(S,E,V,i)-StrLen(S)-1),A=SubStr(B,1,-1)
If InStr(S,D,V,i+1)
If(B&1=""){
If(A&1!="")
Sleep,% A*1000
else{
L:=(!R?(InStr(S,E,V,i)-StrLen(B)-2>4?4:InStr(S,E,V,i)-StrLen(B)-2):0)
Loop,%L%{
C:=SubStr(SubStr(S,InStr(S,D,V,i)-L,L),A_Index,1)
If C in %M%
{ C:=SubStr(S,InStr(S,D,V,i)-(L+1-A_Index),L+1-A_Index)
break
}else C:=""
}Send,% (K?"{RAW}":"") C "{" B "}"
}}else Sleep,%B%
T:=InStr(S,E,V,i+1)+1
}}
V1.1 Changelog:
Added a checkbox in the gui menu and some code to eliminate blank entries when they are copied. When script is started, this is by default turned off and must be enabled by the user if wished for.
Last edited by Epishade on 21 Dec 2021, 17:08, edited 2 times in total.
maltby
Posts: 2
Joined: 27 Aug 2020, 00:48

Re: ClipArray (multiple clipboards w/ added functionality)

07 Sep 2020, 21:17

Looks great. The documentation is very clear!
Deepak8811
Posts: 4
Joined: 12 Jun 2018, 12:51

Re: ClipArray (multiple clipboards w/ added functionality)

08 Sep 2020, 23:31

:superhappy: :clap:

This is a fantastic script.

Just one more thing. How can I add tab after pasting each element?
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

09 Sep 2020, 15:07

Thanks Deepak! To add a tab after pasting, it needs to be in curly brackets. So if you wanted to copy abc123 and paste it somewhere else and have it automatically tab, you'll really want to copy abc123{tab}. This can be done easily in excel by using a cell formula, as I show in the video. It's also documented if you click on the Help button. And if you need it to run a little slower, you can run sleep commands by placing a number in curly brackets. So if you copy abc{1000}{tab}, then when you paste that entry, it will paste "abc", wait for 1 second, and then tab to the next field.
Deepak8811
Posts: 4
Joined: 12 Jun 2018, 12:51

Re: ClipArray (multiple clipboards w/ added functionality)

09 Sep 2020, 23:36

How about adding this functionality in the script? It will be difficult to convince non-tech people to create a concatenated column everytime to use the script and more the column's, more the time to concatenate them.
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

10 Sep 2020, 01:19

I thought about including that as a checkable option when I first created the script, but I decided against that as not all forms are created equal such that every time you paste something you'll want to tab to the next field. For example, you might want to skip a few fields after pasting one thing before you paste the next thing, in which case having an option to automatically tab after pasting won't put you in the right spot. It was my idea that by being able to write code to execute after you paste, you can customize it to your needs.

Plus, the running code part after pasting is of course optional. You can still copy and paste the raw data using the script if need-be, without worrying about tacking code onto the end. Also if you have a lot of rows of data for example, you can create the formula in Excel for one row and copy it down to all the other rows. If you only have a few rows of data, then that's a scenario where it might take less time to just use the script and tab to the next field manually without writing a cell formula to fill everything in for you. It's still faster than typing the data in manually or ctrl-c/ctrl-v-ing everything over.
burque505
Posts: 1735
Joined: 22 Jan 2017, 19:37

Re: ClipArray (multiple clipboards w/ added functionality)

11 Sep 2020, 08:29

@Epishade, that's very impressive. Thank you for sharing it.
Regards,
burque505
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

11 Sep 2020, 18:24

Thanks Burque! Hope you find some use out of it if you use it.
Core
Posts: 5
Joined: 14 Sep 2020, 10:49

Re: ClipArray (multiple clipboards w/ added functionality)

14 Sep 2020, 22:28

@Epishade I was just asking for this(!) I have a program that generates numerical data streams comprised of a big single number per line, for hundreds of lines, each number needs to be entered into a web form, and it was torture! Now, i'll try you program, i hope it reduces the toil and streamlines my work flow. Thank you!

Update: Three problems i immediately ran into, 1) Ctrl-Z stops working (in NotePad); and 2) i cannot accept copy from a CMD Window, which is where my data is... =( 3) There's no F-key to clear the array, so i have to kill & restart all the time.

Can you maybe add a Shift+F1 to copy from the current clipboard into the array? That way it may work from other applications in Windows. Maybe F6 could clear the array? 4

Ty!
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

17 Sep 2020, 00:41

Hi @Core . Are you sure you're selecting the text that you want to copy in your command window and pressing F1? It works for me just opening up the prompt, highlighting some text, and pressing F1 to put the data into the array. I would expect it to work all the same for you.

I'm not having the issue you're having with ctrl-z in NotePad either while I'm running this script. It works fine for me. Are you sure that's related to my script? I can't think of anything in my script that would cause ctrl-z to stop working.

You can clear the array by pressing F5 and clicking the reset button. A lot of times I just find it easier to skip ahead 50 entries or so and start copying from there anyways too.

I might suggest, if you're having trouble copying data directly from the source using the script that you may copy it normally with ctrl+c and paste it in an excel file or somewhere else that may work better. I don't know why it should be any different, as pressing F1 literally sends the ctrl+c command, but who knows?

If you want that functionality for Shift+F1 to put your current clipboard into the array, try adding the code below to the script:

Code: Select all

+F1::
Gosub ClipboardFormat
Return
Let me know if there's anything else I can help with!
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: ClipArray (multiple clipboards w/ added functionality)

17 Sep 2020, 07:06

Great script! The video is very well done! :thumbup:
Core
Posts: 5
Joined: 14 Sep 2020, 10:49

Re: ClipArray (multiple clipboards w/ added functionality)

19 Sep 2020, 08:18

@Epishade Did you run your CMD windows as Administrator? I run my CMD windows elevated, so it may be that your script won't copy my CMD windows due to permissions, please try that test and see if you have the same experience, or maybe you just run your AHK scripts elevated perhaps.

Also, I cannot get Ctrl-Z to fail again, so until it does and i can report more on it, let's ignore it as being an issue atm.

Where in your script do i insert the snippet you gave me?

I did not know Reset cleared the script's clipboard, will try that, ty! (tried it, it works easy enough, thx.)

Btw, in the floating box, did you notice the "Entry" index is 2 for every 1 line? I have to press F3 twice to paste the data (except for the 1st line of data), or press F2/F4 twice to step back/forwards, is that as it is intended to be?
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

19 Sep 2020, 19:43

@Core After trying out running the command prompt as administrator, it does appear that highlighting text and pressing F1 to store it won't work. If I run the script as administrator, that resolved the problem though. Hopefully that solution works for you. I don't work within command prompt enough to know of any other sort of solution.

If you have blank entries between data such that you need to press F3 twice or use F4 to step forward an extra time after pasting, that sounds to me that your data has more than 1 tab or newline between lines. This script will attempt to split entries by tab or newline, so if your data has more than one or isn't spaced appropriately that might happen.

As mentioned before, you might have more success copying and pasting the full set of data manually into an excel file or spreadsheet and copying from there or fixing it up if needed.
Core
Posts: 5
Joined: 14 Sep 2020, 10:49

Re: ClipArray (multiple clipboards w/ added functionality)

20 Sep 2020, 00:24

Hmm, not blank, just have to press F3 twice to paste data, i think the clue is: does your program look for a newline (\n) as LF or CR or CRLF? https en.wikipedia.org /wiki/Newline Broken Link for safety

The reason i ignore your wanting me to use an extra middleware step with the data is bc that will make the workflow far worse, i'm trying to save myself needless keystrokes & clicks, i want to do from CMD directly to browser, your script may have some quirks, but it's the fastest middleware script yet for doing this work. =)
Epishade
Posts: 13
Joined: 23 Aug 2018, 19:42

Re: ClipArray (multiple clipboards w/ added functionality)

20 Sep 2020, 03:52

In the code, I look at newlines as CRLF I suppose. Those newlines are replaced with a paragraph symbol and then the same is done with tabs so that I can delimit any entries separated by paragraph symbols into the copy array all at once.

Try monkeying around with the code on lines 117 and 118 maybe to change the type of newline being looked at I guess?

This code:

Code: Select all

Clipboard := StrReplace(Clipboard,"`r`n","¶")     ; Replaces newlines (vertical cells) with ¶ symbol.
Clipboard := StrReplace(Clipboard,"`t","¶")     ; Replaces tabs (horizontal cells) with ¶ symbol.
Dawondr
Posts: 2
Joined: 28 May 2021, 15:41

Re: ClipArray (multiple clipboards w/ added functionality)

28 May 2021, 15:46

This looks great and exactly what I need but every time I press the F3 button it just shows the next clipboard slot. The F1 is copying and F2 & F4 go forward and back, its just that F3 doesn't paste and just advances the parking slot. any suggestions? I'm a newbie with AHK so please don't get to technical - lol

Dave
jwwpua
Posts: 13
Joined: 06 Aug 2021, 15:03

Re: ClipArray (multiple clipboards w/ added functionality)

08 Sep 2021, 22:03

Old thread I know, but I'm having the same issue as the previous poster. It happens with Excel, notepad, and other apps too. If I copy multiple lines with f1, entry 1 works, entry 2 is blank, entry 3 is line2, 4 blank, etc.

Same in excel, whether I copy a row or column (or both). Can't figure out why blank entries are between every piece of data. Any ideas? If I disable the tab/line break option, everything is just pasted in 1 entry. Such a great script, so I'm hoping I can fix this somehow. Thanks!
jwwpua
Posts: 13
Joined: 06 Aug 2021, 15:03

Re: ClipArray (multiple clipboards w/ added functionality)

09 Sep 2021, 11:26

I was able to solve my issue above by replacing the backwards paragraph symbol (¶) with something else. I'm using a pipe (|) for now.

Somehow the ¶ was being interpreted in a way that created blank entries between the actual entries.👍


EDIT: I think it was lines 117, 118, 119, and 123.
Last edited by jwwpua on 10 Sep 2021, 13:43, edited 2 times in total.
Dawondr
Posts: 2
Joined: 28 May 2021, 15:41

Re: ClipArray (multiple clipboards w/ added functionality)

09 Sep 2021, 12:49

Thank you for the update jwwpua, I will give that a try for mine as well. Thanks again, Dave

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 162 guests