Sign in to follow this  
Followers 0

Copy/Paste from web page

9 posts in this topic

Posted

Suppose you select part of a web page that includes images and texts and copy it into the clipboad by Ctrl+C; pasting it into a compose windows of Thunderbird results in having all images and texts together in thunderbird where the images are loaded from somewhere in the web.

I'd like to simulate this process using Autoit using the following code, but it just shows the text and not the images in the compose windows of Thunderbird. I am wondering if there is anyway to simulate the exact operation of manually doing this task by Ctrl+C and then Ctrl+V?

Send("^c")
Sleep(200)
$Clipboard = ClipGet()
Sleep(200)
$Clipboard="-compose " & '"' & "body=" & "'" & $Clipboard & "'" & '"'
Run("c:\Program Files\Mozilla Thunderbird\thunderbird.exe " & $Clipboard , "", "")

Thanks in advance

Share this post


Link to post
Share on other sites



Posted

Clipget returns the text content of the clipboard, whereas you want the html content.

To do this you will need to use the _Clipboard_* library functions with CF_HTML format (note that CF_HTML is not a constant, you have to use RegisterClipboardFormat first).

Local $CF_HTML = _ClipBoard_RegisterFormat("HTML Format") ; Gets the HTML format identifier
MsgBox(0, "test", _ClipBoard_GetData($CF_HTML))

Unfortunately I can't test at the moment, but IIRC there is a header (i.e. you don't just get the HTML, you get some meta information). You may need to parse this before sending the html to thunderbird. Also be careful of newlines and quotes in the html... These can cause all sorts of problems with the run command.

Share this post


Link to post
Share on other sites

Posted

many thanks for your reply, but I coudn't make it working! I google the functions, but couldn't find any sample. would you please give me more description?

the function just returs lots of numbers! I also tried the code in this topic, but with no luck!:

btw, sorry for the late reply. I haven't received any notification regarding a reply.

thanks

Share this post


Link to post
Share on other sites

Posted

_Clipboard_GetData returns binary I think... Still can't test, but try `BinaryToString(_ClipBoard_GetData($CF_HTML))` and see if that turns the numbers you were getting to text.

Share this post


Link to post
Share on other sites

Posted

thanks for the reply.

It works but it is similar to Clipget() and just carries the text and not the images.

any solution?

thanks

Share this post


Link to post
Share on other sites

Posted

Meh. Did some testing and ended up going the long way round:

#include <ClipBoard.au3>

If (Not _ClipBoard_Open(0)) Then
    ConsoleWriteError("_ClipBoard_Open failed" & @LF)
Else
    Local $sData = _Clipboard_GetHTML()
    Switch @error
        Case 1
            ConsoleWriteError("Could not register CF_HTML format" & @LF)
        Case 2
            ConsoleWriteError("Clipboard data not available in HTML format" & @LF)
        Case 3
            ConsoleWriteError("Invalid HTML format header (missing end or start fragment)" & @LF)
        Case 0
            ConsoleWrite($sData & @LF)
    EndSwitch
EndIf

_ClipBoard_Close()



; #FUNCTION# ====================================================================================================================
; Name ..........: _Clipboard_GetHTML
; Description ...: Retrieves the data from the clipboard in HTML format
; Syntax ........: _Clipboard_GetHTML( )
; Parameters ....: None
; Return values .: Success: Returns the data from the clipbard as html.
;                  Failure: Returns an empty string and sets @error:
;                           | 1 - Could not register CF_HTML format
;                           | 2 - Clipboard data not available in HTML format
;                           | 3 - Invalid HTML format header (missing end or start fragment)
; Author ........: Matt Diesel (Mat)
; Modified ......:
; Remarks .......: This assumes that the clipboard is already open. The _Clipboard_Open function should be called first.
; Related .......:
; Link ..........:
; Example .......: Above.
; ===============================================================================================================================
Func _Clipboard_GetHTML()
    Local $CF_HTML, $pHTML, $tHeader, $asHeader, $asLine, $iStartFragment = -1, $iEndFragment = -1

    ; Register the CF_HTML format
    $CF_HTML = _ClipBoard_RegisterFormat("HTML Format")

    If (Not $CF_HTML) Then Return SetError(1, 0, "") ; Could not register CF_HTML format
    If (Not _ClipBoard_IsFormatAvailable($CF_HTML)) Then Return SetError(2, 0, "") ; Clipboard data not available in HTML format

    $pHTML = _ClipBoard_GetDataEx($CF_HTML)

    $tHeader = DllStructCreate("char t[104]", $pHTML)
    $asHeader = StringSplit(DllStructGetData($tHeader, "t"), @CRLF, 1)

    ; Find start and end of fragment from header.
    For $i = 1 To $asHeader[0]
        $asLine = StringSplit($asHeader[$i], ":")

        Switch $asLine[1]
            Case "StartFragment"
                $iStartFragment = Int($asLine[2])
            Case "EndFragment"
                $iEndFragment = Int($asLine[2])
        EndSwitch
    Next

    If (($iEndFragment < 0) Or ($iStartFragment < 0)) Then Return SetError(3, 0, "") ; Invalid HTML format header (missing end or start fragment)

    $tHeader = DllStructCreate("char t[" & $iEndFragment - $iStartFragment + 1 & "]", $pHTML + $iStartFragment)
    Return DllStructGetData($tHeader, "t")
EndFunc   ;==>_Clipboard_GetHTML

Good news for you is that you just need to copy _Clipboard_GetHTML and use it. How that deals with images I'm not sure, and it is almost certainly browser dependant.

Share this post


Link to post
Share on other sites

Posted

wwooww! so long code!

thank you very much.

it partially works. I mean if I just select an image in a web page, it copy/paste the image, but it will also paste a "<" character after the image.

I also tried it with a bit more complicated page like google search, but it just paste a dot instead of all copied items in cliboard.

it should be noted that I am passing $sData to Thunderbird using the following code:

$Clipboard="-compose " & '"' & "body=" & $sData & '"'
msgbox(0,"","c:Program FilesMozilla Thunderbirdthunderbird.exe " & $Clipboard)

I reckon here is the problem and not your code as it changes the $sData to text and passes it to thunderbird! I am wondering if you have other way of passing the html data from $sData to the body part of Thunderbird compose windows?

Thanks

Share this post


Link to post
Share on other sites

Posted

A single character? I think I know what that could be... Try changing the second last line of the function, in particular remove the +1 I added out of habbit.

"char t[" & $iEndFragment - $iStartFragment + 1 & "]"

Should probably be:

"char t[" & $iEndFragment - $iStartFragment & "]"

You shouldn't have to add one because there is no terminating null anyway, and we are only reading. ;)

Share this post


Link to post
Share on other sites

Posted

thanks for your reply, and sorry again for my late replay!

I have requested a reply notofication for this topic, but I never received any notification for the replies!! and I have to check this page to see if there is any reply!

anyway, as I explained in my pre, the main problem is that I am passing $sData to Thunderbird using the following code:

$Clipboard="-compose " & '"' & "body=" & $sData & '"'
msgbox(0,"","c:Program FilesMozilla Thunderbirdthunderbird.exe " & $Clipboard)

as it changes the $sData to text and passes it to thunderbird! I am wondering if you have other way of passing the html data from $sData to the body part of Thunderbird compose windows?

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0