Page 1 of 1

Function cannot return ClipboardAll

Posted: 21 Feb 2019, 09:34
by Scoox
[Moderator's note: Topic moved from Bug Reports.]

Hi, just bumped into the following problem:

Code: Select all

!v::
	Clipboard := ;Clear clipboard
	Clipboard := MyFunc() ;MyFunc() returns "hello world", write this string to clipboard
	SendInput, ^v ;Paste clipboard contents (should paste "hello world")
Return

MyFunc()
{
	Clipboard := "hello world" ;Put "hello world" in Clipboard
	MyVar := ClipboardAll ;Write Clipboard to MyVar. MyVar now contains "hello world"
	Return MyVar ;Return MyVar i.e. function returns "hello world"
}
The return value of MyFunc() is the contents of ClipboardAll, which is assigned back to Clipboard. This results in an empty clipboard. Could this be a bug? Thanks

Re: Function cannot return ClipboardAll

Posted: 21 Feb 2019, 11:52
by iPhilip
As posted, it works for me, i.e. !v and ^v return hello world.

Re: Function cannot return ClipboardAll

Posted: 21 Feb 2019, 22:01
by Scoox
iPhilip wrote:
21 Feb 2019, 11:52
As posted, it works for me, i.e. !v and ^v return hello world.
Dang! It was supposed to be MyVar := ClipboardAll, my bad. Please try again, thanks!

Re: Function cannot return ClipboardAll

Posted: 22 Feb 2019, 00:00
by iPhilip
Hi Scoox,

The documentation says:
https://autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll wrote: Binary-clipboard variables may be passed to functions by value (formerly they only worked ByRef).
It doesn't say that clipboard variables can be returned by a function. This works in place of what you posted:

Code: Select all

!v::
   Clipboard := ;Clear clipboard
   MyFunc(Var) ;MyFunc() returns "hello world" in Var
   Clipboard := Var ;Write this string to clipboard
   SendInput, ^v ;Paste clipboard contents (should paste "hello world")
   Var := ;Free the variable in case the clipboard was very large
Return

MyFunc(ByRef MyVar) {
   Clipboard := "hello world" ;Put "hello world" in Clipboard
   MyVar := ClipboardAll ;Write Clipboard to MyVar. MyVar now contains "hello world"
}
I hope it helps.

Re: Function cannot return ClipboardAll

Posted: 22 Feb 2019, 02:43
by Helgef
Your code is perfectly logic and it is reasonable to believe it should work, however, ClipboardAll is one of the quirks of v1, You should take this part of the documentation literally, it is complete,
A variable containing clipboard data can be copied to another variable as in this example: ClipSaved2 := ClipSaved.

This issue is fixed in v2 :thumbup: .

Cheers.

Re: Function cannot return ClipboardAll

Posted: 23 Feb 2019, 21:05
by Scoox
iPhilip wrote:
22 Feb 2019, 00:00
It doesn't say that clipboard variables can be returned by a function.
... though it doesn't say clipboard variables cannot be returned either (at least I didn't find it). From your replies here this looks like a quirk + documentation issue.

Re: Function cannot return ClipboardAll

Posted: 24 Feb 2019, 05:30
by nnnik
Well yeah assigning ClipboardAll ro another variable and writing it to a file using FileAppend is all you can do with it.
Having ClipboardAll and Clipboard as a variable in the first place was a mistake imo.

Re: Function cannot return ClipboardAll

Posted: 24 Feb 2019, 20:35
by Scoox
nnnik wrote:
24 Feb 2019, 05:30
Well yeah assigning ClipboardAll ro another variable and writing it to a file using FileAppend is all you can do with it.
Having ClipboardAll and Clipboard as a variable in the first place was a mistake imo.
Yeah, I would have preferred two functions, something like ClipboardGet(Mode:=0) and ClipboardPut(Mode:=0), where Mode tells the function whether to deal with raw clipboard data (0) or just text (1). And maybe even a ClipboardSwap(Data, Mode:=0).

Re: Function cannot return ClipboardAll

Posted: 27 Feb 2019, 20:53
by iPhilip
Hi Scoox,

I have this post by lexikos bookmarked to remind me of the v1 limitations around binary variables.

Cheers!

- iPhilip