Problem with pasting from clipboard Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Problem with pasting from clipboard

Post by PuzzledGreatly » 06 May 2021, 17:42

I wrote a script to sort pipped strings such as "This|is|pipped|". I've confirmed with msgboxes that it is doing what I want it to do. My problem is that I can't paste the result and I can't see why. Here's the code:

Code: Select all

Saveboard := ClipboardAll

send ^a^c
clipwait, 1

main := ""
loop, parse, clipboard, `n,`r
{
	line := ""
	if instr(A_loopfield,"|")
	{
		if instr(A_loopfield, A_tab)
		loop, parse, A_loopfield, %A_tab%
		{
			if instr(A_loopfield, "|")
			{
				mark := A_loopfield
				sort, mark, U D|
				line .= mark
			}		
			else
			line .= A_loopfield A_tab
		}
		main .= line "`n"
	}
	else
	main .= A_loopfield "`n"
}
;msgbox, 4096,main, % main
clipboard := main
;send %main%
send ^v

Clipboard = %Saveboard%
Saveboard =
Send %main% will work but it is slow. Why doesn't send ^v work? I feel I'm missing something obvious but this is driving me nuts. Please help. Thanks
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Problem with pasting from clipboard

Post by mikeyww » 06 May 2021, 18:29

Your example contains no tabs, but your script refers to tabs. Please provide two examples of input and the corresponding desired output.
User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Problem with pasting from clipboard

Post by Smile_ » 06 May 2021, 18:38

Small note:

Code: Select all

	if instr(A_loopfield,"|")
	{
		if instr(A_loopfield, A_tab)
Why don't you just

Code: Select all

if instr(A_loopfield,"|") && instr(A_loopfield, A_tab)
Plus I tried both send %main% and send ^v both are working, what I'm thinking is that you are testing on example that doesn't have tabs like @mikeyww mentioned so it results to an empty output and then of course nothing is going to be send, what I'm trying to say check what you are testing it on maybe it doesn't satisfy the required conditions.
Last edited by Smile_ on 06 May 2021, 18:46, edited 1 time in total.
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Problem with pasting from clipboard

Post by mikeyww » 06 May 2021, 18:42

Another detail: ClipWait has no effect unless you clear the clipboard first. Also: after you set the clipboard, a ClipWait can be useful.

Example: https://www.autohotkey.com/boards/viewtopic.php?p=276834#p276834
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with pasting from clipboard

Post by PuzzledGreatly » 06 May 2021, 18:57

Thanks for the reply, Mikey. The problem isn't with the input or output but with pasting the output. Why isn't Send ^v pasting anything? For example, I changed the end of my code to:

Code: Select all

msgbox, 4096,main, % main
clipboard := main
msgbox, 4096, Clipboard, % clipboard
send, ^v
Clipboard = %Saveboard%
Saveboard =
Msgbox Main gives me my expected output. Msgbox clipboard confirms the output is on the clipboard but I'm not seeing any change after using send, v. If I used send %main% my file is updated but that takes several seconds to complete and shouldn't be necessary.
User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Problem with pasting from clipboard

Post by Smile_ » 06 May 2021, 19:05

Did you tried SendRaw instead of Send?
Same thing?
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Problem with pasting from clipboard  Topic is solved

Post by rommmcek » 06 May 2021, 19:15

Send, ^v is asynchronous with the script, its execution depends of the app it was send to. So try:

Code: Select all

Send, ^v
Sleep, 300
while DllCall("GetOpenClipboardWindow", Ptr)
	Sleep 50
Clipboard:= Saveboard
Saveboard:= ""
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with pasting from clipboard

Post by PuzzledGreatly » 06 May 2021, 19:19

Thanks for the replies. I added Clipboard = to clear the clipboard so clipwait should function correctly. Sendraw seems to give the same result as send and sendinput. They work but they are slow. Based on the code should send ~v paste the main variable? Perhaps something is going on in the background on my machine to stop paste functioning as it should???
User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Problem with pasting from clipboard

Post by mikeyww » 06 May 2021, 19:31

If you save the clipboard to a file, what is actually there? If you press ^v manually on your keyboard, does it work?

Post your revised script for feedback about it.

Since you have chosen to send ^v without a ClipWait, you might also get unexpected results (though probably not the specific problem in this case).
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with pasting from clipboard

Post by PuzzledGreatly » 06 May 2021, 19:40

Thanks rommmcek that solved the problem with the paste though I don't understand what your code is doing. For clarity Here's my complete code with some amendments to remove rogue pipes. I'm now using ^x rather than ^c:

Code: Select all

Saveboard := ClipboardAll
clipboard =
send ^a^x
clipwait, 1

main := ""
loop, parse, clipboard, `n,`r
{
	line := ""
	if instr(A_loopfield,"|")
	{
		if instr(A_loopfield, A_tab)
		loop, parse, A_loopfield, %A_tab%
		{
			if instr(A_loopfield, "|")
			{
				mark := A_loopfield
				sort, mark, U D|
				line .= mark
			}		
			else
			line .= A_loopfield A_tab
		}
		line := strReplace(line,"||","|")
		line := instr(substr(line,0),"|") ? substr(line,1,-1) : line
		main .= line "`n"
	}
	else
	main .= A_loopfield "`n"
}
clipboard := substr(main,1,-1)
Send, ^v
Sleep, 300
while DllCall("GetOpenClipboardWindow", Ptr)
Sleep 50
Clipboard:= Saveboard
Saveboard:= ""
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with pasting from clipboard

Post by PuzzledGreatly » 06 May 2021, 19:57

Revised version:

Code: Select all

Saveboard := ClipboardAll
clipboard =
send ^a^x
clipwait, 1

main := ""
loop, parse, clipboard, `n,`r
{
	line := ""
	if instr(A_loopfield,"|")
	{
		if instr(A_loopfield, A_tab)
		loop, parse, A_loopfield, %A_tab%
		{
			if instr(A_loopfield, "|")
			{
				mark := A_loopfield
				mark := strReplace(mark,"||","|")
				mark := instr(substr(mark,0),"|") ? substr(mark,1,-1) : mark
				mark := instr(substr(mark,1,1),"|") ? substr(mark,2) : mark
				sort, mark, U D|
				line .= mark
			}		
			else
			line .= A_loopfield A_tab
		}
		main .= line "`n"
	}
	else
	main .= A_loopfield "`n"
}
clipboard := substr(main,1,-1)
Send, ^v
Sleep, 300
while DllCall("GetOpenClipboardWindow", Ptr)
Sleep 50
Clipboard:= Saveboard
Saveboard:= ""
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: Problem with pasting from clipboard

Post by boiler » 06 May 2021, 20:19

To expand on rommmcek’s correct reply, this is a known issue since Windows 10 added clipboard management support, whether you’ve enabled it or not. It takes Windows longer to complete the pasting operation, so you can’t clear the clipboard or assign other contents back to it immediately after pasting. Adding a Sleep, 500 (or 300 is fine) after a Send, ^v and before assigning anything else to the clipboard takes care of this issue.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Problem with pasting from clipboard

Post by PuzzledGreatly » 07 May 2021, 01:11

Thanks, boiler for the explanation. I really find Windows 10 wretched compared with Windows 7 but I'm stuck with it.
User avatar
rommmcek
Posts: 1470
Joined: 15 Aug 2014, 15:18

Re: Problem with pasting from clipboard

Post by rommmcek » 10 May 2021, 01:59

Here is a good thread about explaining this: 'PasteWait': wait for paste to complete w/ my little contribution.
Post Reply

Return to “Ask for Help (v1)”