Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Detect clipboard contents as text, file, etc.



  • Please log in to reply
3 replies to this topic
pyahkr
  • Members
  • 2 posts
  • Last active: Oct 30 2015 09:39 PM
  • Joined: 29 Oct 2015

I've tried looking for a solution for this, but cannot seem to find one. I'm trying to override the Windows built in copy hotkey (ctrl+c) with one that will operate the same way, but will replace some TEXT so that pasting pastes the correction; I'd like to avoid using a new hotkey. Although the code works to replace the text, it has the unfortunate consequence of prevents me from copying FILES to paste them (an obvious issue).

 

I've tried using OnClipboardChange and referencing A_EventInfo, but that returns 1 for both text and files (the issue with copying files mentioned above). Using ClipboardAll does not seem to fix the problem either, unless I'm doing something incorrect.

 

Does anyone know a solution for accomplishing this using a conditional (other than A_EventInfo) to modify only if the contents are text; otherwise, do nothing?

 

#~simplified code

$^c::
clipboard = ; Empty the clipboard
send, ^c
ClipWait, 1 ;wait until something is on the clipboard
if ErrorLevel
{
    ToolTip Error copying to the clipboard.
    return
}
gosub, OnClipboardChange
return


OnClipboardChange:
ToolTip %A_EventInfo% %Clipboard%
Sleep, 300
ToolTip
if (A_EventInfo = 1) { ;but this is the same for text and files (returns 2 for images)
gosub, rep
}

return

rep:
   StringReplace, Clipboard, Clipboard, oldtext, newtext, all 
   ToolTip %A_EventInfo% %Clipboard%
   Sleep, 1000
   ToolTip 
return

 

 

 



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
try it like this

$^c::
clipsave := ClipboardAll ; save the clipboard
clipboard = ; Empty the clipboard for clipwait to work
send, ^c ; send control + c
ClipWait, 1 ; wait a max of 1 second or until something is on the clipboard
if ErrorLevel ; if not was in the clipboard within 1 second 
{
    ToolTip Error copying to the clipboard.
    return
}

ClipVariable := Clipboard
if RegExMatch(ClipVariable, "^\D:\\.*(\.\w{3})", match)
{
	ToolTip File patten in the clipboard will not change.
    return
}

gosub rep
return

rep:
   StringReplace, Clipboard, Clipboard, oldtext, newtext, all 
   ToolTip %Clipboard%
   Sleep, 1000
   ToolTip 
return

Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer
if DllCall("IsClipboardFormatAvailable", "uint", 1)
    MsgBox Clipboard contains text.
else if DllCall("IsClipboardFormatAvailable", "uint", 15)
    MsgBox Clipboard contains files.
else
    MsgBox Clipboard does not contain files or text.
It is possible for an application to store text and files simultaneously, but most do not.

pyahkr
  • Members
  • 2 posts
  • Last active: Oct 30 2015 09:39 PM
  • Joined: 29 Oct 2015

Thanks guys! I went with Lexikos's approach. This is amazingly helpful for this script, but also knowing this for moving forward. 

Jackie Sztuk _Blackholyman, I was using a regex at one point, but I was still having trouble. That might be due to me struggling with regex anyway. Your example helps me learn a bit too. Thanks

 

Script below for anyone interested.

$^c::
clipboard = ; Empty the clipboard
send, ^c
ClipWait, 1 ;wait until something is on the clipboard
if ErrorLevel
{
   ToolTip Error copying to the clipboard.
   return
}
if DllCall("IsClipboardFormatAvailable", "uint", 1) {
   gosub, rep
}
else if DllCall("IsClipboardFormatAvailable", "uint", 15) {
}
else {
}
return

rep:
StringReplace, Clipboard, Clipboard, oldtext, newtext, all 
ToolTip %Clipboard%
Sleep, 1000
ToolTip 
return