This button / code for context menu lets you extract the associated icon of an executable (might be the first icon from the apps resources).
In the future i will create a script command addin with a litttle extended functionality.
To save output as .png change this line
var powerShellCode = GenerateShellScript(exePath, false);
to
var powerShellCode = GenerateShellScript(exePath, true);
Button Code
Extract icon to ico.dcf (5.4 KB)
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
<label>Extract icon to ico</label>
<icon1>#extract</icon1>
<function type="script">
<instruction>@script JScript</instruction>
<instruction>function OnClick(clickData)</instruction>
<instruction>{</instruction>
<instruction> var firstSelectedItem = GetFirstSelectedItemFromTab(clickData.func.sourcetab);</instruction>
<instruction />
<instruction> if(!firstSelectedItem)</instruction>
<instruction> {</instruction>
<instruction> Log("No exe selected", true);</instruction>
<instruction> return;</instruction>
<instruction> }</instruction>
<instruction> </instruction>
<instruction> var ext = firstSelectedItem.realpath.ext.toLowerCase();</instruction>
<instruction> if (!(ext == ".exe")// || ext == ".dll"))</instruction>
<instruction> {</instruction>
<instruction> Log("Selected file is not an executable", true);</instruction>
<instruction> return;</instruction>
<instruction> }</instruction>
<instruction />
<instruction> var exePath = firstSelectedItem.realpath;</instruction>
<instruction> var powerShellCode = GenerateShellScript(exePath, false);</instruction>
<instruction> var powerShellCmdLine = "powershell -command \"" + powerShellCode + "\"";</instruction>
<instruction> </instruction>
<instruction> RunHidden(powerShellCmdLine);</instruction>
<instruction> //Log(RunEx("powershell", "-command \"" + powerShellCode + "\"").stderr);</instruction>
<instruction>}</instruction>
<instruction />
<instruction>function GetFirstSelectedItemFromTab(tab)</instruction>
<instruction>{</instruction>
<instruction> if (tab.selstats.selitems > 0) </instruction>
<instruction> return tab.selected(0);</instruction>
<instruction />
<instruction> return null;</instruction>
<instruction>}</instruction>
<instruction />
<instruction>//https://www.technetnewengland.com/post/extract-an-exe-icon-to-png-using-powershell-no-installs-needed</instruction>
<instruction>function GenerateShellScript(exePath, extractAsImage)</instruction>
<instruction>{</instruction>
<instruction> var shellScript = </instruction>
<instruction> "Add-Type -AssemblyName System.Drawing; " +</instruction>
<instruction> "Add-Type -AssemblyName PresentationCore; " + </instruction>
<instruction> "$exe = '" + exePath + "'; " +</instruction>
<instruction> "$out = [IO.Path]::ChangeExtension($exe, '." + (extractAsImage ? "png" : "ico") + "'); " +</instruction>
<instruction> "$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($exe); " +</instruction>
<instruction> "$fs = New-Object IO.FileStream($out, [IO.FileMode]::Create); ";</instruction>
<instruction />
<instruction> if(extractAsImage)</instruction>
<instruction> {</instruction>
<instruction> shellScript +=</instruction>
<instruction> "$bitmap = $icon.ToBitmap(); " +</instruction>
<instruction> "$bitmap.Save($fs, [System.Drawing.Imaging.ImageFormat]::Png); " +</instruction>
<instruction> "$fs.Close(); " +</instruction>
<instruction> "Write-Host 'Saved PNG icon to $out';";</instruction>
<instruction> }</instruction>
<instruction> else</instruction>
<instruction> {</instruction>
<instruction> shellScript +=</instruction>
<instruction> "$icon.Save($fs); " +</instruction>
<instruction> "$fs.Close(); " +</instruction>
<instruction> "Write-Host 'Saved ico icon to $out';";</instruction>
<instruction> }</instruction>
<instruction> </instruction>
<instruction> return shellScript;</instruction>
<instruction>}</instruction>
<instruction />
<instruction>function RunHidden(cmdLine)</instruction>
<instruction>{</instruction>
<instruction> var cmd = DOpus.Create.Command();</instruction>
<instruction> cmd.SetModifier('runmode','hide');</instruction>
<instruction> cmd.SetType("msdos");</instruction>
<instruction> cmd.AddLine(cmdLine);</instruction>
<instruction> cmd.Run();</instruction>
<instruction>}</instruction>
<instruction />
<instruction>function RunEx(exe, params){</instruction>
<instruction> params = (params?" "+params:"");</instruction>
<instruction> var shell = new ActiveXObject("WScript.Shell");</instruction>
<instruction> var exec = shell.Exec(exe + params);</instruction>
<instruction> var stdOut = "", stdErr = "";</instruction>
<instruction> while(exec.Status == 0){</instruction>
<instruction> stdOut += exec.StdOut.ReadAll();</instruction>
<instruction> stdErr += exec.StdErr.ReadAll();</instruction>
<instruction> }</instruction>
<instruction> return {</instruction>
<instruction> returncode : exec.ExitCode,</instruction>
<instruction> stdout : stdOut,</instruction>
<instruction> stderr : stdErr</instruction>
<instruction> };</instruction>
<instruction>}</instruction>
<instruction />
<instruction>function Log(msg, e) {</instruction>
<instruction> DOpus.output(String(msg), e || false);</instruction>
<instruction>}</instruction>
</function>
</button>
Actual Script Code (for context menu and better readability)
function OnClick(clickData)
{
var firstSelectedItem = GetFirstSelectedItemFromTab(clickData.func.sourcetab);
if(!firstSelectedItem)
{
Log("No exe selected", true);
return;
}
var ext = firstSelectedItem.realpath.ext.toLowerCase();
if (!(ext == ".exe")// || ext == ".dll"))
{
Log("Selected file is not an executable", true);
return;
}
var exePath = firstSelectedItem.realpath;
var powerShellCode = GenerateShellScript(exePath, false);
var powerShellCmdLine = "powershell -command \"" + powerShellCode + "\"";
RunHidden(powerShellCmdLine);
//Log(RunEx("powershell", "-command \"" + powerShellCode + "\"").stderr);
}
function GetFirstSelectedItemFromTab(tab)
{
if (tab.selstats.selitems > 0)
return tab.selected(0);
return null;
}
//https://www.technetnewengland.com/post/extract-an-exe-icon-to-png-using-powershell-no-installs-needed
function GenerateShellScript(exePath, extractAsImage)
{
var shellScript =
"Add-Type -AssemblyName System.Drawing; " +
"Add-Type -AssemblyName PresentationCore; " +
"$exe = '" + exePath + "'; " +
"$out = [IO.Path]::ChangeExtension($exe, '." + (extractAsImage ? "png" : "ico") + "'); " +
"$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($exe); " +
"$fs = New-Object IO.FileStream($out, [IO.FileMode]::Create); ";
if(extractAsImage)
{
shellScript +=
"$bitmap = $icon.ToBitmap(); " +
"$bitmap.Save($fs, [System.Drawing.Imaging.ImageFormat]::Png); " +
"$fs.Close(); " +
"Write-Host 'Saved PNG icon to $out';";
}
else
{
shellScript +=
"$icon.Save($fs); " +
"$fs.Close(); " +
"Write-Host 'Saved ico icon to $out';";
}
return shellScript;
}
function RunHidden(cmdLine)
{
var cmd = DOpus.Create.Command();
cmd.SetModifier('runmode','hide');
cmd.SetType("msdos");
cmd.AddLine(cmdLine);
cmd.Run();
}
function RunEx(exe, params){
params = (params?" "+params:"");
var shell = new ActiveXObject("WScript.Shell");
var exec = shell.Exec(exe + params);
var stdOut = "", stdErr = "";
while(exec.Status == 0){
stdOut += exec.StdOut.ReadAll();
stdErr += exec.StdErr.ReadAll();
}
return {
returncode : exec.ExitCode,
stdout : stdOut,
stderr : stdErr
};
}
function Log(msg, e) {
DOpus.output(String(msg), e || false);
}