Skip to content

Instantly share code, notes, and snippets.

@xsmolasses
Last active December 2, 2023 15:17
Show Gist options
  • Save xsmolasses/6a78e5a9350ae1e746336601dfecb670 to your computer and use it in GitHub Desktop.
Save xsmolasses/6a78e5a9350ae1e746336601dfecb670 to your computer and use it in GitHub Desktop.
Record Clipboard Owner
/*
GetClipboardPwner.ahk
release: #10
support: AutoHotkey v1.1.21.01 and above seems compatible.
install: AutoHotkey v1.1 branch - not support v2.0 branch.
https://github.com/AutoHotkey/AutoHotkey/releases/tag/v1.1.37.01
execute:
GetClipboardPwner.ahk
command:
"AutoHotkey.exe" "GetClipboardPwner.ahk" [poll] [tooltip] [>suppress_console_out.txt]
START "" /WAIT "AutoHotkey.exe" "GetClipboardPwner.ahk" [poll] [tooltip]
START "" /B /WAIT "AutoHotkey.exe" "GetClipboardPwner.ahk" [poll] [tooltip] [>suppress_console_out.txt]
example:
START "" /WAIT "AutoHotkeyU32.exe" "GetClipboardPwner.ahk"
START "" /WAIT "AutoHotkeyU64.exe" "GetClipboardPwner.ahk" poll tooltip
*/
;
; #Requires [v1.1.33+]
;#Requires AutoHotkey v1.1.33+
; [v1.1.35+]: A_Clipboard is an alias of Clipboard.
;#Requires AutoHotkey v1.1.35+
;#Requires AutoHotkey >=v1.1.36 <2
;
;#Warn All,MsgBox
#NoEnv
;#NoTrayIcon
;#SingleInstance Force
#SingleInstance Off
;
Global __Args:=""
;
;
Global __stdout:=""
Global __conout:=""
;
;
Global __ToolTip:=false
;
Global __ClipboardKnownOwnersDetails:=[] ; hWnd array assigned details
;
;
try __stdout:=FileOpen("*","w`n","CP0")
if(!IsObject(__stdout))
{
if(!DllCall("AttachConsole","UInt",-1,"Int"))
DllCall("AllocConsole","Int")
try __conout:=FileOpen("*","w`n","CP0")
}
;
;
for i,_Arg in A_Args
{
if(A_Index>1)
__Args.=" "
__Args.=_Arg
}
;
;
if(RegExMatch(__Args,"\btooltip"))
{
__ToolTip:=true
}
;
if(RegExMatch(__Args,"\bpoll"))
{
stdout("GetClipboardPwner polling clipboard ownership . . .`n")
Loop
{
Sleep 50
GetClipboardPwner()
}
}
else
{
stdout("GetClipboardPwner awaiting clipboard changes . . .`n")
OnClipboardChange("GetClipboardPwner",1)
Loop
{
Sleep 2147483647
}
}
return
GetClipboardPwner(DataType:="")
{
Local _hWnd:=0,_TID:=0,_PID:=0,_ProcessPath:=""
Local _ClipboardPresentOwnerDetails:=""
Local _ClipboardKnownOwnersStringed:=""
Local _ClipboardSample
_hWnd:=DllCall("GetClipboardOwner","UPtr")
if(!_hWnd)
return
_TID:=DllCall("GetWindowThreadProcessId","UPtr",_hWnd,"UIntP",_PID,"UInt")
WinGet _ProcessPath,ProcessPath,ahk_pid %_PID%
_ClipboardPresentOwnerDetails:=_ProcessPath . " (PID: " . _PID . " hWnd: " . _hWnd . ")`n"
. (RegExMatch(Clipboard,"O)^([^`r`n]{1,66})",_ClipboardSample)
? "Clipboard sample on its own line:`n" . _ClipboardSample[1] . "`n" : "")
if(__ClipboardKnownOwnersDetails[_hWnd]!=_ClipboardPresentOwnerDetails)
{
__ClipboardKnownOwnersDetails[_hWnd]:=_ClipboardPresentOwnerDetails
stdout(_ClipboardPresentOwnerDetails)
}
if(__ToolTip)
{
_ClipboardKnownOwnersStringed:=""
for i,_ClipboardOwnerDetails in __ClipboardKnownOwnersDetails
{
_ClipboardKnownOwnersStringed.=_ClipboardOwnerDetails . "`n"
}
ToolTip % "Clipboard present Owner:`n`n" . _ClipboardPresentOwnerDetails
. "`nClipboard previous Owners:`n`n" . _ClipboardKnownOwnersStringed
}
}
stdout(cout)
{
if(IsObject(__stdout))
{
__stdout.WriteLine(cout)
__stdout.Read(0)
}
if(IsObject(__conout))
{
__conout.WriteLine(cout)
__conout.Read(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment