emendelson

Copy two different formats to the clipboard?

35 posts in this topic

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.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Oops.

Sorry

Edited by mLipok

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

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?


Spoiler

Paster - Main function is to paste text, but has more functions.

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Share this post


Link to post
Share on other sites

#4 ·  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
1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  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

Share this post


Link to post
Share on other sites
55 minutes ago, 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.

 

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

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites
30 minutes ago, 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.

try to use this concept:

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

 

1 person likes this

Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites
27 minutes ago, mLipok said:

try to use this concept:

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

 

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.

Share this post


Link to post
Share on other sites

Where you ready about this limit ?


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

#12 ·  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

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#13 ·  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

Share this post


Link to post
Share on other sites

and if you use

$rtfhandle = FileOpen($outfile, 128)

?


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

10 minutes ago, iamtheky said:

and if you use

$rtfhandle = FileOpen($outfile, 128)

?

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

Share this post


Link to post
Share on other sites

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?


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

13 minutes ago, 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?

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

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Also, to clarify

18 hours ago, 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?

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

Share this post


Link to post
Share on other sites

#19 ·  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

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#20 ·  Posted

4 hours ago, iamtheky said:

Am still hunting for an optimal solution though

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.

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