make-icns.ps1
# 根据 png 创建 icns 文件
# ./make-icns.ps1 ./keboo.png , 不传文件名默认为 logo.png
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$pngfile = "./logo.png"
)
$ErrorActionPreference = "Stop"
# 获取文件完整路径和所在目录
$filePath = (Get-Item $pngfile).FullName
$workingDir = Split-Path -Parent $filePath
Set-Location $workingDir
Write-Host "正在处理文件: $pngfile" -ForegroundColor Cyan
# 1. 创建临时文件夹
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($pngfile)
$iconsetName = "$($fileName).iconset"
if (Test-Path $iconsetName) { Remove-Item -Recurse -Force $iconsetName }
New-Item -ItemType Directory -Name $iconsetName
# 2. 定义尺寸清单 (物理像素, 文件名)
$sizes = @(
@("16", "icon_16x16.png"),
@("32", "icon_16x16@2x.png"),
@("32", "icon_32x32.png"),
@("64", "icon_32x32@2x.png"),
@("128", "icon_128x128.png"),
@("256", "icon_128x128@2x.png"),
@("256", "icon_256x256.png"),
@("512", "icon_256x256@2x.png"),
@("512", "icon_512x512.png"),
@("1024", "icon_512x512@2x.png")
)
# 3. 循环调用 sips 生成图片
foreach ($size in $sizes) {
$px = $size[0]
$name = $size[1]
# 注意:macOS 下可以直接在 pwsh 中调用 sips
sips -z $px $px $pngfile --out "$iconsetName/$name" | Out-Null
if(-not $?){
throw "sips $px $px 命令执行失败!"
}
}
# 4. 合成 icns
iconutil -c icns $iconsetName
Write-Host "转换完成!已生成 $($fileName).icns" -ForegroundColor Green
# 5. 清理
Remove-Item -Recurse -Force $iconsetName