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

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
robinson
Posts: 214
Joined: 12 Sep 2019, 20:28

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

11 May 2024, 20:52

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!
User avatar
mikeyww
Posts: 27192
Joined: 09 Sep 2014, 18:38

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

11 May 2024, 21:52

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.
robinson
Posts: 214
Joined: 12 Sep 2019, 20:28

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

11 May 2024, 23:06

@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)
}
User avatar
boiler
Posts: 17209
Joined: 21 Dec 2014, 02:44

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

12 May 2024, 04:25

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.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

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

12 May 2024, 07:00

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)
14.3 & 1.3.7

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot] and 43 guests