It is currently March 29th, 2024, 11:03 am

Using Powershell to extract icon

Get help with creating, editing & fixing problems with skins
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Using Powershell to extract icon

Post by StArL0rd84 »

Found a neat little Powershell script that supposedly can extract a icon from an exe file.
Source
But i need help rewriting it a bit so it will work with RunCommand.
As it is now it spits out errors i don't really understand.

Ps1:

Code: Select all

$exe_in = $args[0]
$ico_out = $args[1]

[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing');
[Drawing.Icon]::ExtractAssociatedIcon(\"($exe_in)").ToBitmap().Save(\"($ico_out)")
ini:

Code: Select all

[Variables]
App4=C:\Program Files (x86)\VideoLAN\VLC\vlc.exe

[mExtract]
Measure=Plugin
Plugin=RunCommand
Program=Powershell.exe
Parameter=-NoProfile -Noninteractive "& '.\ExtractIcon.ps1' '#App4#' '#@#App4.ico'"
OutputType=Ansi
DynamicVariables=1

[MeterString]
Meter=String
X=0
Y=0
FontColor=255,255,255,255
SolidColor=0,0,0,255
FontFace=Segoe UI
FontSize=20
AntiAlias=1
Text=Run
LeftMouseUpAction=[!CommandMeasure mExtract "Run"]
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Powershell to extract icon

Post by raiguard »

I attempted to run the script you had there, and it was throwing a hissy fit. So I instead just copied one of the batch scripts from the link, which appears to be similar. And I managed to make it work!
geticon.gif
I basically mutilated your skin and took out everything except the pertinent code so you can have a look:

Code: Select all

[Rainmeter]
MiddleMouseUpAction=[!Refresh]

[Variables]
app=C:\Program Files (x86)\VideoLAN\VLC\vlc.exe
iconPath=#CURRENTPATH#Icons

[MeasureIconName]
Measure=String
String=#app#
Substitute=".*\\(.*?).exe":"\1.ico"
RegExpSubstitute=1

[MeasureGetIcon]
Measure=Plugin
Plugin=RunCommand
Parameter=icon-extractor.bat "#app#" "#iconPath#"
FinishAction=[!Refresh]

[MeterString]
Meter=String
FontColor=255,255,255,255
SolidColor=0,0,0,255
FontFace=Segoe UI
FontSize=20
AntiAlias=1
Text=Run
LeftMouseUpAction=[!CommandMeasure MeasureGetIcon "Run"]

[MeterImage]
Meter=Image
ImageName=#iconPath#\[MeasureIconName]
X=r
Y=R
SolidColor=0,0,0
DynamicVariables=1
The only thing I haven't been able to figure out is why you need to refresh before the icon will show up. Maybe Rainmeter doesn't like a new file being created in the middle of a skin's lifecycle? I dunno.

Anyway, I hope this helps!
You do not have the required permissions to view the files attached to this post.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Using Powershell to extract icon

Post by StArL0rd84 »

raiguard wrote: May 31st, 2019, 5:37 am The only thing I haven't been able to figure out is why you need to refresh before the icon will show up. Maybe Rainmeter doesn't like a new file being created in the middle of a skin's lifecycle? I dunno.

Anyway, I hope this helps!


Oh wow rai, it really works.
But you don't have to refresh the skin.
FinishAction=[!UpdateMeter MeterImage][!Redraw] works too!

The reason i looked into extracting an icon this way is because i did not like the command-line options out there.
And while the Fileview plugin can search and produce a icon to display, it is really slow.

I would like to shomehow rename the icon in the process to something like App1, 2, 3, and so on.
So i changed some things in th bat file and i got it working.

Code: Select all

[mGetIconApp1]
Measure=Plugin
Plugin=RunCommand
Parameter=#@#bat\icon-extractor.bat "#App1#" "#@#bat" "App1"
FinishAction=[!UpdateMeter aIcon1][!Redraw]

Code: Select all

@echo off
setlocal

set "exe_in=%~1"
set "out_dir=."
set "out_nam=%~3"

if not "%~2"=="" 2>NUL pushd "%~2" && ( call set "out_dir=%%CD%%" & popd )
set "ico_out=%out_dir%\%out_nam%.ico"

set "psCommand="[void][Reflection.Assembly]::LoadWithPartialName('System.Drawing');^
[Drawing.Icon]::ExtractAssociatedIcon(\"%exe_in%\").ToBitmap().Save(\"%ico_out%\")""

powershell -noprofile -noninteractive %psCommand
But i discovered that it can not overwrite App1.ico
Got to find a way to either overwrite or delete App1.ico before it creates a new App1.ico

This could work perfectly for my app launcher.
Thank you for your help!
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Powershell to extract icon

Post by raiguard »

StArL0rd84 wrote: May 31st, 2019, 10:09 am Oh wow rai, it really works.
But you don't have to refresh the skin.
FinishAction=[!UpdateMeter MeterImage][!Redraw] works too!
WTF, that didn't work for me. Glad you managed to get it working! I'll poke around and see about overwriting files.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Using Powershell to extract icon

Post by StArL0rd84 »

raiguard wrote: May 31st, 2019, 4:35 pm WTF, that didn't work for me. Glad you managed to get it working! I'll poke around and see about overwriting files.
Thank you for looking into overwriting files.
Right now i am using an extra Powershell measure to delete the icon before i create a new one.

Code: Select all

[mDeleteIconApp1]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell
Parameter=Remove-Item '#@#Icons\App1.ico'
OutputType=ANSI
DynamicVariables=1
FinishAction=[!CommandMeasure mGetIconApp1 "Run"]
UpdateDivider=-1
Group=IconApp1
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Using Powershell to extract icon [SOLVED]

Post by StArL0rd84 »

I have cracked it!
Took some effort to learn and figure out, but i have managed to Frankenstein a script file together that does three things.

1. Deletes icon
2. Extracts a new icon
3. Retrieves the "friendly" name of the application.
So instead of using RegExpSubstitute to get just: chrome,
We get the full name: Google Chrome.

Code: Select all

Parameter Syntax:
Path-to-script-file "Path to .exe" "Directory to save icon at" "Icon name"

Substitute="#CRLF#":""
The resulting application name has a line break at the end, so to remove that, we can subtitute #CRLF# with "nothing"

FinishAction:
It's is a good idea to save the name to a variable so the meter has the name on next startup.
This way, we avoid having to run the script for all the meters that depend on it on startup, ONLY when the path changes 

[mGetNameAndIconApp1]
Measure=Plugin
Plugin=RunCommand
Parameter=#@#Scripts\bat\get-name-and-icon.bat "#App1#" "#@#Icons" "App1"
OutputType=ANSI
UpdateDivider=-1
DynamicVariables=1
Substitute="#CRLF#":""
FinishAction=[!SetVariable AppName1 "[mGetNameAndiconApp1]"][!WriteKeyValue Variables AppName1 "[mGetNameAndiconApp1]"][!UpdateMeasure mColorApp1][!UpdateMeterGroup App1][!Redraw]
ezgif.com-video-to-gif.gif
The new example skin i have packed uses the DragNDrop plugin by theAzack9.
So just drag and drop your exe file on the skin, and the script file should do it's thing 8-)
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Powershell to extract icon

Post by raiguard »

Wow, that's really awesome! I have been toying with the idea of making a launcher skin myself, and this will be immensely useful! I hope you don't mind if I shamelessly steal this piece of code... :D

Glad I could help!
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Using Powershell to extract icon

Post by eclectic-tech »

raiguard wrote: June 3rd, 2019, 3:37 am Wow, that's really awesome! I have been toying with the idea of making a launcher skin myself, and this will be immensely useful! I hope you don't mind if I shamelessly steal this piece of code... :D

Glad I could help!
Ditto! :oops: :D :welcome:
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Using Powershell to extract icon

Post by StArL0rd84 »

raiguard wrote: June 3rd, 2019, 3:37 am Wow, that's really awesome! I have been toying with the idea of making a launcher skin myself, and this will be immensely useful! I hope you don't mind if I shamelessly steal this piece of code... :D

Glad I could help!
I don't mind at all :bow:
I'm happy that you find it useful.

Just bear in mind that the dragNdrop plugin is kinda buggy right now. Still waiting for the overhaul by theazack9.
You can read my posts on his thread to learn more. But that's why I am using setoption bangs on the plugin as a workaround.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))