Loop through clip board lines and manipulate text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tazzzan33
Posts: 16
Joined: 07 Dec 2017, 12:41

Loop through clip board lines and manipulate text

13 Dec 2017, 13:36

I am currently trying to write a program that allows me to copy multiple lines of text and append a comma to the end of the line. (except for the last line).

However I have ran into errors with special characters and cannot figure out how to allow them to work. Id rather accept all characters into the clipboard rather than stripping them out if possible.

First I tried to replace them with RegExReplace but that has not worked and is giving me more errors. Please see the code below, I have put both the simple loop and the replace logic into it.

Code: Select all

#b::
index:=0
ClipSaved := ClipboardAll
Loop, parse, clipboard, `n, `r
{
	;
	;tmp :=RegExReplace(A_loopfield,[\\/:*?"<>|])    
	index+=1
	;MsgBox,%A_Index%   %tmp%,
	NewClip[index] := %A_LoopField%,
	
}
Return

Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Loop through clip board lines and manipulate text

13 Dec 2017, 14:08

Code: Select all

Loop, parse, clipboard, `n, `r
{	
	str .= RegExReplace(A_loopfield,"[\\/:*?""<>|]") ",`n"
}
MsgBox % SubStr(str,1,-2) ; to eliminate the last comma and the last newline
Return
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Guest

Re: Loop through clip board lines and manipulate text  Topic is solved

13 Dec 2017, 14:09

keep it simple:

Code: Select all

; one
newtext:=""
Loop, parse, clipboard, `n, `r
	newtext .= A_LoopField ",`n"
MsgBox % newtext ; clipboard:=newtext 

Code: Select all

; two
clipboard:=StrReplace(clipboard,"`n",",`n")
MsgBox % clipboard
what you're doing with NewClip[index] won't work as that assumes NewClip in an Object/Array which it isn't in your script (yet) you would need to have "NewClip:=[]" or "NewClip:={}" at the start of your script in order to be able to do that easier would be to .push it (just like Salt & Peppa in the 90s)

Code: Select all

; three
newtext:=[]
Loop, parse, clipboard, `n, `r
	newtext.push(A_LoopField ",")

for k, v in newtext
	MsgBox % k ":" v

tazzzan33
Posts: 16
Joined: 07 Dec 2017, 12:41

Re: Loop through clip board lines and manipulate text

13 Dec 2017, 14:31

So why wont the substr function work with clipboard like it does with other variable? It is still putting in the last comma.

Also thank you for your reply this helps so much!

Code: Select all

 
Loop, parse, clipboard, `n, `r
{	
	str .= RegExReplace(A_loopfield,"[\\/:*?""<>|]") ",`n"
}
;MsgBox % SubStr(str,1,-2) ; to eliminate the last comma and the last newline

;SendRaw, % SubStr(str,1,-2)
clipboard := % SubStr(str,1,-2)
Send, ^v
Return
tazzzan33
Posts: 16
Joined: 07 Dec 2017, 12:41

Re: Loop through clip board lines and manipulate text

13 Dec 2017, 14:40

I actually figured it out. Still getting the hang of when to use the % and when not to. Thank you again for the help!

Code: Select all


#v::
index:=0
ClipSaved := ClipboardAll
Loop, parse, clipboard, `n, `r
{	
	str .= RegExReplace(A_loopfield,"[\\/:*?""<>|]") ",`n"
}
clipboard :=  SubStr(str,1,-2)
Send, ^v
clipboard := ClipSaved
Return

Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Loop through clip board lines and manipulate text

13 Dec 2017, 14:55

tazzzan33 wrote:I actually figured it out. Still getting the hang of when to use the % and when not to. Thank you again for the help!
A helpful tip is to enclose variables in % signs when strings are expected to be UNquoted. So, when you look at an example in the documentation and see no quotes around a string (ex. MsgBox, Hello World!), use % signs if you plan to replace that string with a variable (ex. MsgBox, %myString%), but, when you do see quotes (ex. SubStr("123abc789", 4, 3)), don't use % signs (ex. SubStr(myString, 4, 3)).

When it comes to assignments, you can do either myString = Hello World! or myString := "Hello World!. If you wanted to replace the message with a variable, you would have to enclose it in % in the first case (ex. myString = %myMsg%) because no quotes are necessary with the = operator, but you wouldn't with the second case (ex. myString := myMsg) because quotes are necessary with the := operator.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], SimmoF, uchihito and 220 guests