Extract app icon to .png and .ico

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(&quot;No exe selected&quot;, true);</instruction>
		<instruction>		return;</instruction>
		<instruction>	}</instruction>
		<instruction>		</instruction>
		<instruction>	var ext = firstSelectedItem.realpath.ext.toLowerCase();</instruction>
		<instruction>    if (!(ext == &quot;.exe&quot;)// || ext == &quot;.dll&quot;))</instruction>
		<instruction>	{</instruction>
		<instruction>		Log(&quot;Selected file is not an executable&quot;, true);</instruction>
		<instruction>		return;</instruction>
		<instruction>	}</instruction>
		<instruction />
		<instruction>	var exePath = firstSelectedItem.realpath;</instruction>
		<instruction>	var powerShellCode = GenerateShellScript(exePath, false);</instruction>
		<instruction>	var powerShellCmdLine = &quot;powershell -command  \&quot;&quot; + powerShellCode + &quot;\&quot;&quot;;</instruction>
		<instruction>	</instruction>
		<instruction>	RunHidden(powerShellCmdLine);</instruction>
		<instruction>	//Log(RunEx(&quot;powershell&quot;, &quot;-command  \&quot;&quot; + powerShellCode + &quot;\&quot;&quot;).stderr);</instruction>
		<instruction>}</instruction>
		<instruction />
		<instruction>function GetFirstSelectedItemFromTab(tab)</instruction>
		<instruction>{</instruction>
		<instruction>	if (tab.selstats.selitems &gt; 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>	&quot;Add-Type -AssemblyName System.Drawing; &quot; +</instruction>
		<instruction>	&quot;Add-Type -AssemblyName PresentationCore; &quot; + </instruction>
		<instruction>    &quot;$exe = &apos;&quot; + exePath + &quot;&apos;; &quot; +</instruction>
		<instruction>	&quot;$out = [IO.Path]::ChangeExtension($exe, &apos;.&quot; + (extractAsImage ? &quot;png&quot; : &quot;ico&quot;) + &quot;&apos;); &quot; +</instruction>
		<instruction>	&quot;$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($exe); &quot; +</instruction>
		<instruction>	&quot;$fs = New-Object IO.FileStream($out, [IO.FileMode]::Create); &quot;;</instruction>
		<instruction />
		<instruction>	if(extractAsImage)</instruction>
		<instruction>	{</instruction>
		<instruction>		shellScript +=</instruction>
		<instruction>		&quot;$bitmap = $icon.ToBitmap(); &quot; +</instruction>
		<instruction>		&quot;$bitmap.Save($fs, [System.Drawing.Imaging.ImageFormat]::Png); &quot; +</instruction>
		<instruction>		&quot;$fs.Close(); &quot; +</instruction>
		<instruction>		&quot;Write-Host &apos;Saved PNG icon to $out&apos;;&quot;;</instruction>
		<instruction>	}</instruction>
		<instruction>	else</instruction>
		<instruction>	{</instruction>
		<instruction>		shellScript +=</instruction>
		<instruction>		&quot;$icon.Save($fs); &quot; +</instruction>
		<instruction>		&quot;$fs.Close(); &quot; +</instruction>
		<instruction>		&quot;Write-Host &apos;Saved ico icon to $out&apos;;&quot;;</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(&apos;runmode&apos;,&apos;hide&apos;);</instruction>
		<instruction>	cmd.SetType(&quot;msdos&quot;);</instruction>
		<instruction>	cmd.AddLine(cmdLine);</instruction>
		<instruction>	cmd.Run();</instruction>
		<instruction>}</instruction>
		<instruction />
		<instruction>function RunEx(exe, params){</instruction>
		<instruction>	params = (params?&quot; &quot;+params:&quot;&quot;);</instruction>
		<instruction>	var shell = new ActiveXObject(&quot;WScript.Shell&quot;);</instruction>
		<instruction>	var exec = shell.Exec(exe + params);</instruction>
		<instruction>	var stdOut = &quot;&quot;, stdErr = &quot;&quot;;</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);
}

c lost in translation?

2 Likes

Thanks, I fixed it in all occurences. Thats what happens with a last second simplification. But have you never heard of lick interaction? :wink:

1 Like