Page 1 of 1

works in IDE, won't work “in the field”

Posted: 11 May 2024, 20:52
by robinson
Hi, noob here.
Sorry for the unhelpful title but it's not easy to describe this problem in 8 words.
This little part of my script is trying to identify two separate paragraphs within a string by the gap between them,
then store them in two variables called a and b.

Code: Select all

+x:: {
	KeyWait("x"), KeyWait("LShift"), sound_pop()
z:="
(
1
2
3

4
5
6
)"
	a:=Trim(SubStr(z, 1, InStr(z,"`n`n")),"`n")     ; 1st paragraph (start at start, end at double carriage return)
	b:=Trim(SubStr(z, InStr(z,"`n`n")   ),"`n")     ; 2nd paragraph (start at double carriage return, end at end)
	MsgBox(a b)
}
So this is what you see in the message box, which means it's working:

Code: Select all

1
2
34
5
6
But then why, when I write out the contents of the z variable into a Word document or something,
highlight it, copy it to clipboard, and store the clipboard contents in z,
testing it again “in the field”, as it were, the message box suddenly returns blank/empty/nothing.
I suspect it has something to do with the carriage returns, but I don't know.
Something small and stupid on my part no doubt.
Can someone point me in the right direction?
Thanks!

Re: works in IDE, won't work “in the field”

Posted: 11 May 2024, 21:52
by mikeyww
Hello,

From what you described, you are running a script other than the script that you have posted. I recommend posting the script that demonstrates the problem that you are experiencing.

Re: works in IDE, won't work “in the field”

Posted: 11 May 2024, 23:06
by robinson
@mikeyww
I'm writing this text into a Word document and selecting it:

Code: Select all

1
2
3

4
5
6
And the script which is producing a blank message box is this:

Code: Select all

+x:: {
	KeyWait("x"), KeyWait("LShift"), sound_pop()
	SendEvent("{LCtrl DOWN}{c DOWN}{c UP}{LCtrl UP}"), Sleep(200)         ; ^c
	z:=A_Clipboard, Sleep(200)
	a:=Trim(SubStr(z, 1, InStr(z,"`n`n")),"`n")     ; 1st paragraph (start at start, end at double carriage return)
	b:=Trim(SubStr(z, InStr(z,"`n`n")   ),"`n")     ; 2nd paragraph (start at double carriage return, end at end)
	MsgBox(a b)
}

Re: works in IDE, won't work “in the field”

Posted: 12 May 2024, 04:25
by boiler
The line breaks in your copied text are probably made up of CR+LF (`r`n), not just LF (`n). You should have your script be able to handle either case, which you can do by making this your z assignment:

Code: Select all

z := StrReplace(A_Clipboard, "`r`n", "`n")

Also, it’s best to use ClipWait instead of Sleep to wait for the clipboard to be populated. Empty the clipboard before sending Ctrl+C when using it.

Re: works in IDE, won't work “in the field”

Posted: 12 May 2024, 07:00
by flyingDman
I'd use:

Code: Select all

#+c::
	{
	a_clipboard := ""	
	send "^c"
	if clipwait(1)
		{	
		z := regexreplace(a_clipboard,"`n`r","`n")
		arr := strsplit(z,"`n`n","`r") 
		MsgBox(a:=arr[1] b:=arr[2])
		}
	}
If you have more than 2 paragraphs use Join (https://www.autohotkey.com/docs/v2/Functions.htm#Variadic)