Shell output yields incorrect character

I’m reading the output of a command line application via the shell class, but having a problem when that output contains extended characters.

The specific example that triggered this issue is the filename:

Fun., Janelle Monáe - We Are Young (feat. Janelle Monáe).mp3

If I run the command in the command prompt, the filename looks just as it does above. But reading the data from .ReadAll yields the following:

Fun., Janelle Monße - We Are Young (feat. Janelle Monße).mp3

I have tried using .ConvertEncoding(Encodings.UTF8) without success. What’s interesting is that the Unicode á character code is 225, which is the same as the ASCII code for ß. So it seems this may be some Unicode/ASCII mix-up.

Any ideas on how to properly import the string so that I can get the correct character?

Edit: I should mention on MacOS it works fine. It’s only on Windows that I have this character issue.

Figured it out.

In .DataAvailable:

var lineData as String
#If TargetMacOS Then
  lineData = Me.ReadAll
#ElseIf TargetWindows
  lineData = Me.ReadAll.DefineEncoding(encodings.WindowsANSI)
#endif

Sorry to spam the forum, but maybe this will help someone else in the future.

I think it uses the current Windows code page so it might not always be WindowsANSI.

Oof. Thanks for that. Is there a way to detect what it’s using?

There might be something in the MBS plugins.

Alternatively, you could maybe execute something in the shell that outputs a known set of characters. Your Xojo code could then run through the different encodings until the characters match.

Revised solution:

When calling Shell.Execute, assuming CmdLineToExecute is a string containing your command line:

Shell1.Execute If(TargetWindows, "chcp 437 & ", "") + CmdLineToExecute

Then in Shell1.DataAvailable:

var lineData as String
#If TargetMacOS Then
  lineData = Me.ReadAll
#ElseIf TargetWindows
  lineData = Me.ReadAll.DefineEncoding(encodings.WindowsANSI)
#endif

This works, but I think you should consider also using DefineEncoding on the Mac string as well (unless it already has a non-Nil encoding). I believe it should be set to UTF-8.

Also - I’m a little surprised that I can’t find a way in Xojo to get the operating system’s default text encoding. You could then avoid all the platform-specific code and just do:

linedata=Me.ReadAll.DefineEncoding(Encodings.SystemDefault)
1 Like