Search code examples
sizeclipboardautohotkeyclipboarddata

Autohotkey: how to determine clipboard buffer contents size


I have a script that scrapes a certain portion of the screen and acquires about 100-200 bytes long text in to the clipboard. Sometimes, due to web server timeout or missing CSS definitions etc, the page doesn't render correctly and the sane mouse drag, selects a much larger amount of text and copies it to the clipboard.

I want to be able to notice this situation and scrap the clipboard contents and run my script again, till it is at the expected size of 100-200 bytes. Finally abort the script if large buffer keeps happening for certain number of times.

I have the logic for it but only thing I am not able to figure out the clipboard size and how to get it inside AHK script. Is there a predefined variable for it ? Or is there another, more complicated method ? What comes to mind is to paste the contents into notepad and save it. Then look at the file size, but it is very convoluted. I want something without a disk write operation.

Any ideas ? I saw the strlen command but not sure how to use it...


Solution

  • If it is text in your clipboard this works:

    ClipSize := strlen(clipboard)
    

    Example Script

    sClip:=GetClipboardSize(Clipboard)
    sClip_a:=GetClipboardSize(ClipboardAll)
    
    MsgBox Clipboard Size: %sClip%`nClipboardAll Size: %sClip_a%
    
    GetClipboardSize(c) {
        if (!s:=strlen(c)) {
            tmp:=A_temp "\clipboardsize_" A_TickCount "~~tempfile~~.tmp"
            FileDelete,%tmp%
            FileAppend,%ClipboardAll%,%tmp%
            FileGetSize,s,%tmp%
            FileDelete,%tmp%
        }
        return s
    }