0.0.8 • Published 5 years ago
windows-system-icon v0.0.8
windows-system-icon
Extract the associated file icons with Node.js using Powershell on Windows.
Requirements
- Powershell
Usage
NPM
npm install windows-system-icon
TypeScript
import { generateIcons, Icon } from "../dist/index";
const icons: Icon[] = [
{
inputFilePath: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
outputFilePath: "C:\\Users\\Oliver\\Desktop\\Google Chrome Icon.png",
outputFormat: "Png",
},
];
const followShortcuts = false; // this parameter is optional
generateIcons(icons, followShortcuts)
.then(() => console.log("Done!"))
.catch((err) => console.log(err));
JavaScript
import { generateIcons } from "../dist/index";
const icons = [
{
inputFilePath: "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Google Chrome.lnk",
outputFilePath: "C:\\Users\\Oliver\\Desktop\\Google Chrome Icon.png",
outputFormat: "Png",
},
];
const followShortcuts = false; // this parameter is optional
generateIcons(icons, followShortcuts)
.then(() => console.log("Done!"))
.catch((err) => console.log(err));
Supported output formats
Gif
Icon
Jpeg
Png
Tiff
Make sure to capitalize the output formats
How it works
windows-system-icon
uses Powershell in the background to extract the associated file icons.
Here is the powershell script that gets executed in the background:
Add-Type -AssemblyName System.Drawing
$filePath = "<input-file-path>";
$fileExists = Test-Path -Path $filePath;
if ($fileExists -and $filePath.EndsWith(".lnk")) {
Try {
$sh = New-Object -ComObject WScript.Shell;
$filePath = $sh.CreateShortcut($filePath).TargetPath;
}
Catch {
<# do nothing #>
}
}
if ($fileExists) {
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon("<input-file-path>");
$bitmap = $icon.ToBitmap().save("<output-file-path>", [System.Drawing.Imaging.ImageFormat]::<output-format>);
}