Using the clipboard

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
JonWayn
Posts: 17
Joined: 11 Feb 2024, 03:14

Using the clipboard

15 Apr 2024, 17:31

This is a snippet of code that I am having problems with:

Code: Select all

	;	Copy file to clipboard
	SendInput "{Alt}{Right}{Enter}"
	Sleep 1000
	SendInput "C"
	ClipWait
	
	DeleteFile(ExportPth)
	FileAppend A_Clipboard, ExportPth
So, in the bigger picture, I am opening a file in Acrobat, selecting its full text, then using the edit menu to copy that text to the clipboard. But in the snippet above where I attempt to write the clipboard contents to a file, some other content gets written, which is just some script code, rather than the content that was just added to the clipboard. However, if I were to open a text window like notepad, right after, and paste, the file content gets pasted then. What am I doing wrong, and how can I correct it?

Thank you
User avatar
mikeyww
Posts: 27011
Joined: 09 Sep 2014, 18:38

Re: Using the clipboard

15 Apr 2024, 19:36

Hello,

Why not Ctrl+C? It is a standard key sequence to copy selected text.

To send modified keys, read: https://www.autohotkey.com/docs/v2/lib/Send.htm#Parameters

How to use ClipWait properly
JonWayn
Posts: 17
Joined: 11 Feb 2024, 03:14

Re: Using the clipboard

15 Apr 2024, 19:43

I did try that first. ClipWait never returned, so I changed to using the menu item. However, the text actually does get copied as indicated after, when I use Ctrl+Paste in another window. So, why isn't it equal to the contents of A_Clipboard when used with FileAppend?
JonWayn
Posts: 17
Joined: 11 Feb 2024, 03:14

Re: Using the clipboard

15 Apr 2024, 19:58

Oh, wait a minute! I tried Ctrl+c instead of Ctrl+C and it now works. Thank you for your insight
User avatar
mikeyww
Posts: 27011
Joined: 09 Sep 2014, 18:38

Re: Using the clipboard

15 Apr 2024, 20:15

Code: Select all

#Requires AutoHotkey v2.0
acrobat := 'ahk_exe Acrobat.exe'
outFile := 'clips.txt'

#HotIf WinActive(acrobat)
F3 Up:: {
 A_Clipboard := '', Send('^c')
 If ClipWait(1) {
  FileAppend A_Clipboard '`r`n', outFile
  SoundBeep 1500
 } Else MsgBox 'An error occurred while waiting for the clipboard.', 'Error', 'Icon!'
}
#HotIf
JonWayn
Posts: 17
Joined: 11 Feb 2024, 03:14

Re: Using the clipboard

15 Apr 2024, 23:41

Nice sample code. I modified my existing code to that structure. Thank you
JonWayn
Posts: 17
Joined: 11 Feb 2024, 03:14

Re: Using the clipboard

17 Apr 2024, 04:31

mikeyww, would explain the line of code: A_Clipboard := '', Send('^c')?
is it equivalent to 2 separate commands, and if so, is there a reason to place them on 1 line? Otherwise, what does it mean?

Thank you
User avatar
mikeyww
Posts: 27011
Joined: 09 Sep 2014, 18:38

Re: Using the clipboard

17 Apr 2024, 05:17

  1. AHK has no commands per se.
  2. Yes, these are two separate statements separated by a comma.
  3. Combining statements onto a line is mostly a convenience. You are welcome to separate the statements.
Comma (multi-statement). Commas may be used to write multiple sub-expressions on a single line. This is most commonly used to group together multiple assignments or function calls. For example: x:=1, y+=2, ++index, MyFunc(). Such statements are executed in order from left to right. In contrast to AutoHotkey v1, the comma operator usually performs worse than writing separate expressions. Performance continues to decrease as more and more expressions are combined into a single expression; for example, it can be 35 % slower to combine five or ten simple expressions into a single expression. The exception to this may be when combining function calls.
Source: Variables and Expressions - Definition & Usage | AutoHotkey v2

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Google [Bot], joedf, kunkel321, niCode, vmech and 77 guests