It is currently March 29th, 2024, 10:08 am

RunCommand help needed with Unicode output

Get help with installing and using Rainmeter.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

RunCommand help needed with Unicode output

Post by SilverAzide »

I'm trying to use a Powershell command line to get localized month names from Windows. For some reason, I cannot get the RunCommand plugin to return non-ANSI values, despite specifically using the UTF-16 option.

Here is a specific example. At a command (CMD) prompt, the following command:

Code: Select all

powershell -NonInteractive -Command "(([cultureinfo]::currentculture='ru-RU').DateTimeFormat.AbbreviatedMonthNames[0..11]) -Join '|'"
Returns this, as expected:
янв|фев|мар|апр|май|июн|июл|авг|сен|окт|ноя|дек

However, the following skin returns gibberish...

Code: Select all

[Rainmeter]
AccurateText=1
DynamicWindowSize=1
Update=1000
OnRefreshAction=[!CommandMeasure MeasureRunGetMonthNames "Run"]

[MeasureRunGetMonthNames]
Measure=Plugin
Plugin=RunCommand
Parameter="powershell -NonInteractive -Command "(([cultureinfo]::currentculture = 'ru-RU').DateTimeFormat.AbbreviatedMonthNames[0..11]) -Join '|'""
OutputType=ANSI
;OutputType=UTF-16
State=Hide
Timeout=2000
Substitute="#CRLF#":""

[MeterText]
Meter=String
SolidColor=0,0,0,255
FontColor=255,255,255,255
FontSize=12
Padding=8,8,8,8
X=0
Y=0
Text="Months: [MeasureRunGetMonthNames]"
DynamicVariables=1
With ANSI output, I get: ???|???|???|???|???|???|???|???|???|???|???|???
With UTF-16 output, I get: 㼿簿㼿簿㼿簿㼿簿㼿簿㼿簿㼿簿㼿簿㼿簿㼿簿㼿簿㼿ി

I'm using the latest Rainmeter beta. Any ideas?
Gadgets Wiki GitHub More Gadgets...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RunCommand help needed with Unicode output

Post by jsmorley »

Not really sure. Brian is probably going to have to weigh in on this, but it looks like some peculiarity in how PowerShell works.

If I simplify your code:

Code: Select all

[Rainmeter]
AccurateText=1
DynamicWindowSize=1
Update=1000
OnRefreshAction=[!CommandMeasure MeasureRunGetMonthNames "Run"]

[MeasureRunGetMonthNames]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell
Parameter=echo "май"
OutputType=UTF-16
State=Hide
Timeout=2000
Substitute="#CRLF#":""

[MeterText]
Meter=String
SolidColor=0,0,0,255
FontColor=255,255,255,255
FontSize=12
Padding=8,8,8,8
X=0
Y=0
Text="Months: [MeasureRunGetMonthNames]"
DynamicVariables=1

I also get 㼿ി. However, if I remove the Program=PowerShell option, so the "echo" is happening in cmd instead, I properly get май returned.
1.png
2.png
So while the output of PowerShell certainly seems to be UTF-16 (Really UCS-2), somehow RunCommand is not getting the correct result. I'm a bit stumped to be honest.
You do not have the required permissions to view the files attached to this post.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: RunCommand help needed with Unicode output

Post by SilverAzide »

jsmorley wrote:So while the output of PowerShell certainly seems to be UTF-16 (Really UCS-2), somehow RunCommand is not getting the correct result. I'm a bit stumped to be honest.
Thanks for checking into this! I appended code to redirect the PowerShell output to a file and it gives the correct result, just like when the output goes to the console. Something does seem to be flakey with RunCommand (redirecting output using the plugin still gives the gibberish result, as one would expect).
Gadgets Wiki GitHub More Gadgets...
User avatar
Brian
Developer
Posts: 2674
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: RunCommand help needed with Unicode output

Post by Brian »

This is actually caused by the Powershell **console** defaulting to Ascii encoding (not the output encoding ie. $OutputEncoding).

To solve this, you need set the console encoding to "Unicode", then run the command.

Something like this should work:

Code: Select all

[Rainmeter]
AccurateText=1
DynamicWindowSize=1
Update=1000
OnRefreshAction=[!CommandMeasure MeasureRunGetMonthNames "Run"]

[MeasureRunGetMonthNames]
Measure=Plugin
Plugin=RunCommand
Parameter="powershell -NonInteractive -Command "[Console]::OutputEncoding = New-Object -typename System.Text.UnicodeEncoding; (([cultureinfo]::currentculture = 'ru-RU').DateTimeFormat.AbbreviatedMonthNames[0..11]) -Join '|'""
OutputType=UTF-16
State=Hide
Timeout=2000
Substitute="#CRLF#":""

[MeterText]
Meter=String
SolidColor=0,0,0,255
FontColor=255,255,255,255
FontSize=12
Padding=8,8,8,8
X=0
Y=0
Text="Months: [MeasureRunGetMonthNames]"
DynamicVariables=1
Hopefully that helps.

-Brian
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RunCommand help needed with Unicode output

Post by balala »

Although I see Brian has fixed the issue and his code works perfectly (congratulations Brian), I would say that depending on what you need , maybe you don't even have to use a PowerShell script for a such output. The following native Rainmeter measure can return the localized month name abbreviation:

Code: Select all

[MeasureMonth]
Measure=Time
Format=%b
FormatLocale=ru-RU
No Powershell script needed. At all.
Again: depends on what you need.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: RunCommand help needed with Unicode output

Post by SilverAzide »

balala wrote:Although I see Brian has fixed the issue and his code works perfectly (congratulations Brian), I would say that depending on what you need , maybe you don't even have to use a PowerShell script for a such output. The following native Rainmeter measure can return the localized month name abbreviation:

Code: Select all

[MeasureMonth]
Measure=Time
Format=%b
FormatLocale=ru-RU
No Powershell script needed. At all.
Again: depends on what you need.
Yes, I know about this, but I need all the months and all the days of the week. Your approach would require 19 measures plus two more to do the concatenation. I was trying to avoid that if possible, but that was the fallback plan if PowerShell didn't work.
Gadgets Wiki GitHub More Gadgets...
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: RunCommand help needed with Unicode output

Post by SilverAzide »

Brian wrote:Hopefully that helps.
Yes it does! Thank you! :thumbup:
Gadgets Wiki GitHub More Gadgets...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RunCommand help needed with Unicode output

Post by jsmorley »

BTW, this form is slightly more efficient, since running the entire thing as a Parameter means it is running cmd.exe then using that to execute powershell.exe.

Code: Select all

[MeasureRunGetMonthNames]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell
Parameter=-NonInteractive -Command "[Console]::OutputEncoding = New-Object -typename System.Text.UnicodeEncoding; (([cultureinfo]::currentculture = 'ru-RU').DateTimeFormat.AbbreviatedMonthNames[0..11]) -Join '|'"
OutputType=UTF16
State=Hide
Timeout=2000
Substitute="#CRLF#":""
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RunCommand help needed with Unicode output

Post by balala »

SilverAzide wrote:Yes, I know about this, but I need all the months and all the days of the week. Your approach would require 19 measures plus two more to do the concatenation. I was trying to avoid that if possible, but that was the fallback plan if PowerShell didn't work.
Yep, that's why I said:
balala wrote:Again: depends on what you need.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RunCommand help needed with Unicode output

Post by jsmorley »

I also think to be on the safe side, you might want:

Code: Select all

Parameter=-NoProfile -ExecutionPolicy Bypass -NonInteractive -Command "[Console]::OutputEncoding = New-Object -typename System.Text.UnicodeEncoding; (([cultureinfo]::currentculture = 'ru-RU').DateTimeFormat.AbbreviatedMonthNames[0..11]) -Join '|'"