Jump to content

Recommended Posts

Posted

In another thread -

-  I got the answer to the question, how to copy RTF data to the Windows clipboard? Now I have another question, and I think the answer is that I'm trying to do something impossible, but here's the question anyway:

I have two files, one in RTF format, the other in plain text format; both were created by a converter program that converts WordPerfect documents to RTF, text, etc. Can I copy both files to the clipboard, one in RTF format, the other in text format, so that if I then paste the clipboard to a text editor, the plain text version will get pasted, while if I paste the clipboard to an RTF-aware application like Word, the formatted text version will get pasted?

I don't think this is possible, but this forum has plenty of members who can do things that I always thought were impossible, so I thought it would be worth the trouble to ask.

Posted (edited)

Oops.

Sorry

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

What if you make it so that on a press of a hotkey, the script creates a txt file, and pastes the data there, and at the press of another button, it reads and pastes at cursor?

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted (edited)

It is possible to put multiple types in clipboard.

If you want to see an example of multiple types, run the example from _ClipBoard_EnumFormats and copy something from somewhere like Firefox.

Here is a small example

#include <Clipboard.au3>

_ClipBoard_Open(0)
_ClipBoard_Empty()
$iFormat = _ClipBoard_RegisterFormat("Rich Text Format")
$sRTF = FileRead("Document.rtf")
$t=DllStructCreate("WCHAR["&StringLen($sRTF)&"]")
$hT = DllStructGetPtr($t)
DllStructSetData($t, 1, $sRTF)
_ClipBoard_SetDataEx($hT, $iFormat)
$sUnicode = "Document.rtf"
$t2 = DllStructCreate("WCHAR["&StringLen($sUnicode)&"]")
$hT2 = DllStructGetPtr($t2)
DllStructSetData($t2, 1, $sUnicode)
_ClipBoard_SetDataEx($hT2, $CF_UNICODETEXT)
_ClipBoard_Close()

 

Edited by genius257
Posted (edited)

Thank you for this, but I'm afraid I can't make it work. The RTF data gets pasted with underscores between each character (as if there were some confusion between unicode text and ASCII text), and the plain text doesn't get copied. I tried modifying it with CHAR instead of WCHAR, but that didn't help.

I've tried this with an RTF file created by a DOS application (therefore in raw ASCII text), and also with an RTF creted by Word 2016, but neither one worked. Don't take time over this - I'll continue to experiment.

PS: Careca, thank you for the suggestion, but I'm trying to write a script that saves an RTF file to the clipboard without opening it in any application.

 

Edited by emendelson
Posted
  On 12/31/2016 at 4:14 AM, emendelson said:

Thank you for this, but I'm afraid I can't make it work. The RTF data gets pasted with underscores between each character (as if there were some confusion between unicode text and ASCII text), and the plain text doesn't get copied. I tried modifying it with CHAR instead of WCHAR, but that didn't help.

I've tried this with an RTF file created by a DOS application (therefore in raw ASCII text), and also with an RTF creted by Word 2016, but neither one worked. Don't take time over this - I'll continue to experiment.

PS: Careca, thank you for the suggestion, but I'm trying to write a script that saves an RTF file to the clipboard without opening it in any application.

 

Expand  

my only suggestion would be convert string to binary and use BYTE instead

Posted

I'll give that a try, but meanwhile, I've solved the problem with a kludgy workaround - I open the RTF file with WordPad in a hidden window, send Ctrl-A to select all the contents, Ctrl-C to copy it to the clipboard, and Alt-F4 to close the window. The clipboard gets RTF and text data, and I don't have to create two files, one text, the other RTF, to get the same result.

But I'll definitely try your suggestion and will report back if it succeeds.

 

Posted
  On 12/31/2016 at 3:40 PM, emendelson said:

meanwhile, I've solved the problem with a kludgy workaround - I open the RTF file with WordPad in a hidden window, send Ctrl-A to select all the contents, Ctrl-C to copy it to the clipboard, and Alt-F4 to close the window.

Expand  

try to use this concept:

Func _RTF_to_CLIPBOARD($sRTF_FileFullPath)
    _GUICtrlRichEdit_Create(...
    _GUICtrlRichEdit_StreamFromFile(...
    _GUICtrlRichEdit_SetSel(...
    _GUICtrlRichEdit_Copy(...
    _GUICtrlRichEdit_Destroy(...
EndFunc

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 12/31/2016 at 4:11 PM, mLipok said:

try to use this concept:

Func _RTF_to_CLIPBOARD($sRTF_FileFullPath)
    _GUICtrlRichEdit_Create(...
    _GUICtrlRichEdit_StreamFromFile(...
    _GUICtrlRichEdit_SetSel(...
    _GUICtrlRichEdit_Copy(...
    _GUICtrlRichEdit_Destroy(...
EndFunc

 

Expand  

Thank you - I think that should work - will try it when I get back to my machine. But I think it will raise one issue: this script is designed to work with documents of any length, and _GUICtrlRichEdit_StreamFromFile has a limit that has to be changed with _GUICtrlRichEdit_SetLimit. I suppose I could set it to something like 1,000,000 characters but I'll have to experiment to see whether this is actually feasible. But it certainly seems practical - provided that it copies both RTF and text format to the clipboard.

Posted

Where you ready about this limit ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

I don't understand the OP.

If you ClipPut RTF data, the Rich Text Formatting is abandoned when you ClipGet it to a plain text editor.  So there is no need to maintain a plain text copy, because those are the only parts that make the trip when you paste outside of an rtf editor.

Is there another behavior I am missing?

Edited by iamtheky

  Reveal hidden contents

Posted (edited)

I'm not using ClipGet to retrieve the data that I put into the clipboard. I'm using a script to put data into the clipboard, so that I can press Ctrl-V when running an application and paste the data into the application. ClipGet is not involved anywhere.

Try these two scripts. The first uses ClipPut, and the clipboard interprets the data as raw RTF code, not formatted text:

$outfile = @ScriptDir & "\document.rtf"
$rtfhandle = FileOpen($outfile)
$convtext = FileRead($rtfhandle)
FileClose($rtfhandle)
$ret = ClipPut($convtext)

The second script explicitly copies the data into the clipboard in RTF format, and it pastes correctly into Word, etc., but nothing gets pasted into Notepad, UltraEdit., etc. It appears that the clipboard needs to get data copied to it in both Text and RTF formats if you want to be able to paste into a text editor and a word-processor.

#include <clipboard.au3>
$outfile = @ScriptDir & "\document.rtf"
$rtfhandle = FileOpen($outfile)
$convtext = FileRead($rtfhandle)
FileClose($rtfhandle)
$clipFmt = _ClipBoard_RegisterFormat("Rich Text Format")
$ret = _ClipBoard_SetData($convtext, $clipFmt)

That's why I asked the question. I didn't expect this behavior, but that's what occurs.

Of course, maybe I'm doing everything wrong, in which I hope someone will show me what I should be doing instead.

Edited by emendelson
Posted

and if you use

$rtfhandle = FileOpen($outfile, 128)

?

  Reveal hidden contents

Posted (edited)
  On 12/31/2016 at 9:59 PM, iamtheky said:

and if you use

$rtfhandle = FileOpen($outfile, 128)

?

Expand  

Using the first script (with ClipPut), the raw RTF data gets pasted into both text editors and word-processors, not the plain text or the formatted text. Using the second script (with the advanced Clipboard functions), nothing gets pasted into a text editor, and the formatted text is correctly pasted into word processors (exactly the same behavior as when the ", 128" is omitted).

Edited by emendelson
Posted

excellent, so why not selectively put if you can reliably place both on the clipboard.  Is the source long gone by the time you paste it?

  Reveal hidden contents

Posted (edited)
  On 12/31/2016 at 10:12 PM, iamtheky said:

excellent, so why not selectively put if you can reliably place both on the clipboard.  Is the source long gone by the time you paste it?

Expand  

The source is long gone by the time the data gets pasted. The script (which works perfectly well with the WordPad->clipboard workaround) does this: it's designed for people who have old WordPerfect files and want to use the content in something else, even if they don't have WordPerfect. The user selects a WordPerfect file, the script runs the vDos DOS emulator, which in turn runs an old DOS-based WordPerfect WP->RTF conversion utility to create a temporary RTF file from the WordPerfect file; the script then copies the contents of the temporary RTF file to the clipboard, deletes the temporary file, and exits. Maybe an hour later, the user pastes the clipboard contents into something - a mail message, a word-processor, etc., etc.

Edited by emendelson
Posted (edited)

Also, to clarify

  On 12/31/2016 at 9:06 PM, iamtheky said:

If you ClipPut RTF data, the Rich Text Formatting is abandoned when you ClipGet it to a plain text editor.  So there is no need to maintain a plain text copy, because those are the only parts that make the trip when you paste outside of an rtf editor.

Is there another behavior I am missing?

Expand  

To clarify this: ClipGet doesn't "abandon Rich Text Formatting." What happens is this. When you copy data to the clipboard from a browser or word-processor, the browser puts multiple formats into to the clipboard - typically text, unicode text, RTF, and a few more. You can use _Clipboard_Enum_Formats to see what formats are in the clipboard at any time. When you use ClipGet, it retrieves the text format (if it's already in the clipboard); ClipGet doesn't "abandon" the other formats; it simply doesn't get them. And if the text format has not been explicitly put into the clipboard, then there's no way for ClipGet to get it, because it doesn't exist.

Edited by emendelson
Posted (edited)

 

I meant abandon, as in to leave behind.  ClipGet only retrieves the format that is suitable (though I have been playing and cant determine if the default is CF_text or some kind of auto selection) and abandons the others (those formats are not present in the data that you pasted into notepad). 

edit:  I am also liking the way ClipPutFile behaves in word, where local links are pasted in as images, if the email client you are describing is outlook you may get that same behavior.  Am still hunting for an optimal solution though

It seems as though you may be defending that ClipPut fills the clipboard with formatting information, and I agree with that.  Apologies if that seemed in dispute.

Edited by iamtheky

  Reveal hidden contents

Posted
  On 1/1/2017 at 3:46 PM, iamtheky said:

Am still hunting for an optimal solution though

Expand  

Thank you. My workaround using WordPad really isn't good enough, because WordPad doesn't display footnotes and other features that are supported by the RTF format. I'm still looking for a way to put RTF and Text into the clipboard at the same time, taking them from different files.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...