Use that thread if you have questions as this one is archived.
Clip() - All your clipboard needs in one place
This script consolidates two things you do that involve the clipboard: Finding out what text is currently being selected, and sending large amounts of text via Control+V. It also standardizes some features often needed with these operations, and improves performance through consolidation.
Clip(Text="", ReSelect=False)
Usage: Call it without any parameters to retrieve the text currently selected.
Var := Clip() ; will store any selected text in %Var%Or call it with some text in the first parameter to "send" that text via the clipboard and Control+V.
; The two are analogous. Clip() is generally preferable for larger amounts of text Clip("Some text") SendInput {Raw}Some text ; Raw because when using Clip() keyboard combinations like ^s (ctrl+s) will be sent literally. SendInput {Raw} also does this.Parameters:
- Text:If you put text in this parameter, the function will send the text via the clipboard. If you leave this parameter blank, the function will return the selected text instead.
- ReSelect: Only use this parameter in send mode (i.e. if there is text in the first parameter.) If ReSelect is true (has a value of 1), the cursor will re-select the just-pasted text. This is convenient if you are going to make extra modifications to the text, for instance, if you want to bold it after you paste it. If ReSelect is empty or false (equal to 0), then it will not reselect the text. If ReSelect is any other value (such as 2 or a non-numeric string), it will only reselect the text if there are 3000 or fewer characters - more than this number may be lead to choppy re-selecting.
- Can send and retrieve with one function
- No delay while sending. Normally you have to wait 400ms or so after sending Control+V before restoring the clipboard's contents, or else sometimes it pastes the backup contents instead. Clip() tasks this to a timer so your script can continue executing.
- Improves performance by only saving & restoring the clipboard's contents once in the case of rapid clipboard operations.
- Can reselct pasted text.
;---------------------------------------------------------------------------------------------------- ; Simple examples ^1::MsgBox, % "The selected text is: " Clip() ; displays the selected text ^2::Clip(A_Now, True) ; sends A_Now and then re-selects it ^3::Clip("""" Clip() """") ; puts quotes around the selected text. Despite calling Clip() twice, the clipboard is only backed up & restored once ;---------------------------------------------------------------------------------------------------- ; More involved example ; The below hotkeys add some of the features of SciTE to notepad. #IfWinActive ahk_class Notepad ; makes the hotkeys context-sensitive ; Duplicate the above line ^d:: SendInput {End}+{Home} @ := Clip() SendInput {End}{Enter} Clip(@) Return ; Swap with the above line ^t:: SendInput {End}+{Home} @ := Clip() SendInput {Del 2}{Up}{Enter}{Up} Clip(@) Return ; Indent a block of text. (This introduces a small delay for typing a normal tab; to avoid this you can use ^Tab / ^+Tab as a hotkey instead.) $Tab:: $+Tab:: TabChar := A_Tab ; this could be something else, say, 4 spaces NewLine := "`r`n" If ("" <> Text := Clip()) { @ := "" Loop, Parse, Text, `n, `r @ .= NewLine (InStr(A_ThisHotkey, "+") ? SubStr(A_LoopField, (InStr(A_LoopField, TabChar) = 1) * StrLen(TabChar) + 1) : TabChar A_LoopField) Clip(SubStr(@, StrLen(NewLine) + 1), 2) } Else Send % (InStr(A_ThisHotkey, "+") ? "+" : "") "{Tab}" Return #IfWinActiveCode:
Clip(Text="", Reselect="") ; http://www.autohotkey.com/forum/viewtopic.php?p=467710 , modified February 19, 2013 { Static BackUpClip, Stored, LastClip If (A_ThisLabel = A_ThisFunc) { If (Clipboard == LastClip) Clipboard := BackUpClip BackUpClip := LastClip := Stored := "" } Else { If !Stored { Stored := True BackUpClip := ClipboardAll ; ClipboardAll must be on its own line } Else SetTimer, %A_ThisFunc%, Off LongCopy := A_TickCount, Clipboard := "", LongCopy -= A_TickCount ; LongCopy gauges the amount of time it takes to empty the clipboard which can predict how long the subsequent clipwait will need If (Text = "") { SendInput, ^c ClipWait, LongCopy ? 0.6 : 0.2, True } Else { Clipboard := LastClip := Text ClipWait, 10 SendInput, ^v } SetTimer, %A_ThisFunc%, -700 Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter} If (Text = "") Return LastClip := Clipboard Else If (ReSelect = True) or (Reselect and (StrLen(Text) < 3000)) { StringReplace, Text, Text, `r, , All SendInput, % "{Shift Down}{Left " StrLen(Text) "}{Shift Up}" } } Return Clip: Return Clip() }Notes:
- With any large amount of text (more than 20 or so characters), it is usually MUCH faster to send it via the clipboard, as opposed to with Send or SendInput.
- Clip() waits only .15 seconds before its ClipWait times out and reports that no text is selected. I have done some testing and decided that this is an ample amount. However, on a slow machine it is possible you might have to increase this timeout.
- The delayed restoration of the clipboard that occurs with Clip() can have unintended consequences. For instance, if you send some text with Clip() and then immediately exit the script, the clipboard will never be restored to its original state because the timer to restore it did not expire.
- You cannot have another "Clip" label in your script. If this is problematic, you can easily enough change the name of the label.