1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Detect Clipboard Format

Discussion in 'T&T - Scripting' started by Joe Caverly, Oct 15, 2013.

  1. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    Visit http://jpsoft.com/forums/threads/detecting-clipboard-format.5225/ for a message thread about clipboard formats, and also for a link to a plugin that does the same thing.

    Joe

    Code:
    ::-------------------------------------------------------
    :: CLIPFMT.BTM
    :: Shows how many different formats are on the clipboard,
    ::  then lists the clipboard formats.
    ::
    :: 2013/10/15
    :: Joe Caverly
    ::
    ::TCC  15.01.52  Windows Vista [Version 6.0.6002]
    ::TCC Build 52  Windows Vista Build 6002  Service Pack 2
    ::--------------------------------------------------------
    @setlocal
    @echo off
    set rc=%@winapi[user32.dll,OpenClipboard,0]
    iff %rc gt 0 then
      echo Clipboard Open
      set ln=%@winapi[user32.dll,CountClipboardFormats]
      echo There are %ln clipboard formats.
      set ln=%@dec[ln]
      set uFormat=%@winapi[user32.dll,EnumClipboardFormats,0]
      echos %@format[5,%uFormat]` - `
      gosub ClipboardGetFormatName
      do kount=1 to %ln
        set uFormat=%@winapi[user32.dll,EnumClipboardFormats,%uFormat]
        echos %@format[5,%uFormat]` - `
        gosub ClipboardGetFormatName
      enddo
      set rc=%@winapi[user32.dll,CloseClipboard]
      if %rc gt 0 echo Clipboard Closed
    else
      echo Could not open Clipboard
    endiff
    endlocal
    
    quit
    
    :ClipboardGetFormatName
    switch %uFormat
    case 1
      echo CF_TEXT
    case 2
      echo CF_BITMAP
    case 3
      echo CF_METAFILEPICT
    case 4
      echo CF_SYLK
    case 5
      echo CF_DIF
    case 6
      echo CF_TIFF
    case 7
      echo CF_OEMTEXT
    case 8
      echo CF_DIB
    case 9
      echo CF_PALETTE
    case 10
      echo CF_PENDATA
    case 11
      echo CF_RIFF
    case 12
      echo CF_WAVE
    case 13
      echo CF_UNICODETEXT
    case 14
      echo CF_ENHMETAFILE
    case 15
      echo CF_HDROP
    case 16
      echo CF_LOCALE
    case 17
      echo CF_DIBV5
    case 18
      echo CF_MAX
    case %@convert[16,10,0080]
      echo CF_OWNERDISPLAY
    case %@convert[16,10,0081]
      echo CF_DSPTEXT
    case %@convert[16,10,0082]
      echo CF_DSPBITMAP
    case %@convert[16,10,0083]
      echo CF_DSPMETAFILEPICT
    case %@convert[16,10,008E]
      echo CF_DSPENHMETAFILE
    case %@convert[16,10,0200]
      echo CF_PRIVATEFIRST
    case %@convert[16,10,02FF]
      echo CF_PRIVATELAST
    case %@convert[16,10,0300]
      echo CF_GDIOBJFIRST
    case %@convert[16,10,03FF]
      echo CF_GDIOBJLAST
    case 49158
      echo FileName
    case 49159
      echo FileNameW
    case 49161
      echo DATAOBJECT
    case 49171
      echo Ole Private Data
    case 49380
      echo Shell Object Offsets
    case 49382
      echo FileContents
    case 49383
      echo FileGroupDescriptor
    case 49389
      echo Preferred DropEffect
    case 49268
      echo Shell IDList Array
    case 49619
      echo RenPrivateFileAttachments
    default
      echo UNKNOWN
    endswitch
    return
     
    #1 Joe Caverly, Oct 15, 2013
    Last edited: Oct 15, 2013
  2. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,191
    Likes Received:
    11
    Now that's pretty nifty.
     
  3. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Code is nice, action nifty, but the XenForo website makes it difficult to highlight juist the code for copy-and-paste. It report did surprise me, multiple content types at the same time. I just did echo abc > clip: and got a list of four formats: OEMTEXT, LOCALE, TEXT and UNICODETEXT. My TCC is running in its own window, with UnicodeOutput=No. Can someone explain?
     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,311
    Likes Received:
    39
    The clipboard can actually do some translation automatically. You can copy ASCII text onto it and then read it back as Unicode, or vice versa. (Or put a bitmap on the clipboard and then read it as a DIB, though that isn't relevant here.)
     
  5. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    From msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx
    (this link returns "Content Not Found". Use link at bottom of message.)

    Multiple Clipboard Formats
    A window can place more than one clipboard object on the clipboard, each representing the same information in a different clipboard format. When placing information on the clipboard, the window should provide data in as many formats as possible. To find out how many formats are currently used on the clipboard, call the CountClipboardFormats function.

    Clipboard formats that contain the most information should be placed on the clipboard first, followed by less descriptive formats. A window pasting information from the clipboard typically retrieves a clipboard object in the first format it recognizes. Because clipboard formats are enumerated in the order they are placed on the clipboard, the first recognized format is also the most descriptive.

    For example, suppose a user copies styled text from a word-processing document. The window containing the document might first place data on the clipboard in a registered format, such as RTF. Subsequently, the window would place data on the clipboard in a less descriptive format, such as text (CF_TEXT).

    When the content of the clipboard is pasted into another window, the window retrieves data in the most descriptive format it recognizes. If the window recognizes RTF, the corresponding data is pasted into the document. Otherwise, the text data is pasted into the document and the formatting information is lost.

    Addendum: Not sure why the link does not want to work from this message, so here is a plain text version that you can copy-and-paste;

    msdn.microsoft.com/en-us/library/windows/desktop/ms649013(v=vs.85).aspx

    Joe
     
    #5 Joe Caverly, Oct 15, 2013
    Last edited: Oct 15, 2013
  6. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Thanks, Charles and Joe!

    It does seem a bit of waste for all those translations be be done automatically, whether or not utilized, though most clipboard operations are manually triggered, and thus not very frequent.
     

Share This Page