# Incompatible encoding from drag and drop?

**URL:** <https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021>\
**Category:** wxRuby\
**Created:** [March 4, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021 "2009-03-04T23:40:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 4, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/1 "2009-03-04T23:40:28Z")

</div>

Hi all,

I’m trying to get a little app running which receives filenames via drag  
and drop. While it mostly runs fine, when I’m dropping a file with e.g.  
german umlauts or french accents (Ã¨, Ã©), I’m not able to open the file  
given by the droptarget-class. Seemingly the special characters get  
corrupted.

See the small attached program to reproduce my problem. I condensed it  
to the absolute minimum to evoke the error.

Thanks in advance for every hint and forgive me if that’s a noobs  
question!

Cheers,  
Raskolnikov.

ruby 1.8.6  
wxruby (2.0.0, 1.9.9)

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 5, 2009, 12:44am UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/2 "2009-03-05T00:44:31Z")

</div>

Christian Schmidt wrote:

> I’m trying to get a little app running which receives filenames via drag  
> and drop. While it mostly runs fine, when I’m dropping a file with e.g.  
> german umlauts or french accents (Ã¨, Ã©), I’m not able to open the file  
> given by the droptarget-class. Seemingly the special characters get  
> corrupted.

You need to be sure that the text content of the file you’re dropping in  
is in UTF-8 encoding. You should be able to set this in a text editor.

If the text isn’t in UTF-8, you can read it then convert it to UTF-8  
using Ruby’s iconv library.

> See the small attached program to reproduce my problem. I condensed it  
> to the absolute minimum to evoke the error.

Also, you should return true or false from on\_drop\_files, to signal to  
wxRuby whether or not the drop files action succeeded.

hth  
alex

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 5, 2009, 10:14am UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/3 "2009-03-05T10:14:36Z")

</div>

Hi Alex,

thank you for the reply!

The Problem isn’t reading the file but opening it…! The program throws  
an exception if, for instance, you try to drop the file in the attached  
zip onto it.

My program will read in mp3-files and examine them with id3lib-ruby, so  
I don’t have influence on the file’s contents anyway.

Thanks in advance,  
Christian.

Alex F. wrote:

> Christian Schmidt wrote:
> 
> > I’m trying to get a little app running which receives filenames via drag  
> > and drop. While it mostly runs fine, when I’m dropping a file with e.g.  
> > german umlauts or french accents (Ã¨, Ã©), I’m not able to open the file  
> > given by the droptarget-class. Seemingly the special characters get  
> > corrupted.
> 
> You need to be sure that the text content of the file you’re dropping in  
> is in UTF-8 encoding. You should be able to set this in a text editor.
> 
> If the text isn’t in UTF-8, you can read it then convert it to UTF-8  
> using Ruby’s iconv library.
> 
> > See the small attached program to reproduce my problem. I condensed it  
> > to the absolute minimum to evoke the error.
> 
> Also, you should return true or false from on\_drop\_files, to signal to  
> wxRuby whether or not the drop files action succeeded.
> 
> hth  
> alex

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 5, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/4 "2009-03-05T18:05:36Z")

</div>

Christian Schmidt wrote:

> The Problem isn’t reading the file but opening it…! The program throws  
> an exception if, for instance, you try to drop the file in the attached  
> zip onto it.

OK - it helps if you post the error message you get so we can see what’s  
going wrong.

I have a file whose name has accented characters, and it works fine for  
me (Ruby 1.8, 1.9, OS X, wxRuby 2.0.0). Can you say what platform and  
version of wxRuby you’re using?

alex

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 6, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/5 "2009-03-06T16:57:49Z")

</div>

Christian Schmidt wrote:

> it really works on your box?

Yes, the reason being that in OS X, file names are encoded in UTF-8, and  
this is the encoding of the string file name that wxRuby returns.

> Ok, on my system, when I use the example file, it throws:
> 
> “No such file or directory - C:\Ã¶Ã¶Ã¤Ã¨Ã©.txt”
> 
> I’m working on XP-Home, SP3. German locales  
> ruby -v: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

On Windows, file names aren’t encoded in UTF-8, but in a local encoding  
(for me, and probably you, CP1252). So the filename returned by wxRuby  
doesn’t exist.

I think we should fix this so that wxRuby methods that accept or return  
file paths return those strings in an encoding that can be passed  
straight to Ruby’s File/Dir methods.

In the meantime, you can work around this by manually converting the  
filepaths to the correct encoding before opening them:

require ‘iconv’ # at the top of your script

files.each do | file |  
if Wx::PLATFORM == ‘WXMSW’  
file = Iconv.conv(‘CP1252’, ‘UTF-8’, file)  
end  
… process file as normal  
end

alex

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 7, 2009, 11:18am UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/6 "2009-03-07T11:18:55Z")

</div>

Hi Alex,

thanks a lot! It works like a charm.

Have a nice weekend,  
Christian.

Alex F. wrote:

> Christian Schmidt wrote:
> 
> > it really works on your box?
> 
> Yes, the reason being that in OS X, file names are encoded in UTF-8, and  
> this is the encoding of the string file name that wxRuby returns.
> 
> > Ok, on my system, when I use the example file, it throws:
> > 
> > “No such file or directory - C:\Ã¶Ã¶Ã¤Ã¨Ã©.txt”
> > 
> > I’m working on XP-Home, SP3. German locales  
> > ruby -v: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
> 
> On Windows, file names aren’t encoded in UTF-8, but in a local encoding  
> (for me, and probably you, CP1252). So the filename returned by wxRuby  
> doesn’t exist.
> 
> I think we should fix this so that wxRuby methods that accept or return  
> file paths return those strings in an encoding that can be passed  
> straight to Ruby’s File/Dir methods.
> 
> In the meantime, you can work around this by manually converting the  
> filepaths to the correct encoding before opening them:
> 
> require ‘iconv’ # at the top of your script
> 
> files.each do | file |  
> if Wx::PLATFORM == ‘WXMSW’  
> file = Iconv.conv(‘CP1252’, ‘UTF-8’, file)  
> end  
> … process file as normal  
> end
> 
> alex

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 5, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/incompatible-encoding-from-drag-and-drop/162021/7 "2009-03-05T20:17:21Z")

</div>

Hi again,

it really works on your box?  
Ok, on my system, when I use the example file, it throws:

“No such file or directory - C:\Ã¶Ã¶Ã¤Ã¨Ã©.txt”

I’m working on XP-Home, SP3. German locales  
ruby -v: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]  
gem list wxruby: wxruby (2.0.0, 1.9.9)

Looking forward to your answer,  
Christian.

Alex F. wrote:

> Christian Schmidt wrote:
> 
> > The Problem isn’t reading the file but opening it…! The program throws  
> > an exception if, for instance, you try to drop the file in the attached  
> > zip onto it.
> 
> OK - it helps if you post the error message you get so we can see what’s  
> going wrong.
> 
> I have a file whose name has accented characters, and it works fine for  
> me (Ruby 1.8, 1.9, OS X, wxRuby 2.0.0). Can you say what platform and  
> version of wxRuby you’re using?
> 
> alex
