I'm accessing the Windows clipboard using twapi.
When the text in the clipboard was copied from an MS Excel cell, then
text contains some new line char that I could not identify and thus
unable to remove it using
regsub {[\r\n]+$} $text ""
What other way are there besides "string trim" to remove new line chars?
Many thanks
Alexandru
--
I'm accessing the Windows clipboard using twapi.
When the text in the clipboard was copied from an MS Excel cell, then
text contains some new line char that I could not identify and thus
unable to remove it using
regsub {[\r\n]+$} $text ""
What other way are there besides "string trim" to remove new line chars?
Many thanks
Alexandru
--
Well, that's what my orginal reexp does. No effect.
regsub {[\r\n]+$} $text ""
string map "\n {} \r {}" $text
Thanks Ralf.
I tried:
format %x [scan [string index $text end] %c]
but it only identifies 0 as last char.
both-mike
set text [regsub {[[:space:]]+$} $text ""]
and
set text [regsub {[\s]+$} $text ""]
made no difference...
--
Harold suggested:
string map "\n {} \r {}" $text
This deletes every occurrence of \n and every occurrence of \r,
anywhere in the string.
Which is very different.
so, does not
regsub {\x00$} $text {} text_sans_nul
do what you want?
-mike
I'm accessing the Windows clipboard using twapi.Could this be an encoding issue? I remember that the default output from
When the text in the clipboard was copied from an MS Excel cell, then
text contains some new line char that I could not identify and thus
unable to remove it using
regsub {[\r\n]+$} $text ""
What other way are there besides "string trim" to remove new line chars?
Many thanks
Alexandru
--
Hi Alexandru,
please allow me to share this wish 8.6.14 32 bit session:
Excel 2013 with a cell with the content "AÄ".
AÄ
% clipboard get
AÄ
% lmap i [split $c ""] {scan $i %c}
65 196 10
% string trimright $c "\x0a"
AÄ
%
So, "string trimright \x0a" does the job for me.
Take care,
Harald
Here is the code.
Can anywone test on Windows with Excel?
The test procedure is:
Open an Excel table with data an copy one single cell.
In the Tcl/Tk console execute the code below.
See how the copied text is output to the window.
In my case there is an additional line under the copied text, meaning a
new line was still created.
package require twapi
proc ::ClipboardGetText {} {
::twapi::open_clipboard
set text [::twapi::read_clipboard 1]
::twapi::close_clipboard
return $text
}
proc ::ClipboardSetText {text} {
# Write clipboard content
::twapi::open_clipboard
::twapi::empty_clipboard
::twapi::write_clipboard_text $text
::twapi::close_clipboard
}
## Remove line breaks at the end of string (mostly to avoid issues when copying from Excel)
proc ::test {} {
set text [::ClipboardGetText]
puts [format %x [scan [string index $text end] %c]]
set text [regsub {[\r\n]+$} $text ""]
set text [regsub {[[:space:]]+$} $text ""]
set text [regsub {[\s]+$} $text ""]
set text [regsub {\x0$} $text ""]
# Do not use "trim" since we want to preserve trailing white spaces
but remove new line char
puts $text
}
::test
--
I tried
set text [encoding convertfrom cp1252 $text]
but no success.
alexandru <alexandru.dadalau@meshparts.de> wrote:
I tried
set text [encoding convertfrom cp1252 $text]
but no success.
Are you sure the clipboard is cp1252?
If it is utf-8 then converting as if it is cp1252 will create strange results.
Am 20.03.2025 um 19:03 schrieb Rich:
alexandru <alexandru.dadalau@meshparts.de> wrote:
I tried
set text [encoding convertfrom cp1252 $text]
but no success.
Are you sure the clipboard is cp1252?
If it is utf-8 then converting as if it is cp1252 will create strange
results.
Dear Rich,
the Windows clipboard is more complicated/sophisticated.
Each entry has a type. This is nowdays mostly CF_UNICODETEXT, which is
16 bit unicode.
A sending application can put an entry into the clipboard in multiple formats and the receiving application can pick one or more.
We have often "enhanced" this code in TCL to adopt it to modern
usage. There should not be any issues with the encoding, expect that
the sending application does anything strange.
I wasn't able to respond until now.
Currently I'm happy with the workaround using "clipboard get" instead of twapi equivalent command.
Stil one might suspect a bug in twapi. I would expect that twapi can
best handle the Windows clipboard.
Harald Oehlmann <wortkarg3@yahoo.com> wrote:
Am 20.03.2025 um 19:03 schrieb Rich:
alexandru <alexandru.dadalau@meshparts.de> wrote:
I tried
set text [encoding convertfrom cp1252 $text]
but no success.
Are you sure the clipboard is cp1252?
If it is utf-8 then converting as if it is cp1252 will create strange
results.
Dear Rich,
the Windows clipboard is more complicated/sophisticated.
Yes, that I am well aware.
Each entry has a type. This is nowdays mostly CF_UNICODETEXT, which is
16 bit unicode.
A sending application can put an entry into the clipboard in multiple
formats and the receiving application can pick one or more.
Yes, but alexandru's example he posted, he's not picking a format from
the clipboard, he's assuming it was cp1252 and forcefully decoding it
as if it was cp1252. If he left out telling us he requested cp1252
from the clipboard then that's on him.
We have often "enhanced" this code in TCL to adopt it to modern
usage. There should not be any issues with the encoding, expect that
the sending application does anything strange.
This is good, because handling the clipboard on windows is an ugly
beast. My comment was to what alexandru posted showing simply forcing
a decode as if what he got was cp1252, without showing he asked the
clipboard to supply cp1252 data.
Am 21.03.2025 um 21:15 schrieb alexandru:
I wasn't able to respond until now.
Currently I'm happy with the workaround using "clipboard get" instead of
twapi equivalent command.
Stil one might suspect a bug in twapi. I would expect that twapi can
best handle the Windows clipboard.
TWAPI gives you more control.
But you should know what you do.
In your use case (text transmission), the native clipboard code is IMHO
best suited.
If you want to do enhanced things (transmit images, whatever), use TWAPI.
Take care,
Harald
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,028 |
Nodes: | 10 (1 / 9) |
Uptime: | 125:17:50 |
Calls: | 13,329 |
Calls today: | 1 |
Files: | 186,574 |
D/L today: |
369 files (85,914K bytes) |
Messages: | 3,355,124 |